-- Terre Porter <[EMAIL PROTECTED]> wrote
(on Thursday, 04 December 2008, 04:23 PM -0500):
> Hi all,
>  
> Am I reading what this function does incorrectly? 
> 
> I read it as, set an error message for element, and set the element as
> invalid or the same as what the comment says.

This was reported on-list yesterday, and an issue has been filed. In
short, markAsError() is not being honored during isValid().

> 
> --------------------- zend/form/element.php
>       /**
>      * Add an error message and mark element as failed validation
>      * 
>      * @param  string $message 
>      * @return Zend_Form_Element
>      */
>     public function addError($message)
>     {
>         $this->addErrorMessage($message);
>         $this->markAsError();
>         return $this;
>     }
> ---------------------
> 
> I've created a test function below. Test results are under the code.
> 
> ---------------------
> Test code: 
> ---------------------
> 
> /* \/---------- code inside generic index action controller */
> 
> /* force error switch, to test with */
> $testMakeInvalid = false;
> 
> /* if is form post */
> if ($this->getRequest()->isPost()) {
> 
>       /* load form with post values */
>       $form = $this->getForm1($_POST, testMakeInvalid );
> 
>       /* is form valid ? */
>       if (!$form->isValid($_POST)) {
>               /* form is invalid */
>               echo '-- form is NOT valid<BR>';
>       } else {
>               /* for is valid */
>               echo '-- form is valid<BR>';
>       }
> } else {
>       /* load empty form */
>       $form = $this->getForm1(array(), testMakeInvalid );
> }
> $this->view->form = $form ;
> /* /\----------  code inside generic index action controller */
> 
> /* view template contains */
> <? echo echo $this->form; ?>
> 
> /* the get form function */
> Function getForm ($data = array(), $testMakeInvalid = false) {
> 
> /* create form */
> $form = new Zend_Form();
> $form->setMethod('post');
> $form->setAttrib('id', 'reg-free');
> 
> /* create form element */
> $elm = new Zend_Form_Element_Text('element1');
> $elm->setOptions(array('maxlength' => 125, 'class' => 'frmImput', 'size' =>
> 32));
> $elm->setLabel('test field');
> $elm->setRequired(true);
> $elm->addValidator('regex', false, array('/^((0[1-9]|1[012])[-
> \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)[0-9][0-9])/i'));
> $elm->addFilter('stringTrim');
> $elm->addValidator('stringLength', false, array(1,125));
> 
> if ($testMakeInvalid) {
>       $elm->addError('force element error');
> } else {
>       /* check if data is passed to element */
>       if (isset($data['element1'])) {
>               echo '-- has data<BR>'; 
>               $elm->addError('with form data error here');
> 
>       } else {
>               echo '-- no data<BR>';
>               $elm->addError('empty form load error here');
>       }
> }
> 
> /* add in a submit button */
> $submit=$form->CreateElement('submit','submit')->setLabel('Save');
> 
> /* add form elements to form */
> $form->addElement($expDate);
> $form->addElement($submit);
> 
> /* return form object */
> return $form;
> 
> }
> ---------------------
> 
> The results are as follows :
> 
> -- Test #1 
> 
> Set $testMakeInvalid = false;
> Empty form load
> 
> Result -> "empty form load error here"
> 
> This was the expected result.
> 
> -- Test #2 
> 
> Set $testMakeInvalid = false;
> Post form with value "invalid" (fails the regex validation)
> 
> Result -> "with form data error here"
> 
> This was the expected result.
> 
> -- Test #3
> 
> Set $testMakeInvalid = true;
> Form empty or not empty
> 
> Result -> "force element error"
> 
> This was the expected result
> 
> -- Test #4
> 
> Set $testMakeInvalid = false;
> Post form with value "01/02/2003" (passes the regex validation)
> 
> Result -> "form valid"
> 
> WHAT!!! The expected result was for it to fail.
> 
> It ignored the $elm->addError statement totally.
> 
> Is this a bug or is the $elm->addError function ignored on purpose if the
> element passes previous validation checks?
> 
> I know I can write my own validation function but using the $elm->addError
> just looked simpler with less code.
> 
> Thanks 
> Terre
> 
> 

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend Framework           | http://framework.zend.com/

Reply via email to