> Hmmm... I tried the above form definition, and then executed the
> following:
> $values = array('Fax' => '');
> if (!$form->isValid($values)) {
> var_export($form->getMessages());
> exit;
> }
> echo "Validated!";
> And got the expected 'Validated!' response. Are you *sure* that the
> value passed for your 'Fax' element was empty?
It was an empty string, var_dump($_POST) printed:
array
'Fax' => string '' (length=0)
'submit' => string 'Submit Query' (length=12)
My test case, which can be seen at http://doreen.downagain.com/test/form
consists of
the controller:
class TestController extends Access2phpBaseController {
public function formAction() {
$form = new forms_TestForm();
$this->view->form = $form;
}
public function processAction() {
$form = new forms_TestForm();
$formdata = $this->_request->getPost();
if(!$form->isValid($formdata)) {
var_dump($formdata);
var_export($form->getMessages());
exit;
} else {
echo "Validated!";
exit;
}
$this->view->form = $form;
$this->_helper->viewRenderer->setScriptAction('form');
}
}
and the form:
class forms_TestForm extends Zend_Form
{
public function __construct($options = null) {
$loader = Zend_Registry::get('loader');
$loader->load('FormUtils');
parent::__construct(array(
'action' => '/test/process',
'method' => 'post',
'elements' => array(
'Fax' => array('text', array(
'validators' => array(
array('stringLength', false,
array(8,32)),
),
'label' => 'Fax',
)),
'submit' => 'submit',
),
));
}
}
(the view script is just an echo $this->form)
If you go to the above url, and just click submit, you'll see (hopefully!)
the following:
array
'Fax' => string '' (length=0)
'submit' => string 'Submit Query' (length=12)
array ( 'Fax' => array ( 'stringLengthTooShort' => '\'\' is less than 8
characters long', ), )
So the form doesn't validate. Can you confirm if this is the case for you
too?
>> Looking at the code in Zend/Form.php, I see in the function isValid(),
>> the following:
>Wrong method. You need to look at Zend_Form_Element::isValid().
>Zend_Form::isValid() will see that we have a key for that element and
>pass it on the element to validate; it has no logic regarding
>required/not-required, as we leave that up to the elements.
The Zend_Form::isValid I'm looking at does have logic regarding
required/not-required:
public function isValid(array $data)
{
$valid = true;
foreach ($this->getElements() as $key => $element) {
if (!isset($data[$key])) {
if ($element->getRequired()) { <------ REQUIRED /
NOT-REQUIRED LOGIC HERE
$valid = $element->isValid(null, $data) && $valid;
}
} else {
$valid = $element->isValid($data[$key], $data) && $valid;
}
}
So while I see what you're saying regarding using Zend_Form_Element, under
the behaviour I'm seeing, Zend_Form_Element::isValid() isn't playing ball.
>> A few questions:
[...]
>> 2) Is the solution to do what I've done, or is there a better idea?
>I don't think there's any solution necessary -- I think there may be an
>issue with the input you're providing, that it's not actually empty.
If you can review the above and get back to me, it would be appreciated.
I'd like to do things the Right Way, and having patches on my version of the
framework isn't an ideal situation at all.
Thanks in advance,
Jonathan.
--
View this message in context:
http://www.nabble.com/Zend_Form%2C-validation-occurring-on-non-required-fields-tp16580841p16593816.html
Sent from the Zend Framework mailing list archive at Nabble.com.