views:

280

answers:

2

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).

+1  A: 

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';
}

?>
fireeyedboy
Still not a solution because you cannot leave required fields empty, so you will get an exception. And not required fields do not process filters and validators so it won't work there, too.
Richard Knop
I might have found your solution; the allowEmpty meta command, see:http://tinyurl.com/lcyadzStill, are you sure filters aren't being applied either if a field is not required? (I'll make a test myself later on, because I'm curious now) But that sounds pretty darn strange.http://framework.zend.com/manual/en/zend.filter.input.html#zend.filter.input.metacommands.allow-empty
fireeyedboy
So basically, to get what you want: set the field to required, apply your validators and filters, and apply my filter for the null if empty bit, and set the meta command to allowEmpty. That should do the trick (I think)
fireeyedboy
Ok, so now I've tested your example myself. Your example works fine with me. Validators are actually executed, even without the allowEmpty meta command. I think you are using the wrong type of methods to retrieve the sanitized values. Are you sure you are using:$yourForm->getValues() or $yourForm->getValue( 'title' )? PS.: be sure to add the NullIfEmpty filter last, because StringTrim and/or HtmlEntities will convert to string.
fireeyedboy
+1  A: 

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

weierophinney