John x
John x

Reputation: 4031

Get the elements with same name zend framework

i have a scenario where i am adding multiple qualifications, i am saving the values in hidden fields like

 $("<input/>",{type:'hidden',value:deg,name:'degree[]'}).attr("data-deg",deg).appendTo("form");

$("<input/>",{type:'hidden',value:year,name:'year[]'}).attr("data-year",year).appendTo("form");

$("<input/>",{type:'hidden',value:inst,name:'inst[]'}).attr("data-inst",inst).appendTo("form");

if i do var_dump it shows me the following output

["degree"]=> array(2) { [0]=> string(4) "M.Sc" [1]=> string(4) "B.Sc" }

in the controller im getting it like

$Degree = $form->getValue('degree[]'); 

but var_dump($Degree) gives me NULL, how can i get the values plz help

EDIT

here is my form looks like

$degreename = $this->createElement('select', 'degreename')
        ->setRequired(true);
        $degreename->removeDecorator('label');
        $degreename->removeDecorator('htmlTag');
        $degreename->removeDecorator('Errors');
        $ddloptions = $this->db->GetDegrees();
        $degreename->setMultiOptions($ddloptions);
        $degreename->addMultiOptions(array(
                '0'=>'--Select Degree--'
        ));
        $degreename->setValue(0);


        $passingyear = $this->createElement('select', 'passingyear')
        ->setRequired(true);
        $passingyear->removeDecorator('label');
        $passingyear->removeDecorator('htmlTag');
        $passingyear->removeDecorator('Errors');
        $passingyear->addMultiOptions(array(
                '0'=>'--Select Passing Year--',
                '1950'=>'1950',
                '1951'=>'1951',
                '1952'=>'1952',
        )
        );
        $passingyear->setValue('0');


$institute = new Zend_Form_Element_Text('institute');
$institute->setRequired(false)
->addFilter('StripTags')
->addFilter('StringTrim');
$institute->removeDecorator('label');
$institute->removeDecorator('htmlTag');
$institute->removeDecorator('Errors');

though the degreename and the passing year are required they always has the value 0 institute i have set to false the user can add multiple qualifications and upon each user selection i add a hidden field like shown above

Upvotes: 0

Views: 1078

Answers (2)

Adrian World
Adrian World

Reputation: 3128

How about

$Degree = $form->getValue('degree'); 
'degree[]' is not a proper key in php and the POST request

UPDATE1:
Your Javascript piece for the hidden elements still puzzles me a bit and I wonder if you have a "degree" element in $form. To get a final answer it might be good to know what you use inside your $form.

If you just need the submitted data (as is) you are just fine getting the information from $_POST. You wouldn't need Zend_Form for that.

UPDATE2:
Now seeing your form code the fog if lifting; you are missing the elements for you hidden elements. When you call isValid($_POST) you have nothing in the form to validate the data. Calling getValue('degree') or actually any of the hidden element will return NULL.

If you just want the data you will be fine with just $_POST but that is not really secure data handling. Zend_Form elements allow you to add filters and validators for sanitizing the data. For that you have to either create the hidden elements with Zend_Form (use Javascript only to add the values) or have elements in your form you use only for validation. You can use something like if ( !empty($_POST) ) to added them for validation.

Upvotes: 1

Mr Coder
Mr Coder

Reputation: 8196

When you create instance of degree then do set it as array

  $degree = new Zend_Form_Element_Hidden('degree');
  $degree->setIsArray(true);

Now $form->degree->getValue() ; //will return array ;

Upvotes: 2

Related Questions