Additional Validation problems in book

8 messages Options
Embed this post
Permalink
npm

Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
Chapter 7: Ok, so I did due diligence and searched the forum for fixes, found the ones for date but 'content' was still throwing errors.  I ended up with this code that seems to work:

    var $validate = array(
        'name'=>array(
            'alphaNumeric'=>array(
                'rule'=>'alphaNumeric',
                'required'=>true,
                'message'=>'The title must be alphanumeric'
            ),
            'maxLength'=>array(
            'rule'=>array('maxLength',80),
            'message'=>'The title must not exceed 80 characters'
            )
        ),
        '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'
        ),
        'content'=>array(
            'rule' => array(
                'required'=>true
            ),
        )
    );

I hope it saves someone else the time spent digging for an answer. :)
npm

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
Oh yes, another thing is the book says to use the <?=$form->error('name'); ?> to display the errors.  It appears you don't need to any longer, they just magically appear.
davidgolding

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
Validation errors to automagically appear through the $form->input() fields, but $form->error() is still a valid function. First, you ought to know it's there if you need to manage the placement of error messages, say for apps where you need the individual field errors to appear apart from the fields themselves. Also, if you want to manage how multiple error messages appear, for multiple validations, or to be more specific in validation output in the view, $form->error() is going to be one way to go.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

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

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
In reply to this post by npm
Thanks for your diligence :)

But the regex string you've provided shouldn't be necessary. If you're not satisfied with the 'date' parameter, then I'd submit a Trac ticket or discuss it in the IRC chatroom.
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

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

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
Does the date validation now match as it comes from the database?  From other errata reports I'd read (from a few months back) they'd had to use the regex to get it to validate properly.

I'll give it a shot.
npm

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
Nope, it doesn't validate properly if you say:

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

To get the example code to work right now I seem to have to use the regex.
npm

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
In reply to this post by davidgolding
davidgolding wrote:
Validation errors to automagically appear through the $form->input() fields, but $form->error() is still a valid function. First, you ought to know it's there if you need to manage the placement of error messages, say for apps where you need the individual field errors to appear apart from the fields themselves. Also, if you want to manage how multiple error messages appear, for multiple validations, or to be more specific in validation output in the view, $form->error() is going to be one way to go.
But if you do include $form->error() you get the error message twice.  What would you do to hush up the $form->input() line from printing the error message?
davidgolding

Re: Additional Validation problems in book

Reply Threaded More More options
Print post
Permalink
$form->input('field.name',array('error'=>false));
--Dave

Author, "Beginning CakePHP: From Novice to Professional"

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