2
votes

Let's start this off with a short code snippet 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 is:

'required' => false,

Which means that the input field is not required and you can submit the form without filling it. However, this also means that any filters and validators won't apply to it if you choose to fill in this field.

Common sense tells me that is an irrational behavior. The way I understand the word 'required' in relation with HTML input fields: an input field that is not required should return NULL if it is not filled in but if user decides to fill it both filters and validators should apply to it. That's what makes sense to me. Do you agree with me or is my common sense not so common?

Now more practical question, because this is how Zend_Form behaves, how can I achieve not required fields which would work as I described above (if nothing is typed in by user it returns NULL otherwise filters and validators normally apply).

2

2 Answers

3
votes

Not really a complete answer to your question, but since comments don't have syntax formatting; here's a filter you can use to make your field values null 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 required part: I'm not sure really. You could try to search the ZF mailinglists on Nabble:

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

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

Edit: Ok, so now I've done some tests myself, cause what you said all sounded counter intuitive to me. Your example works fine with me. This is what I've 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';
}

?>
1
votes

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

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