Validation fails

14 messages Options
Embed this post
Permalink
Todd

Validation fails

Reply Threaded More More options
Print post
Permalink
The validation section of the book seems to fail for most of the cases.
The date always complains that it's an invalid date (it was a baked view).

And the required validation shows an error even if there is content.
Errors similar to those AMC mentions.

Thank you in advance for any clarification you may give us David.
michaelGregoire

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Same for me. I copied the $validate member exactly as you list it on pages 105-106.

First thing I noticed is that the

'content' => array(
  'required' => true
)


portion of the Post model validation seems to throw a php error:

Warning (2): preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/model.php, line 2202]

Next, the date selects data doesn't validate with built-in 'date' validation.

Also... the validation coverage in the book, seems to be too limited for being such an important part of web app. development.

Finally... the ajax junk comes way too soon in the book. I purchased the book to learn about cakephp... not ajax development with cakephp. Ajax should have really been a final chapter... it's just not important enough to come before proper coverage of the framework, when all you're after is to learn the framework.



Todd

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Michael,
As I mentioned on another post, the way to solve the validation in RC2 is to include a rule before it.
So if you do 'rule'=>'alphaNumeric' it will start to validate.

As for the date, I haven't found a solution - if you do, please post it.


michaelGregoire

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Yeah I saw that. Thanks Todd. Yeah, I came to the same conclusion after seeing the error disappear after I commented out the "content" rule. Though 'alphaNumeric' is a whole other can of worms, as it fails when a space is included, so using it is useless. Especially with 'content'. Though that's a cakephp issue and un-related here.

That still doesn't solve the 'date' validation problem.
Todd

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
I decided that using minLength (set to 1) would be the better solution to simulate a required field.
You can skip the required attribute that way.
I'm working on other stuff now, but I'll look through the date stuff more later.

michaelGregoire

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Good suggestion Todd.
davidgolding

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
In reply to this post by Todd
Thanks for posting these errata here. Technically, I suppose they're not errata since the book is consistent with RC1 and these errors appear to be only in RC2. But it's definitely worth mentioning and I thank you for posting so that other readers can benefit. It's true -- the book needs to be as up-to-speed as possible, and posting inconsistencies between the book and the most recent release of Cake is essential for readers to get the full value out of their book.

This ticket addresses the error. Where the book mentions the validation:

'rule'=>'date'

RC2 now has it with more control, like this:

'rule'=>array('date','my')

or

'rule'=>array('date','dMy')

So, for readers using RC2 (which I certainly recommend you do), please replace the validation rule in the book, especially as mentioned in Table 7-7, with the examples above.

Thanks again, guys, for mentioning the problem here. Hope this helps.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]
Todd

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Thanks David,

I understand it's probably not worth your time to go through and update the code until 1.2 becomes official. I assume that should happen in the next couple of months, but who knows.

Thanks for all your hard work with the book.
davidgolding

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Thanks Todd. Can't wait to see what will happen when the full 1.2 release comes out. I'll definitely take a fine-tooth comb to the book when 1.2 is out.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]
StubbTX

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
In reply to this post by davidgolding
David,

Thanks for a great book and helping out on these forums.

I'm trying your solution and I'm still having no luck:

       'date'=>array(
                'rule'=>array('date','dMy'),
                'required'=>true,
                'message'=>'You must supply a valid date - arrgh'
        ),


I'm RC2 and using the baked view that produces the date in the multiple drop-downs.   I've tried changing the format to 'ymd' and others but with no luck.  Any idea what I'm doing wrong?
michaelGregoire

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
I've figured out why the validation isn't working. The problem is that a complete timestamp is sent to the 'date' validation method which isn't built to validate a complete timestamp, only a date.

You can do one of two things, create a custom validation method or use a custom regex.

For the purpose of the book, I went with the custom regex.

Here's what I used for regex
/^((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))$/


So here's what my date validator looks like now:

'date' => array(
      'rule' => array('custom', '/^((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))$/'),
      'required' => true,
      'message' => 'You must supply a valid date'
),

It now validates.

This all is definitely something that the folks at cakephp need to rectify... they provide this handy date widget that includes the time, but their 'date' validator doesn't accept the time.


StubbTX

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Thanks at lot!  I was starting to suspect this but your solution really saved me the time.

I'm trying to adopt cake for the third time and I always get turned off by these type of things not readily under my control.  I supposed the good outweighs the bad but it can be frustrating.

Thanks again.  
michaelGregoire

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
Same here.
davidgolding

Re: Validation fails

Reply Threaded More More options
Print post
Permalink
In reply to this post by michaelGregoire
Thanks for the solution and regex string. Have you submitted a ticket to Cake's trac? If not, I'd like to get this one in there... You're right, Cake should validate its own date-time inputs.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

[ get your copy at: http://www.amazon.com/Beginning-CakePHP-Novice-Professional/dp/1430209771/ ]