Zend Framework Form Irrational Behavior

Let me start this off with a short piece of code that I will use to demonstrate my opinion:

$title = new Zend_Form_Element_Text('title', array(
    'label' => 'Title',
    'required' => false,
    'filters' => array(
        'StringTrim',
        'HtmlEntities'
    ),
    'validators' => array(
        array('StringLength', false, array(3, 100))
    ),
));

      

This important line:

'required' => false,

      

This means that no input field is required and you can submit the form without filling it out. However, this also means that any filters and validators will not be applied to it if you choose to fill in this field.

Common sense tells me that this is irrational behavior. The way I understand the word "required" in relation to HTML input fields: an input field that is not required should return NULL if it is empty, but if the user chooses to fill it in, filters and validators should be applied to it. This makes sense to me. Do you agree with me or is my general meaning not so widespread?

Now a more practical question, because this is how Zend_Form behaves, how can I achieve unnecessary fields that will work as I described above (if nothing is entered by the user, it returns NULL, otherwise filters and validators are applied).

+2


source to share


2 answers


Not quite a complete answer to your question, but since comments have no syntax formatting; here is a filter you can use to make your field values โ€‹โ€‹empty if empty.

class My_Filter_NullIfEmpty implements Zend_Filter_Interface
{
    public function filter( $value )
    {
          // maybe you need to expand the conditions here
        if( 0 == strlen( $value ) )
        {
            return null;
        }
        return $value;
    }
}

      

About the part required: I'm not really sure. You can try to search ZF mailing lists in Nabble:

http://www.nabble.com/Zend-Framework-Community-f16154.html



Or subscribe to their mailing list and ask them a question. Either through Nabble or directly through the addresses on framework.zend.com: http://tinyurl.com/y4f9lz

Edit: Ok, so now I've done some tests myself, because you said it all sounded counter intuitive to me. Your example works fine with me. This is what I used:

<?php

class Form extends Zend_Form
{
    public function init()
    {

        $title = new Zend_Form_Element_Text('title', array(
                'label' => 'Title',
                'required' => false,
                'filters' => array(
                    'StringTrim',
                    'HtmlEntities',
                    'NullIfEmpty' // be sure this one is available
                ),
                'validators' => array(
                    array('StringLength', false, array(3, 100))
                ),
            ));

        $this->addElement( $title );
    }
}

$form = new Form();

$postValues = array( 'title' => '' ); // or
$postValues = array( 'title' => '        ' ); // or
$postValues = array( 'title' => 'ab' ); // or
$postValues = array( 'title' => ' ab ' ); // or
$postValues = array( 'title' => '<abc>' ); // all work perfectly fine with me

// validate the form (which automatically sets the values in the form object)
if( $form->isValid( $postValues ) )
{
    // retrieve the relevant value
    var_dump( $form->getValue( 'title' ) );
}
else
{
    echo 'form invalid';
}

?>

      

+3


source


Actually, what you describe as your expectations is exactly how Zend_Form works. If you mark an element as optional, the following happens: (a) if no value is passed, it skips validation, but if (b) is passed a value, then it must pass all validators for them to be valid.



BTW, your best bet is to ask ZF questions on the ZF mailing lists: http://framework.zend.com/archives

+1


source







All Articles