-- lightflowmark <[email protected]> wrote
(on Wednesday, 11 February 2009, 05:39 AM -0800):
> I have a form in which I have elements I want to conditionally require,
> depending on which submit button is pressed (a 'save for later' button, and
> a 'form complete' button). My first approach was to use setRequired(true)
> on every element, and then conditionally use isValid() or isValidPartial()
> in the controller.
>
> The comment for isValidPartial suggests that it ignores setRequired:
> /**
> * Validate a partial form
> *
> * Does not check for required flags.
> *
> * @param array $data
> * @return boolean
> */
>
> However, this appears to be untrue; isValidPartial fails if setRequired is
> true but the field is empty.
Actually, it needs to be documented better. The idea is that it
validates against *only* those items that are passed to it -- and not
*all* elements in the form (which is the behavior of isValid()). If an
element passed to isValidPartial() has specified that it is required,
then isValidPartial() will merrily check to see if a non-empty value was
provided.
> So I wrote my own validator, ConditionalNotEmpty extends
> Zend_Form_Element_NotEmpty, with an isValid() method which calls
> parent::isValid() conditionally.
>
> However, there is a feature in the Zend_Form_Element->isValid() function
> which prevents running any other validators if the field is empty:
> if ((('' === $value) || (null === $value))
> && !$this->isRequired()
> && $this->getAllowEmpty()
> ) {
> return true;
> }
>
> .. otherwise run other validators ...
>
>
>
>
> So as far as I can see, the only way to do what I'm attempting is to
> conditionally apply setRequired(), to all the elements in the form that need
> it, in the controller. This seems like a horrible kludge to me.
>
> So I guess I've got 2 questions:
> 1) has anyone done what I'm attempting in a clean way?
> 2) are there good reasons why isValidPartial() and
> Zend_Form_Element->isValid() behave the way they do?
>
> Any advice appreciated :-)
--
Matthew Weier O'Phinney
Software Architect | [email protected]
Zend Framework | http://framework.zend.com/