RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-07 Thread Mariano Iglesias

That sounds familiar ;)

I wouldn't override validates, as on CakePHP 1.1 the parameter to
validates() has a different meaning to the one you are using, while in 1.2
validates() is supposed to take no parameters ($data parameter is
deprecated.)

I think it'd be better to have it as a separate function (something like
expects())

Also it wouldn't hurt to back the validation rules and restore after
validation. Once again, on AppModel:

function validateFields($fields)
{
if (!isset($this->__backValidateRules))
{
$this->__backValidateRules = array();
}

$ignore = array_diff(array_keys($this->validate), $fields);

foreach($ignore as $field)
{
$this->__backValidateRules[$field] =
$this->validate[$field];
unset($this->validate[$field]);
}
}

function validates($param)
{
$validates = parent::validates($param);

if (isset($this->__backValidateRules))
{
foreach($this->__backValidateRules as $field => $rule)
{
$this->validate[$field] = $rule;
}

$this->__backValidateRules = array();
}

return $validates;
}

So this way after validation is performed rules are restored to their
original state.

PS: Note that I'm not doing anything with validates() parameter, I'm just
passing it through.

-MI

---

Remember, smart coders answer ten questions for every question they ask. 
So be smart, be cool, and share your knowledge. 

BAKE ON!

blog: http://www.MarianoIglesias.com.ar

-Mensaje original-
De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
de scragz
Enviado el: MiƩrcoles, 07 de Marzo de 2007 06:56 a.m.
Para: Cake PHP
Asunto: Re: Different Validations on 1 Table (Model) Based on the Action


This overrides built-in validates and allows you to pass an array of
field names that you want to check. Pretty much like the one above but
I didn't like how that one permanently changed the fields that were
set for model->validate. Bonus would be to make it so you could change
the rules by passing an assoc array of new rules.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Different Validations on 1 Table (Model) Based on the Action

2007-03-07 Thread scragz

This overrides built-in validates and allows you to pass an array of
field names that you want to check. Pretty much like the one above but
I didn't like how that one permanently changed the fields that were
set for model->validate. Bonus would be to make it so you could change
the rules by passing an assoc array of new rules.

function validates($fields = null)
{
if (is_array($fields) && count($fields)) {
$validate = $this->validate;
$ignore = array_diff(array_keys($this->validate), $fields);

foreach ($ignore as $field) {
unset($this->validate[$field]);
}

$return = parent::validates();

$this->validate = $validate;

return $return;
} else {
return parent::validates();
}
}

if ($this->User->validates(array('user', 'password'))) {
// Check login
}

On Mar 6, 3:42 pm, "Mariano Iglesias" <[EMAIL PROTECTED]>
wrote:
> On the User model class add a function like:
>
> function validateFields($fields)
> {
> $ignore = array_diff(array_keys($this->validate), $fields);
>
> foreach($ignore as $field)
> {
> unset($this->validate[$field]);
> }
>
> }
>
> You could also instead move this function to AppModel so you can have it
> available for other models.
>
> Then on your controller:
>
> $this->User->validateFields(array('user', 'password'));
>
> if ($this->User->set($this->data) && $this->User->validates())
> {
> // Check login
>
> }
>
> -MI
>
> ---
>
> Remember, smart coders answer ten questions for every question they ask.
> So be smart, be cool, and share your knowledge.
>
> BAKE ON!
>
> blog:http://www.MarianoIglesias.com.ar
>
> -----Mensaje original-
> De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
> de cecplex
> Enviado el: Martes, 06 de Marzo de 2007 08:15 p.m.
> Para: Cake PHP
> Asunto: Re: Different Validations on 1 Table (Model) Based on the Action
>
> What if I have 15 fields and I only want 2 of them validated. Is there
> no construct available to let me validate a specific set of fields?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Different Validations on 1 Table (Model) Based on the Action

2007-03-07 Thread Dr. Tarique Sani

On 3/7/07, cecplex <[EMAIL PROTECTED]> wrote:
>
> What if I have 15 fields and I only want 2 of them validated. Is there
> no construct available to let me validate a specific set of fields?
>

Did you try out validating anything?

CakePHP will validate only those fields which are provided to it

Cheers
Tarique

-- 
=
PHP for E-Biz: http://sanisoft.com
Cheesecake-Photoblog: http://cheesecake-photoblog.org
=

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread Mariano Iglesias

On the User model class add a function like:

function validateFields($fields)
{
$ignore = array_diff(array_keys($this->validate), $fields);

foreach($ignore as $field)
{
unset($this->validate[$field]);
}
}

You could also instead move this function to AppModel so you can have it
available for other models.

Then on your controller:

$this->User->validateFields(array('user', 'password'));

if ($this->User->set($this->data) && $this->User->validates())
{
// Check login
}

-MI

---

