2

I wanted to pass a variable from a controller to the form. How can this be implemented? Can anyone please help me on this issue.

Thank you.

3
  • 1
    What kind of variable are we talking about here? A value for a field? Or values to generate a select box with for instance? Commented Mar 31, 2011 at 7:41
  • No it is not related to populating in form element. I referring to case similar to view when we pass $this->view->variableName = 'test'; Is it possible to do anything similar to this for the case of passing variable to form? Commented Mar 31, 2011 at 8:01
  • The question is not clear. There are many ways in which you can pass a variable to the form. Do you need this variable during creation of the form, validation? Could you provide an example what you want this variable for? Commented Mar 31, 2011 at 10:34

3 Answers 3

4

Just a live example:

class Admin_Form_Product extends Admin_Form_Abstract {

    protected $_categories = array();

    protected $_brands = array();

    protected $_types = array();

    protected $_periods = array();

    protected $_stores = array();

    public function init() {
        $this
            ->addElement('hidden', 'id')
            ->addElement('text', 'name', array('label' => 'Name:', 'required' => true, 'size' => 50))
            ->addElement('multiCheckbox', 'category_id', array('label' => 'Category:', 'required' => true, 'multiOptions' => $this->getCategories()))
            ->addElement('multiCheckbox', 'store_id', array('label' => 'Stores:', 'required' => true, 'multiOptions' => $this->getStores()))
            ->addElement('select', 'brand_id', array('label' => 'Brand:', 'required' => true, 'multiOptions' => $this->getBrands()))
            ->addElement('select', 'type_id', array('label' => 'Type:', 'required' => true, 'multiOptions' => $this->getTypes()))
            ->addElement('select', 'period_id', array('label' => 'Period:', 'required' => true, 'multiOptions' => $this->getPeriods()))
            ->addElement('textarea', 'description', array('label' => 'Description:', 'rows' => 12, 'cols' => 50))
            ->addElement('text', 'price', array('label' => 'Price ($):', 'required' => true, 'size' => 7))
            ->addElement('text', 'quantity', array('label' => 'Quantity:', 'required' => true, 'size' => 7, 'value' => 1))
            ->addElement('button', 'submit', array('label' => 'Save', 'type' => 'submit'));

        $this->category_id->setValidators(array(new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::NULL)));
        $this->brand_id->setValidators(array(
            new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::NULL)
        ));
        $this->price->setValidators(array(new Zend_Validate_Float()));
    }

    public function setCategories($categories = array()) {
        $this->_categories = $categories;
        return $this;
    }

    public function getCategories() {
        return $this->_categories;
    }

    public function setBrands($brands) {
        $this->_brands = $brands;
    }

    public function getBrands() {
        return $this->_brands;
    }

    public function setTypes($types) {
        $this->_types = $types;
    }

    public function getTypes() {
        return $this->_types;
    }

    public function setPeriods($periods) {
        $this->_periods = $periods;
    }

    public function getPeriods() {
        return $this->_periods;
    }

    public function setStores($stores) {
        $this->_stores = $stores;
    }

    public function getStores() {
        return $this->_stores;
    }

    public function prepareDecorators() {
        $this
            ->clearDecorators()
            ->addDecorator(new Zend_Form_Decorator_ViewScript(array('viewScript' => 'forms/product-form.phtml')))
            ->addDecorator('Form');

        $this->setElementDecorators(array('ViewHelper'));

        return parent::prepareDecorators();
    }

}

Then in you controller you can just pass necessary vars as config into form constructor:

public function addAction() {
    $categories = $this->_helper->service('Category')->getCategories();
    $brands = $this->_helper->service('Brand')->getBrands();
    $types = $this->_helper->service('Type')->getTypes();
    $stores = $this->_helper->service('Store')->getStores();
    $periods = $this->_helper->service('Period')->getPeriods();
    $filter = new Skaya_Filter_Array_Map('name', 'id');
    $filterCategory = new Skaya_Filter_Array_ParentRelation();

    $form = new Admin_Form_Product(array(
        'name' => 'user',
        'action' => $this->_helper->url('save'),
        'method' => Zend_Form::METHOD_POST,
        'categories' => $filterCategory->filter($categories->toArray()),
        'brands' => $filter->filter($brands->toArray()),
        'types' => $filter->filter($types->toArray()),
        'periods' => $filter->filter($periods->toArray()),
        'stores' => $filter->filter($stores->toArray())
    ));
    $form->prepareDecorators();
    $this->view->form = $form;
}
Sign up to request clarification or add additional context in comments.

Comments

3

In the controller method of your application. addMultiOptions($options);

class OrderController extends Zend_Controller_Action
{   
    $options = array(
        'foo' => 'Foo2 Option',
        'bar' => 'Bar2 Option',
        'baz' => 'Baz2 Option',
        'bat' => 'Bat2 Option' 
    );

    $form  = new Application_Form_PlaceNewOrder();
    $form->items->addMultiOptions($options);

    ....
}

In the form script of your application.

class Application_Form_PlaceNewOrder extends Zend_Form
{
    public function init()
    { 
        $items= new Zend_Form_Element_Select('items');
        $items->setLabel('Items: *');       
        $items->setValue('foo');
        ....
}

Comments

1

when you create the form you can add a custom function

class Application_Form_Sample extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post')->setAction('/sampleController/sampleAction');

    $name = new Zend_Form_Element_Text('name');
    $name->setLabel('Nome')
        ->setDescription("Here goes the name")
        ->setRequired(true);

        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setIgnore(true);

        $this->addElement($name)
        ->addElement($submit);
    }
    public function myFunction($param) {
        //do something with the param
    }
}

from your controller you can call that function and "edit" or "do what you want" with the form

2 Comments

I needed to pass the value that in passed in "myFunction()" i.e. $param inside init() function. IS this possible? I tried this but nothing is printed
what do you mean with printed? what are you trying to do? can you the meaning of "myFunction" here is to do exactly what you want to do, can you provide your code and explain better what you need to do?

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.