Sure, I was afraid that I wasn't giving enough information.
OK, hopefully this explains it:
So, in my model I have a ->getInputFilter() method that looks something
like this (other elements removed for space concerns):
public function getInputFilter()
{
if (!$this->inputFilter) {
$parameters = $this->getInputFilterParameters();
if (!isset($parameters['patterns'])) {
throw new \Exception('Patterns key must be defined in input
filter parameters.');
}
$patterns = $parameters['patterns'];
$if = new InputFilter;
$factory = new InputFactory;
//stuff...
$if->add(
$factory->createInput(array(
'name' => 'res',
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array('name' => 'InArray',
'options' => array(
'haystack' => array('Y','N'),
)),
),
)));
$if->add(
$factory->createInput(array(
'name' => 'firstName',
'allow_empty' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array('name' => 'Regex',
'options' => array(
'pattern' => $patterns['name-regex'],
)),
$this->getDukeFirstNameValidator(),
array('name' => 'StringLength',
'options' => array(
'max' => 30
)),
),
)));
$if->add(
$factory->createInput(array(
'name' => 'lastName',
'required' => true,
'allow_empty' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array('name' => 'Regex',
'options' => array(
'pattern' => $patterns['name-regex'],
)),
$this->getDukeLastNameValidator(),
array('name' => 'StringLength',
'options' => array(
'max' => 30
)),
),
)));
$if->add(
$factory->createInput(array(
'name' => 'busName',
'allow_empty' => true,
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array('name' => 'Regex',
'options' => array(
'pattern' => $patterns['busname-regex'],
)),
$this->getDukeBusNameValidator(),
),
)));
// other stuff...
$if->setValidationGroup($this->getValidationSet());
$this->setInputFilter($if);
}
return $this->inputFilter;
}
Pretty standard, right? Here's the challenge: An empty value for
firstName, lastName or busName MAY OR MAY NOT be valid if they're empty.
It depends on the context; specifically, on the value of the 'res' field.
The problem is, that when you specify 'allow_empty' => TRUE, InputFilter
assumes that if it's empty, and 'allow_empty' is TRUE, then it's valid.
None of the other validators are ever called. The problem is, that it may
be valid as an empty field, depending on the value of another field in the
context. In this case, 'firstName' and 'lastName' are required if 'res' is
'Y', but 'busName' is required if 'res' is N. Setting 'allow_empty' =>
FALSE marks the field as invalid, even though it may actually be valid,
depending on the context.
I think the problem is that 'allow_empty' is interpreted as 'if it's empty,
then it's valid, nothing to see here" when (IMHO, anyway) it should be more
like "if it's empty, that's OK, let's run the other validators."
Z
On Fri, Feb 22, 2013 at 4:34 PM, weierophinney [via Zend Framework
Community] <[email protected]> wrote:
> On Fri, Feb 22, 2013 at 2:39 PM, zburnham <[hidden
> email]<http://user/SendEmail.jtp?type=node&node=4659290&i=0>>
> wrote:
> > Maybe I'm missing something, but here's the problem:
> >
> > I have a form in which and empty value in one input may or may not be
> valid
> > depending on context. I have a custom validator to determine that.
> But,
> > when I call ->isValid() on the form, my custom validator may not be
> called
> > depending on the 'required' and 'allow_empty' settings I've given to the
> > Factory class. For example, I could set 'allow_empty' to 'true' in the
> > spec, but here's what happens:
>
> Can you give a concise input (and/or input filter) definition that
> raises the problem for you, the data set you're trying to validate,
> and what you expect? I'm having trouble following from the
> description, and quoting the internals of the code is not really
> getting the idea across. I think with the information I'm requesting,
> I'll have a better idea, and can either help with a solution, or
> indicate whether or not it's expected behavior.
>
>
>
>
> --
> Matthew Weier O'Phinney
> Project Lead | [hidden
> email]<http://user/SendEmail.jtp?type=node&node=4659290&i=1>
> Zend Framework | http://framework.zend.com/
> PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
>
> --
> List: [hidden email]<http://user/SendEmail.jtp?type=node&node=4659290&i=2>
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden
> email]<http://user/SendEmail.jtp?type=node&node=4659290&i=3>
>
>
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://zend-framework-community.634137.n4.nabble.com/Zend-InputFilter-can-t-handle-an-empty-value-based-on-context-tp4659287p4659290.html
> To unsubscribe from Zend\InputFilter can't handle an empty value based on
> context, click
> here<http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4659287&code=emJ1cm5oYW1AZ21haWwuY29tfDQ2NTkyODd8MTYzOTQ1MTg0>
> .
> NAML<http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--
View this message in context:
http://zend-framework-community.634137.n4.nabble.com/Zend-InputFilter-can-t-handle-an-empty-value-based-on-context-tp4659287p4659293.html
Sent from the Zend Framework mailing list archive at Nabble.com.