Remember, smart coders answer ten questions for every question they ask. 
So be smart, be cool, and share your knowledge. 

BAKE ON!

blog: http://www.MarianoIglesias.com.ar


-Mensaje original-
De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
de cecplex
Enviado el: Martes, 06 de Marzo de 2007 08:15 p.m.
Para: Cake PHP
Asunto: Re: Different Validations on 1 Table (Model) Based on the Action

What if I have 15 fields and I only want 2 of them validated. Is there
no construct available to let me validate a specific set of fields?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread Rhett Waldock

Assuming that User is your model, you would use $this->User->validates().

Mariano gave a good example in his response:

if ($this->User->set($this->data) && $this->User->validates()) {
// Perform your thing to check user / password
}

You could also do the reverse:

if (!$this->User->validates()) {
$this->Session->setFlash('Please enter a valid username and
password')
$this->redirect(array('action'=>'login'), null, true);
}

rhett waldock
clearsite new media

-Original Message-
From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of cecplex
Sent: Tuesday, March 06, 2007 5:10 PM
To: Cake PHP
Subject: Re: Different Validations on 1 Table (Model) Based on the Action


If that's the case, how would I handle errors in a login form?

On Mar 6, 6:09 pm, "Rhett Waldock" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Can you provide more information about how you are using save() in your
> controllers?
>
> The model-defined validation rules that apply automatically to save()
would
> usually be used in an insert or update operation. Presumably, the signup
> action you describe would be in this category.
>
> However a more abstract action like a login, which presumably does a
select
> operation and compares the fields wouldn't be likely to use save().
>
> rhett waldock
> clearsite new media
>
> -Original Message-
> From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf
>
> Of cecplex
> Sent: Tuesday, March 06, 2007 4:09 PM
> To: Cake PHP
> Subject: Different Validations on 1 Table (Model) Based on the Action
>
> I'm very new to CakePHP, I've only worked with it for a day or so now.
> However, I'm confused as to how validation is handled when you want to
> perform different actions.
>
> For example:
>
> I have a login form that has a username and password required field.
> THEN I have a Signup form that has a username and password and
> firstname, lastname, email and other fields.
>
> When I run the save(); function from my controllers, i want it to
> validate differently based on the action. (Either a signup or a login)
>
> Can someone explain this a bit more?






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread Rhett Waldock

In that case, instead of unsetting each field you don't want validated, just
set validation to a new, abridged array of rules.  Something like:

$this->User->validate = array(
'username' => VALID_NOT_EMPTY, 
'password' =>  VALID_NOT_EMPTY
);

rhett waldock
clearsite new media

-Original Message-
From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of cecplex
Sent: Tuesday, March 06, 2007 5:15 PM
To: Cake PHP
Subject: Re: Different Validations on 1 Table (Model) Based on the Action


What if I have 15 fields and I only want 2 of them validated. Is there
no construct available to let me validate a specific set of fields?

On Mar 6, 6:01 pm, "Mariano Iglesias" <[EMAIL PROTECTED]>
wrote:
> Set up all your validation rules in the model (such as username not empty
> and only letters, email valid, etc. etc.).
>
> On the save() action you validate with the model as usual.
>
> On the login action, instead of calling save() on the model you would call
> validates(), and since you want only to validate a few things (such as
user
> not empty, password not empty) you would unset those validation rules you
> don't need at that time:
>
> unset($this->User->validate['email']);
> unset($this->User->validate['someotherfield']);
>
> // Check if it validates
>
> if ($this->User->set($this->data) && $this->User->validates())
> {
> // Perform your thing to check user / password
>
> }
>
> -MI
>
>
---
>
> Remember, smart coders answer ten questions for every question they ask.
> So be smart, be cool, and share your knowledge.
>
> BAKE ON!
>
> blog:http://www.MarianoIglesias.com.ar
>
> -----Mensaje original-----
> De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
> de cecplex
> Enviado el: Martes, 06 de Marzo de 2007 07:09 p.m.
> Para: Cake PHP
> Asunto: Different Validations on 1 Table (Model) Based on the Action
>
> I have a login form that has a username and password required field.
> THEN I have a Signup form that has a username and password and
> firstname, lastname, email and other fields.
>
> When I run the save(); function from my controllers, i want it to
> validate differently based on the action. (Either a signup or a login)






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread cecplex

If that's the case, how would I handle errors in a login form?

On Mar 6, 6:09 pm, "Rhett Waldock" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Can you provide more information about how you are using save() in your
> controllers?
>
> The model-defined validation rules that apply automatically to save() would
> usually be used in an insert or update operation. Presumably, the signup
> action you describe would be in this category.
>
> However a more abstract action like a login, which presumably does a select
> operation and compares the fields wouldn't be likely to use save().
>
> rhett waldock
> clearsite new media
>
> -Original Message-
> From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
>
> Of cecplex
> Sent: Tuesday, March 06, 2007 4:09 PM
> To: Cake PHP
> Subject: Different Validations on 1 Table (Model) Based on the Action
>
> I'm very new to CakePHP, I've only worked with it for a day or so now.
> However, I'm confused as to how validation is handled when you want to
> perform different actions.
>
> For example:
>
> I have a login form that has a username and password required field.
> THEN I have a Signup form that has a username and password and
> firstname, lastname, email and other fields.
>
> When I run the save(); function from my controllers, i want it to
> validate differently based on the action. (Either a signup or a login)
>
> Can someone explain this a bit more?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread cecplex

What if I have 15 fields and I only want 2 of them validated. Is there
no construct available to let me validate a specific set of fields?

On Mar 6, 6:01 pm, "Mariano Iglesias" <[EMAIL PROTECTED]>
wrote:
> Set up all your validation rules in the model (such as username not empty
> and only letters, email valid, etc. etc.).
>
> On the save() action you validate with the model as usual.
>
> On the login action, instead of calling save() on the model you would call
> validates(), and since you want only to validate a few things (such as user
> not empty, password not empty) you would unset those validation rules you
> don't need at that time:
>
> unset($this->User->validate['email']);
> unset($this->User->validate['someotherfield']);
>
> // Check if it validates
>
> if ($this->User->set($this->data) && $this->User->validates())
> {
> // Perform your thing to check user / password
>
> }
>
> -MI
>
> ---
>
> Remember, smart coders answer ten questions for every question they ask.
> So be smart, be cool, and share your knowledge.
>
> BAKE ON!
>
> blog:http://www.MarianoIglesias.com.ar
>
> -Mensaje original-----
> De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
> de cecplex
> Enviado el: Martes, 06 de Marzo de 2007 07:09 p.m.
> Para: Cake PHP
> Asunto: Different Validations on 1 Table (Model) Based on the Action
>
> I have a login form that has a username and password required field.
> THEN I have a Signup form that has a username and password and
> firstname, lastname, email and other fields.
>
> When I run the save(); function from my controllers, i want it to
> validate differently based on the action. (Either a signup or a login)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread Mariano Iglesias

Set up all your validation rules in the model (such as username not empty
and only letters, email valid, etc. etc.).

On the save() action you validate with the model as usual.

On the login action, instead of calling save() on the model you would call
validates(), and since you want only to validate a few things (such as user
not empty, password not empty) you would unset those validation rules you
don't need at that time:

unset($this->User->validate['email']);
unset($this->User->validate['someotherfield']);

// Check if it validates

if ($this->User->set($this->data) && $this->User->validates())
{
// Perform your thing to check user / password
}

-MI

---

Remember, smart coders answer ten questions for every question they ask. 
So be smart, be cool, and share your knowledge. 

BAKE ON!

blog: http://www.MarianoIglesias.com.ar


-Mensaje original-
De: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] En nombre
de cecplex
Enviado el: Martes, 06 de Marzo de 2007 07:09 p.m.
Para: Cake PHP
Asunto: Different Validations on 1 Table (Model) Based on the Action

I have a login form that has a username and password required field.
THEN I have a Signup form that has a username and password and
firstname, lastname, email and other fields.

When I run the save(); function from my controllers, i want it to
validate differently based on the action. (Either a signup or a login)


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



RE: Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread Rhett Waldock

Hello,

Can you provide more information about how you are using save() in your
controllers?

The model-defined validation rules that apply automatically to save() would
usually be used in an insert or update operation. Presumably, the signup
action you describe would be in this category.

However a more abstract action like a login, which presumably does a select
operation and compares the fields wouldn't be likely to use save().

rhett waldock
clearsite new media

-Original Message-
From: cake-php@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of cecplex
Sent: Tuesday, March 06, 2007 4:09 PM
To: Cake PHP
Subject: Different Validations on 1 Table (Model) Based on the Action


I'm very new to CakePHP, I've only worked with it for a day or so now.
However, I'm confused as to how validation is handled when you want to
perform different actions.

For example:

I have a login form that has a username and password required field.
THEN I have a Signup form that has a username and password and
firstname, lastname, email and other fields.

When I run the save(); function from my controllers, i want it to
validate differently based on the action. (Either a signup or a login)

Can someone explain this a bit more?






--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Different Validations on 1 Table (Model) Based on the Action

2007-03-06 Thread cecplex

I'm very new to CakePHP, I've only worked with it for a day or so now.
However, I'm confused as to how validation is handled when you want to
perform different actions.

For example:

I have a login form that has a username and password required field.
THEN I have a Signup form that has a username and password and
firstname, lastname, email and other fields.

When I run the save(); function from my controllers, i want it to
validate differently based on the action. (Either a signup or a login)

Can someone explain this a bit more?


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---