3

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:

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

3
  • 1
    Could you just change your Validator to ->addValidator('StringLength', false, array(0,500)) ? Commented Aug 30, 2011 at 12:01
  • yes, but how do I add this option anyway? Commented Aug 30, 2011 at 12:25
  • i'm confused. why do you need to use setRequired(true)? if you set it to false and add validators, it does exactly what you want it to do: it works if it's empty or is correctly validated. if it fails any of the validations, form isn't submitted despite setRequired being false. Commented Aug 30, 2011 at 12:36

2 Answers 2

6

->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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the filter. Saved my day after 3 years of your comment.
For everyone how comes to this point: In Zend Framework 3 the class is called ToNull and can be set as a filter
3
$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.

2 Comments

I edited my question. I'm curious to know if it is possible to add this option to my element as I might find better use for it in another situation.
@Fabio I had same situation like @Owen. I had to check if one of three form fields was filled with data. I've wrote a custom validator that checked this, but he was not fired because I needed to add required => true to make validators work. But then field must be not empty before validators are fired. It is situation when setAllowEmpty() comes handy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.