Google Groups Home
Help | Sign in
usage of the new validation class in 1.2.x.x.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
gabordemeter  
View profile
 More options Dec 31 2006, 3:12 am
From: "gabordemeter" <gabordeme...@myway.com>
Date: Sun, 31 Dec 2006 00:12:24 -0800
Local: Sun, Dec 31 2006 3:12 am
Subject: usage of the new validation class in 1.2.x.x.
Hi,

Just installed cake 1.2.0.4206 two days ago. So far have not
encountered any problems with it.

I did however noticed the new validation class with a much wider range
of validation methods than before and would like to start using it.
However, since 1.2.x.x. documentation has not been released yet, I was
wondering if anyone knows how to properly use the new validation
methods.

Basically, if I want to check in my User model if a certain submitted
field (say 'username') is alphanumeric, what is the syntax for doing
this check in the User model?

Should I write something like $validate = array('username' =>
'alphanumeric');    ? Or is there a new syntax for doing this check in
cake 1.2.x.x.?

Thx and have a Happy New Year!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
nate  
View profile
(3 users)  More options Jan 1 2007, 12:35 pm
From: "nate" <nate.ab...@gmail.com>
Date: Mon, 01 Jan 2007 09:35:42 -0800
Local: Mon, Jan 1 2007 12:35 pm
Subject: Re: usage of the new validation class in 1.2.x.x.
Right now, the only way to use the new Validation class is to write
your rules like this:

var $validate = array('username' => array('rule' => 'alphaNumeric'));

You can specify other rules as follows:

'rule' => array('between', $min, $max)

'rule' => array('blank')

'rule' => array('cc', $type, $deep)
Where $type can be 'fast' (a basic check that covers all card types) or
'all' (check all cards) or an array of one or more of the following:
'amex', 'bankcard', 'diners', disc', 'electron', 'enroute', 'jcb',
'maestro', 'mc', 'solo', 'switch', 'visa' or 'voyager'.

'rule' => array('comparison', $operator, $compare)
$operator may be one of the following: 'isgreater', 'isless',
'greaterorequal', 'lessorequal', 'equalto', 'notequal', or, their
symbol equivalents: '>', '<', '>=', '<=', '==', '!='.

'rule' => array('custom', $regex)
Where $regex is a custom regular expression.

'rule' => array('date', $format)
Where $format is one of the following: 'dmy', 'mdy', 'ymd', 'dMy',
'Mdy', 'My', 'my'.  $format can also be an array containing mulitple
values.

'rule' => array('decimal', $precision)
Checks that the input is a decimal number with $precision places after
the decimal point.

'rule' => array('email', $deep)
If $deep is true, the email address will be checked for a valid host
name.

'rule' => array('minLength', $min)

'rule' => array('maxLength', $max)

'rule' => array('numeric')

'rule' => array('postal')

'rule' => array('ssn')

'rule' => array('url')

Those are the rules in a nutshell, although more will be added shortly.
 Most of them should be pretty self-explanatory.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gabordemeter  
View profile
 More options Jan 1 2007, 1:08 pm
From: "gabordemeter" <gabordeme...@myway.com>
Date: Mon, 01 Jan 2007 18:08:37 -0000
Local: Mon, Jan 1 2007 1:08 pm
Subject: Re: usage of the new validation class in 1.2.x.x.
Thanks Nate!

The previous days I looked a bit into this and came to the conclusion
that I would like to be able to display several errors for each form
field (i.e. if the username is both using non-alphanumeric characters
as well as having more than the allowed limit of chars I would like to
see both error images displayed below my message).

The solution I found was as follows:

1) In app_model.php I now have:

class AppModel extends Model{

        var $validate;

        function __construct()
        {
                $this->validate = new Validation();
                parent::__construct();
        }

}

2) In my model I have something like:

        function beforeValidate()
        {
                if (!$this->validate->alphaNumeric($this->data['User']['username']))
$this->invalidate('username_alphanumeric');
                if
(!$this->validate->someOtherFunction($this->data['User']['username']))
$this->invalidate('username_someOtherFunction');
                if ($this->validate->blank($this->data['User']['password']))
$this->invalidate('password_blank');
        }

3) In  my view I have:

        <?php echo $html->input('User/username', array('class'=>'signup')); ?>
        <?php echo $html->tagErrorMsg('User/username_alphanumeric', 'Username
must be alphanumeric.') ?>
        <?php echo $html->tagErrorMsg('User/username_blank', 'Username cannot
be someOtherFunction.') ?>

        <?php echo $html->password('User/password', array('class'=>'signup'));
?>
        <?php echo $html->tagErrorMsg('User/password_blank', 'Please choose a
password.') ?>

Btw, I just read the excellent article on your blog explaining the new
automagic in the form helper and will convert the above inputs to it
soon.

Ok, so is the above the right way to do what I'm trying to do? Or is
there a simpler or better way to achieve it?

Also, if I do it like the above, the entire validation happens in
beforeValidate() and I will therefore not have anything in $validate. I
would however want to do the following:

If my beforeValidate logic will invalidate one or more fields (so there
exists at least one validation error), besides displaying the
respective error messages for each field like I did above, I would also
like to have a generic message at the top of my form saying something
like "there were errors in you form...please scroll down to see
them...blah blah".

How should I do this? I tried:

        function beforeValidate()
        {
                if (!$this->validate->alphaNumeric($this->data['User']['username']))
$this->invalidate('username_alphanumeric');
                if
(!$this->validate->someOtherFunction($this->data['User']['username']))
$this->invalidate('username_someOtherFunction');
                if ($this->validate->blank($this->data['User']['password']))
$this->invalidate('password_blank');

                $abc = "there were errors in you form...please scroll
down to see them...blah blah";
                if ($this->validationErrors) $this->set('message', $abc);
        }

The above did not work. Not sure if i can $this->set() from inside a
model. If not, how can I do it?

Thanks again for your previous reply!

On Jan 1, 12:35 pm, "nate" <nate.ab...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
snowdog  
View profile
 More options Feb 7 2007, 12:33 pm
From: "snowdog" <zwo...@gmail.com>
Date: Wed, 07 Feb 2007 17:33:26 -0000
Local: Wed, Feb 7 2007 12:33 pm
Subject: Re: usage of the new validation class in 1.2.x.x.
That is great help! Thanks.
I have one more problem - how to check if the field is NOT BLANK? I
tried for same time an got nothing.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
snowdog  
View profile
 More options Feb 7 2007, 1:19 pm
From: "snowdog" <zwo...@gmail.com>
Date: Wed, 07 Feb 2007 18:19:55 -0000
Local: Wed, Feb 7 2007 1:19 pm
Subject: Re: usage of the new validation class in 1.2.x.x.
Ok, I found the solution for my question - using minLength rule...

On 7 Lut, 18:33, "snowdog" <zwo...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google