Tuesday, September 16, 2008

Re: Why is the Validation Filters Array Processed Backwards and Returns Conservatively?

Interestingly, there's only one test case in the core that deals with
validation order, and it seems incorrect (or at least misleading).

This is the code:

$TestModel->validate = array(
'title' => array(
'tooShort' => array('rule' => array('minLength', 50)),
'onlyLetters' => array('rule' => '/[a-z]+/i')
),
);
$data = array('TestValidate' => array(
'title' => 'I am a short string'
));
$TestModel->create($data);
$result = $TestModel->validates();
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'tooShort'
);
$this->assertEqual($result, $expected);

Based on this test, it looks like it's assumed that both rules would
fail, but that validation would stop on the 1st rule. But it doesn't,
because although the second rule is called "only letters" the regexp "/
[a-z]+/i" doesn't mean "only letters" it means "must contain at least
one sequence of at least one letter" (rather pointless - anything that
would match/fail this would also match/fail "/[a-z]/i"). "only
letters" would be "/^[a-z]+$/i"

I think I'll create a trac ticket about this; it certainly needs some
clarification.

On Sep 16, 10:50 am, "O.J. Tibi" <ojt...@gmail.com> wrote:
> Thanks for the help guys, I'll make a few test cases on this behavior
> later and see if it's as how grigri described.
>
> On Sep 16, 4:51 pm, RichardAtHome <richardath...@gmail.com> wrote:
>
> > Ah, cool - thanks for clearing that up :)
>
> > On Sep 16, 8:18 am, grigri <j...@hendersonwebdesign.com> wrote:
>
> > > > If that's true, how do you explain the behaviour I mentioned above?
> > > > (unique message doesn't display, required message does)?
>
> > > Because in your example, the validation rule is not handled in the
> > > normal manner. Checking if a field is empty or not is not handled by
> > > the 'allowEmpty' key, and works differently to the normal validation
> > > flow (yeah, it's weird).
>
> > > For example, with these rules:
>
> > > var $validate = array(
> > >   "name"=>array(
> > >     "required"=>array(
> > >       "rule"=>array("minLength, 1),
> > >       "message"=>"is required",
> > >       "required"=>true
> > >     ),
> > >     "need-a"=>array(
> > >       "rule"=>"/a/",
> > >       "message"=>"must contain an a"
> > >     ),
> > >     "need-b"=>array(
> > >       "rule"=>"/b/",
> > >       "message"=>"must contain a b"
> > >     )
> > >   )
> > > );
>
> > > If you give it "xyz", both the 'need-a' and 'need-b' rules will fail,
> > > and the error message will first be set to "must contain an a" then
> > > overwritten with "must contain a b". If, in the "need-a" rule you add
> > > "last" => true, then the validation for that field will stop after
> > > 'need-a' fails and the error message will remain "must contain an a".
>
> > > hope this makes sense
> > > grigri
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: