-- Jonathan Tullett <[EMAIL PROTECTED]> wrote
(on Wednesday, 09 April 2008, 08:05 AM +0100):
> I'm running v1.5.1 of the framework and am coming into difficulties with  
> form validation in the Zend_Form class.
>
> The difficulty is that if I have non-required field, the validators  
> attached to the form element still execute.
>
> As an example, take the following form:
>
> $form = new Zend_Form(array(
>      'action' => '/form/process',
>      'method' => 'post',
>      'elements' => array(
>          'Fax' => array('text', array(
>                 'validators' => array(
>                               array('stringLength', false, array(8,32)),
>                  ),
>                  'label' => 'Fax',
>           )),
>
>          'submit' => 'submit',
>       ),
> ));
>
> According to the documentation  
> (http://framework.zend.com/manual/en/zend.form.elements.html), unless  
> the property 'required' is set to true, the field defaults to  
> not-required (required:false).

Hmmm... I tried the above form definition, and then executed the
following:

    $values = array('Fax' => '');
    if (!$form->isValid($values)) {
        var_export($form->getMessages());
        exit;
    }
    echo "Validated!";

And got the expected 'Validated!' response. Are you *sure* that the
value passed for your 'Fax' element was empty?

> However, when calling $form->isValid($_POST) on the submitted form, if  
> that field is empty (ie, a "" string), the field is still validated and  
> the form's returned as invalid (due to, in this example, the string  
> length being < 8).
>
> Looking at the code in Zend/Form.php, I see in the function isValid(),  
> the following:

Wrong method. You need to look at Zend_Form_Element::isValid().
Zend_Form::isValid() will see that we have a key for that element and
pass it on the element to validate; it has no logic regarding
required/not-required, as we leave that up to the elements.

The beginning of Zend_Form_Element::isValid() looks like this:

    $this->setValue($value);
    $value = $this->getValue();

    if (empty($value) && !$this->isRequired() && $this->getAllowEmpty()) {
        return true;
    }

The value of the allowEmpty flag is true by default -- meaning that an
empty value should return a true response if the element is not
required.

<snip>

> A few questions:
>
> 1) Is anyone else able to confirm the behaviour I'm reporting?

Clearly, I wasn't.

> 2) Is the solution to do what I've done, or is there a better idea?

I don't think there's any solution necessary -- I think there may be an
issue with the input you're providing, that it's not actually empty.

> 3) Lastly, it's entirely possible that I'm misunderstanding the  
> Zend_Form module in respects of field validation, if so, if you could  
> enlighten me, it would be appreciated.

I think you understand it fine -- I think there's something else at play
here.

-- 
Matthew Weier O'Phinney
Software Architect       | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to