How do I set the "value" in my Zend form?

The Zend form is a little tricky for me, even if I've been working with it lately ...

I have this form and I am trying to dynamically create multiple checkboxes for it. It works fine, except that I cannot change the value attribute.

In my Zend form class, I have this snippet ...

// psychotic symptoms
        $this->addElement('checkbox', 'psychoticsymptom', array(
                'label' => 'psychoticsymptom',
                'name' => 'psychoticsymptom',
        ));

      

In my view (phtml) I call it like this ...

<div class="element">
    <?php // Psychotic Symptoms
    $Criteria = new Criteria();
    $Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
    $Criteria->addAscendingOrderByColumn( 'Ordinal' );
    $this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );

    foreach( $this->PsychoticSymptomsList as $Symptom ) {

        $form->psychoticsymptom->setValue($Symptom->getDictionaryId());
        $form->psychoticsymptom->setAttrib('name', $Symptom->getWord());

        echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
        $form->psychoticsymptom->getDecorator('label')->setTag(null);

        echo $form->psychoticsymptom->renderViewHelper();
        $form->psychoticsymptom->setLabel($Symptom->getWord());
        echo $form->psychoticsymptom->renderLabel();
        echo '<br />';
                    }
        ?>
    </div>

      

Everything seems to work fine, except that the value attribute in each checkbox passes the value "1". I tried to move the 'setValue' line a few different positions to set the value before the form element is rendered, but I am unable to get that to work. It is worth the effort for me because I need to do the same action in many areas of my application. I would do it a little differently too, but I'm redeploying another application and trying to keep some things unchanged (like a database for example).

Any help is meaningful Thanks

+3


source to share


2 answers


you can try overwriting "checkedValue" and "uncheckedValue". check the link



$this->addElement('checkbox', 'psychoticsymptom', array(
                'label' => 'psychoticsymptom',
                'name' => 'psychoticsymptom',
                'checkedValue' => 'checked Value',
                'uncheckedValue' => 'unchecked Value'
        ));

      

+9


source


You only seem to have one "checkbox" psychoticosymptom element that adds (changes) a value for each $ this-> PsychoticSymptomsList.



You might be better off using the multicheckbox element.

+1


source







All Articles