/** * @group ZF-9540 */ public function testSubFormTranslatorPreferredOverDefaultTranslator() { $defaultTranslations = array('isEmpty' => 'Default message'); $subformTranslations = array('isEmpty' => 'SubForm message'); $defaultTranslate = new Translator('ArrayAdapter', $defaultTranslations); $subformTranslate = new Translator('ArrayAdapter', $subformTranslations); Registry::set('Zend_Translator', $defaultTranslate); $this->form->addSubForm(new SubForm(), 'subform'); $this->form->subform->setTranslator($subformTranslate); $this->form->subform->addElement('text', 'foo', array('required' => true)); $this->assertFalse($this->form->isValid(array('subform' => array('foo' => '')))); $messages = $this->form->getMessages(); $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']); $this->assertFalse($this->form->isValidPartial(array('subform' => array('foo' => '')))); $messages = $this->form->getMessages(); $this->assertEquals('SubForm message', $messages['subform']['foo']['isEmpty']); }
public function testErrorMessagesFromPartialValidationAreLocalizedWhenTranslateAdapterPresent() { $this->_checkZf2794(); $translations = (include dirname(__FILE__) . '/_files/locale/array.php'); $translate = new Zend_Translate('array', $translations, 'en'); $translate->setLocale('en'); $this->form->addElements(array('foo' => array('type' => 'text', 'options' => array('required' => true, 'validators' => array('NotEmpty'))), 'bar' => array('type' => 'text', 'options' => array('required' => true, 'validators' => array('Digits')))))->setTranslator($translate); $data = array('foo' => ''); if ($this->form->isValidPartial($data)) { $this->fail('Form should not validate'); } $messages = $this->form->getMessages(); $this->assertTrue(isset($messages['foo'])); $this->assertFalse(isset($messages['bar'])); foreach ($messages['foo'] as $key => $message) { if (array_key_exists($key, $translations)) { $this->assertEquals($translations[$key], $message); } else { $this->fail('Translation for ' . $key . ' does not exist?'); } } }
/** * @group ZF-9364 */ public function testElementTranslatorPreferredOverDefaultTranslator() { $defaultTranslations = array('isEmpty' => 'Default message'); $formTranslations = array('isEmpty' => 'Form message'); $elementTranslations = array('isEmpty' => 'Element message'); $defaultTranslate = new Zend_Translate('array', $defaultTranslations); $formTranslate = new Zend_Translate('array', $formTranslations); $elementTranslate = new Zend_Translate('array', $elementTranslations); Zend_Registry::set('Zend_Translate', $defaultTranslate); $this->form->setTranslator($formTranslate); $this->form->addElement('text', 'foo', array('required' => true, 'translator' => $elementTranslate)); $this->form->addElement('text', 'bar', array('required' => true)); $this->assertFalse($this->form->isValid(array('foo' => '', 'bar' => ''))); $messages = $this->form->getMessages(); $this->assertEquals(2, count($messages)); $this->assertEquals('Element message', $messages['foo']['isEmpty']); $this->assertEquals('Form message', $messages['bar']['isEmpty']); $this->assertFalse($this->form->isValidPartial(array('foo' => '', 'bar' => ''))); $messages = $this->form->getMessages(); $this->assertEquals(2, count($messages)); $this->assertEquals('Element message', $messages['foo']['isEmpty']); $this->assertEquals('Form message', $messages['bar']['isEmpty']); }
public function testCanValidatePartialNestedFormsWithElementsBelongingToArrays() { $form = new Zend_Form(); $form->setElementsBelongTo('foobar'); $form->addElement('text', 'firstName') ->getElement('firstName') ->setRequired(false); $form->addElement('text', 'lastName') ->getElement('lastName') ->setRequired(true); $subForm = new Zend_Form_SubForm(); $subForm->setElementsBelongTo('foobar[baz]'); $subForm->addElement('text', 'email') ->getElement('email') ->setRequired(true) ->addValidator('NotEmpty'); $subSubForm = new Zend_Form_SubForm(); $subSubForm->setElementsBelongTo('foobar[baz][bat]'); $subSubForm->addElement('checkbox', 'home') ->getElement('home') ->setRequired(true) ->addValidator('NotEmpty'); $subForm->addSubForm($subSubForm, 'subSub'); $form->addSubForm($subForm, 'sub') ->addElement('submit', 'save', array('value' => 'submit')); $data = array('foobar' => array( 'lastName' => 'Cow', )); $this->assertTrue($form->isValidPartial($data)); $this->assertEquals('Cow', $form->lastName->getValue()); $firstName = $form->firstName->getValue(); $email = $form->sub->email->getValue(); $home = $form->sub->subSub->home->getValue(); $this->assertTrue(empty($firstName)); $this->assertTrue(empty($email)); $this->assertTrue(empty($home)); $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6)); $data['foobar']['baz'] = array('bat' => array('home' => 'ab')); $this->assertFalse($form->isValidPartial($data), var_export($form->sub->subSub->home, 1)); $this->assertEquals('1', $form->sub->subSub->home->getValue()); $messages = $form->getMessages(); $this->assertFalse(empty($messages)); $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']), var_export($messages, 1)); $this->assertTrue(isset($messages['foobar']['baz']['bat']['home']['stringLengthTooShort'])); }
/** * 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()) { // Check if all 3 pieces of info are supplied $asn = isset($formData['letting_agent_asn']) ? preg_replace('/[^\\d]/', '', $formData['letting_agent_asn']) : ''; $irn = isset($formData['tenant_reference_number']) ? preg_replace('/[^\\d]/', '', $formData['tenant_reference_number']) : ''; $dob = isset($formData['tenant_dob']) ? preg_replace('/[^\\d\\/]/', '', $formData['tenant_dob']) : ''; // If the IRN is not actually an IRN, but an IRIS reference number (i.e. prefixed with HLT) if (preg_match('/^HLT\\d+$/', $formData['tenant_reference_number'])) { return self::IRIS_LOGIN; } if ($dob != '') { try { //Check for a valid date of birth. If this causes an exception, end //the process here rather than passing up to the overriden method, //otherwise the same exception will be thrown again. $isDobError = false; $dob = new Zend_Date($dob); } catch (Zend_Exception $e) { $isDobError = true; } if ($isDobError) { $this->getElement('tenant_dob')->addError('Please provide a valid date of birth.'); // Validate other fields, to keep the error messages consistent unset($formData['tenant_dob']); $dummy = parent::isValidPartial($formData); return false; } } //Continue the validation. $displayErrorMessage1 = false; $displayErrorMessage2 = false; $displayErrorMessage3 = false; $displayErrorMessage4 = false; if ($asn != '' && $irn != '' && $dob != '') { // Check if a record can be found $tatManager = new Manager_Referencing_Tat($irn); if ($tatManager->isLoginValid($asn, $dob)) { // Check if user allowed to login if (!$tatManager->isTatApplicable()) { // Can't log in. Check if this is because the TAT has expired. if ($tatManager->isTatExpired()) { $displayErrorMessage1 = true; } else { //If the reference is of type INSIGHT or XPRESS, then provide a specific error message. $referenceManager = new Manager_ReferencingLegacy_Munt(); $reference = $referenceManager->getReference($irn); $product = $reference->productSelection->product; if (isset($product->variables[Model_Referencing_ProductVariables::CREDIT_REFERENCE])) { $displayErrorMessage2 = true; } else { //Set the generic form-level error. $displayErrorMessage3 = true; } } } } else { // Can't find a record, set a form-level error $displayErrorMessage3 = true; } } else { // One or more fields are empty (when filtered), set a form-level error $displayErrorMessage4 = true; } //Display the error message if appropriate. if ($displayErrorMessage1) { $this->addError('Unfortunately we only hold information on the Tenant Application Tracker for 30 days and we believe that your application was completed over a month ago. If you have any questions about your tenancy application please speak directly with your letting agent.'); } else { if ($displayErrorMessage2) { $this->addError('Oops, we\'re unable to match the information you\'ve entered, please check your details and try again.'); } else { if ($displayErrorMessage3) { $this->addError('I\'m sorry we\'ve been unable to find your application with the details that you\'ve provided. Please check the details that you entered and try again.'); } else { if ($displayErrorMessage4) { $this->addError('Please ensure you complete all 3 fields before selecting submit.'); } } } } // Call original isValid() return parent::isValid($formData); }
/** * Wrappter to track if the form has undergone a partial validation attempt * * @param array $data * @return type */ public function isValidPartial(array $data) { $bValidPartial = parent::isValidPartial($data); $this->_bHasBeenValidated = true; return $bValidPartial; }
public function testCanGetMessagesOfNestedFormsWithMultiLevelElementsBelongingToArrays() { $this->_checkZf2794(); $form = new Zend_Form(); $form->setElementsBelongTo('foo[bar]'); $form->addElement('text', 'firstName')->getElement('firstName')->setRequired(false); $form->addElement('text', 'lastName')->getElement('lastName')->setRequired(true); $subForm = new Zend_Form_SubForm(); $subForm->setElementsBelongTo('baz'); $subForm->addElement('text', 'email')->getElement('email')->setRequired(true)->addValidator('NotEmpty'); $subSubForm = new Zend_Form_SubForm(); $subSubForm->setElementsBelongTo('bat[quux]'); $subSubForm->addElement('checkbox', 'home')->getElement('home')->setRequired(true)->addValidator('InArray', false, array(array('1'))); $subForm->addSubForm($subSubForm, 'subSub'); $form->addSubForm($subForm, 'sub')->addElement('submit', 'save', array('value' => 'submit')); $data = array('foo' => array('bar' => array('lastName' => 'Cow'))); $form->sub->subSub->home->addValidator('StringLength', false, array(4, 6)); $data['foo']['bar']['baz'] = array('bat' => array('quux' => array('home' => 'ab'))); $form->isValidPartial($data); $messages = $form->getMessages(); $this->assertFalse(empty($messages)); $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']), var_export($messages, 1)); $this->assertTrue(isset($messages['foo']['bar']['baz']['bat']['quux']['home']['notInArray']), var_export($messages, 1)); }