2

It might be a easy but I'm new on this.

I'm trying to write a simple post & category system. I created an category module, and also post module.

I want to add selectbox for select which category of this to addpost page. I dont know how. Can anyone help me?

2

1 Answer 1

1

you just have to inject the resultset in your form and make an array with to pass it in select options.

<?php
namespace Mylib\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Mylib\Form\MyForm;

class MyControler extends AbstractActionController
{
    private $myObjectTable;

    public function getMyObjectTable(){
        if(!$this->myObjectTable){
            $this->myObjectTable = $this->getServiceLocator()
                ->get('MyLib\Model\MyObjectTable');
        }
    }

    public function indexAction(){
        $objects = $this->getMyObjectTable()->fetchall();

        $form = new MyForm($objects);

        $filter = new MyFormFilter();
        $form->setInputFilter($filter->getInputFilter());

        $form->setData($request->getPost());
        if($form->isValid()){
            // [...] here the post treatment
        }
        return array('form' =>$form);
    }
}

and in your form :

<?php
namespace Mylib\Form;

class MyForm
{
    public function __construct($objects){
        $selectOptions = array();
        foreach ($objects as $object) {
            $selectOptions[$object->id]=$object->thePropertyIWantToList;
        }
        $this->add(array(
            'name' => 'object_id',
            'type' => 'Select',
            'options' => array(
                'value_options' => $selectOptions,
                'label' => 'MySelectBoxLabel',
            )
            'attributes' => array(
                'value' => 9, // to select a default value
            )
        ));
        //and the rest of the form... 
         $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'id' => 'submitbutton',
                'value' => 'Go',
            ),
        ));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.