/** * Overridden isValid() method for pre-validation code * * @param array $formData data typically from a POST or GET request * * @return bool */ public function isValid($formData = array()) { // Need to load the letting agents into the drop down or it fails validation // with an invalid id - by default zend does an inArray check on select boxes if (array_key_exists('letting_agent_asn', $formData)) { $agentLookup = new Datasource_Core_Agents(); $agentList = $agentLookup->searchByAsnOrNameAndAddress($formData['letting_agent_asn'], $formData['letting_agent_name'], $formData['letting_agent_town']); $agentDropdownValues = array(); if ($agentList) { foreach ($agentList as $agent) { $agentDropdownValues[$agent['asn']] = $agent['name'] . ', ' . $agent['address']; } } $agentDropdown = $this->letting_agent; $agentDropdown->setMultiOptions($agentDropdownValues); $agentDropdown->setRequired(true); } // Check if letting_agent_agent is 'Yes', if so then either: // letting_agent_name AND letting_agent_town are mandatory, OR // letting_agent_asn is mandatory if (isset($formData['letting_agent_has']) && $formData['letting_agent_has'] == 'yes') { // Set all three to mandatory first $this->getElement('letting_agent_name')->setRequired(true); $this->getElement('letting_agent_town')->setRequired(true); $this->getElement('letting_agent_asn')->setRequired(true); // Decide which can be un-mandatory if (trim($formData['letting_agent_asn']) != '') { $this->getElement('letting_agent_name')->setRequired(false); $this->getElement('letting_agent_town')->setRequired(false); } if (trim($formData['letting_agent_name']) != '' && trim($formData['letting_agent_town']) != '') { $this->getElement('letting_agent_asn')->setRequired(false); } } elseif (isset($formData['letting_agent_has']) && $formData['letting_agent_has'] == 'no') { // The user has said they don't have a letting agent so ignore all the validation // TODO: This is a dirty fix to QC4364 - this code is so complex and dirty that I can't actually find // why it's throwing weird haystack validation errors. Needs re-factoring. return true; } // Call original isValid() return parent::isValid($formData); }
/** * Finds agents that match name and town, each of which may be partially given. * * @param string $name Agent's name. * @param string $town Agent's town. * @return array Simple array of results, primarily for display. * @throws Zend_Exception */ public function searchByNameAndAddress($name, $town) { return $this->_agentDatasource->searchByAsnOrNameAndAddress('', $name, $town); }
/** * Validate and return agent list for use via AJAX * * @return void */ public function getagentsAction() { $output = array(); $request = $this->getRequest(); $postdata = $request->getPost(); $agentLookup = new Datasource_Core_Agents(); $output['data'] = $agentLookup->searchByAsnOrNameAndAddress($postdata['letting_agent_asn'], $postdata['letting_agent_name'], $postdata['letting_agent_town']); $output['errorJs'] = ''; $output['errorCount'] = 0; $output['errorHtml'] = ''; echo Zend_Json::encode($output); }