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: [email protected] [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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---