You can override Zend_Form->isValid($data) and setup "isRequired" or not
depending on the button submitted.
Class form extends Zend_Form
{
public function init()
{
/* define elements */
}
public function isValid($data)
{
if(buttonA is submitted) {
$this->elementA->setRequired()
} else {
$thsi->elementB->setRequired()
}
return parent::isValid($data);
}
}
-----Mensaje original-----
De: lightflowmark [mailto:[email protected]]
Enviado el: miƩrcoles, 11 de febrero de 2009 14:39
Para: [email protected]
Asunto: [fw-general] isValidPartial behaviour problem?
Hi,
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.
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 :-)
Mark
--
View this message in context:
http://www.nabble.com/isValidPartial-behaviour-problem--tp21954845p21954845.
html
Sent from the Zend Framework mailing list archive at Nabble.com.