I have a Zend form with three select boxes. When the form is first loaded only the first select box is populated. The user chooses a value from the first box and this populates the second and third boxes.
$element = $this->createElement('select', 'clinicId');
$element->addMultiOption('', ' ');
foreach($this->_clinics as $id => $details)
$element->addMultiOption($id, $details['clinic_name']);
$element->setAttribs(array('onchange' => 'toggleClinic(this.value, ' . $json . ')'));
$element->setRequired(true);
$element->addErrorMessage('You must select a clinic');
$element->addFilter('StringTrim');
$element->setLabel('Clinic');
$element->clearDecorators();
$element->addDecorator('StandardTable');
if(isset($this->_info['clinic_id']))
$element->setValue($this->_info['clinic_id']);
$this->addElement($element);
$this->_elementNames[] = 'clinicId';
$element = $this->createElement('select', 'attendeeId');
$element->addMultiOption('', ' ');
$element->setRequired(true);
$element->addErrorMessage('You must select an attendee');
$element->addFilter('StringTrim');
$element->setLabel('Appointment With');
$element->clearDecorators();
$element->addDecorator('StandardTable');
if (empty($this->_info['clinic_id']))
$element->setAttribs(array('disabled' => true));
else
{
foreach($this->_clinics[$this->_info['clinic_id']]['physicians'] as $userId => $userName)
$element->addMultiOption($userId, $userName);
if(isset($this->_info['provider_id']))
$element->setValue($this->_info['provider_id']);
}
$this->addElement($element);
$this->_elementNames[] = 'attendeeId';
The third select box is almost identical to the second, just with different labels, variables, etc.
In my javascript toggleClinic() function I have:
$("#attendeeId").removeAttr('disabled');
$("#appointmentType").removeAttr('disabled');
for(id in jsonClinics[selectedId].physicians)
{
var o = new Option(jsonClinics[selectedId].physicians[id], id);
$(o).html(jsonClinics[selectedId].physicians[id]);
$("#attendeeId").append(o);
}
for(id in jsonClinics[selectedId].appointment_types)
{
var o = new Option(jsonClinics[selectedId].appointment_types[id]['name'], id);
$(o).html(jsonClinics[selectedId].appointment_types[id]['name']);
$("#appointmentType").append(o);
}
This all appears to work. I choose a clinic, and the two other drop downs are correctly populated. I can select items from all three checkboxes, submit the form, and Firebug shows all variables being sent via post. However, in my action in the controller, if ($this->_loader->appointmentform->isValid($request->getPost())) returns false, and the form is redisplayed with errors for the second and third select box. Their values have also been removed so I have to change the selected option of the first select box to retrigger toggleClinic.
Of main concern is the first part, there is obviously something being selected, so why is isValid returning false? Any help would be greatly appreciated.
It was an extremely simple error.
The way my action worked, the same action was used for creating the form and submitting it, so it had the check to see if the request was a post request after creating the form. I was not passing the submitted value from the first select box to the form, so it was being recreated without anything selected, thus causing the two dynamic select boxes to be unpopulated, causing the error.
Adding an extra check before creating the form allowed me to send the submitted information back to the form as a parameter, and fixed the problem.