开发者

Zend framework - how to allow empty field for form element

I'm using this construction for my element:

$freetext = $this->CreateElement('textarea', 'freetext')
            ->setLabel('Comments')
            ->setAttrib('class','input-textarea')
            ->setOptions(array('rows' => '2', 'cols'=>'30'))
            ->addValidator('StringLength', false, array(0,500))
            ->addFilter('HtmlEntities')
            ->addFilter('StripTags')
            ->setRequired(true);

I want to add an "allowEmpty" to this but can't find the correct syntax. I was hoping for something like:

开发者_JAVA百科
...    ->addValidator('allowEmpty', false, true)

But this does not work.

Edit: I've changed the setRequired() to true - I want to allow empty string as an acceptable value on a require field.

Regardless of usage, how do I add this option to my element?


->setRequired(false);

this is enough if you want to allow an empty string and save an empty string to database.

if you want the field to be optional and keep null value in database if nothing is given, add:

->addFilter(new Zend_Filter_Null)


$freetext = $this->CreateElement('textarea', 'freetext')
            ->addValidator('StringLength', false, array(10,500))
            ->setRequired(false);

Your code should already do that, the setRequired(false) method do what you're asking for, i.e. if the value is not submitted then validators won't be run.

Do you have any issue with the code you've written, some validation error messages or something else?

Update

I've changed the setRequired() to true - I want to allow empty string as an acceptable value on a require field.

What is the semantic in setRequired(true) and allowing the empty string as a valid value? Or better what do you require if the element can be empty?

What you've asked in the edit is a no sense, because if an element is required it MUST have a value different from the empty string. If you need to accept the empty string as a valid value just use setRequired(false). When you get form values with Zend_Form::getValues() or Zend_Form_Element::getValue() you'll obtain the empty string as result.

Anyway here it's the explanation of setRequired and setAllowEmpty from ZF manual:

Using the defaults, validating an Element without passing a value, or passing an empty string for it, skips all validators and validates to TRUE.

  • setAllowEmpty(false) leaving the two other mentioned flags untouched, will validate against the validator chain you defined for this Element, regardless of the value passed to isValid().

  • setRequired(true) leaving the two other mentioned flags untouched, will add a 'NotEmpty' validator on top of the validator chain (if none was already set)), with the $breakChainOnFailure flag set. This behavior lends required flag semantic meaning: if no value is passed, we immediately invalidate the submission and notify the user, and prevent other validators from running on what we already know is invalid data.

If you do not want this behavior, you can turn it off by passing a FALSE value to setAutoInsertNotEmptyValidator($flag); this will prevent isValid() from placing the 'NotEmpty' validator in the validator chain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