Re: Validating only specific fields within a model?

2008-05-20 Thread Matt Huggins

Thanks jonknee, I didn't even consider the 'on' key in my validation
rules.  I'll have to go back to this and see if that solution works
better than the modification I made to app_model.php that I posted
above. :)

On May 19, 10:40 pm, jonknee <[EMAIL PROTECTED]> wrote:
> > Does anyone have any insight on how to go about validating specific
> > fields in a model for special circumstances like this?  Thanks!
>
> We had a discussion that got into this topic recently:
>
> http://groups.google.com/group/cake-php/browse_thread/thread/dc119a9b...
>
> There are a few ways to go, the easiest in your case is probably using
> the 'on' key in your validation rules. You could also fetch the user's
> data, modify it with the new password and then attempt to save the
> whole thing back.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-20 Thread Matt Huggins

Yes, that's basically what is happening.  Even when using saveField
(with the validate parameter set to true), which only saves a single
field of a model, the entire model is validated based upon whatever
field names are specified in the model's $validate variable.

On May 19, 10:39 pm, "David C. Zentgraf" <[EMAIL PROTECTED]> wrote:
> I haven't come across any problems with the validations yet...
> Are you saying that it attempts to validate fields which aren't  
> present in your input data and fails on them?
>
> On 20 May 2008, at 12:34, Matt Huggins wrote:
>
>
>
> > I originally thought the same thing as you David.  It seems
> > instinctive that only the fields being saved should be validated.
> > However, looking into the model.php file of the core shows that ALL
> > fields are in fact being validated, even if only one or several fields
> > are being updated.
>
> > Specifically, the following if-condition stands out within the save()
> > function.  Note how it doesn't validate some fields; instead it
> > validates all fields.  This is confirmed by verifying the
> > functionality of the validates() function as well, which in turn calls
> > invalidFields() without specifying specific fields for validation.
>
> > if ($options['validate'] && !$this->validates()) {
> >    $this->whitelist = $_whitelist;
> >    return false;
> > }
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-19 Thread jonknee

> Does anyone have any insight on how to go about validating specific
> fields in a model for special circumstances like this?  Thanks!

We had a discussion that got into this topic recently:

http://groups.google.com/group/cake-php/browse_thread/thread/dc119a9b9cf4a89/5ef7b179de86f7a1#5ef7b179de86f7a1

There are a few ways to go, the easiest in your case is probably using
the 'on' key in your validation rules. You could also fetch the user's
data, modify it with the new password and then attempt to save the
whole thing back.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-19 Thread David C. Zentgraf

I haven't come across any problems with the validations yet...
Are you saying that it attempts to validate fields which aren't  
present in your input data and fails on them?

On 20 May 2008, at 12:34, Matt Huggins wrote:

>
> I originally thought the same thing as you David.  It seems
> instinctive that only the fields being saved should be validated.
> However, looking into the model.php file of the core shows that ALL
> fields are in fact being validated, even if only one or several fields
> are being updated.
>
> Specifically, the following if-condition stands out within the save()
> function.  Note how it doesn't validate some fields; instead it
> validates all fields.  This is confirmed by verifying the
> functionality of the validates() function as well, which in turn calls
> invalidFields() without specifying specific fields for validation.
>
> if ($options['validate'] && !$this->validates()) {
>   $this->whitelist = $_whitelist;
>   return false;
> }
> >


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-19 Thread Matt Huggins

I originally thought the same thing as you David.  It seems
instinctive that only the fields being saved should be validated.
However, looking into the model.php file of the core shows that ALL
fields are in fact being validated, even if only one or several fields
are being updated.

Specifically, the following if-condition stands out within the save()
function.  Note how it doesn't validate some fields; instead it
validates all fields.  This is confirmed by verifying the
functionality of the validates() function as well, which in turn calls
invalidFields() without specifying specific fields for validation.

if ($options['validate'] && !$this->validates()) {
$this->whitelist = $_whitelist;
return false;
}
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-19 Thread David C. Zentgraf

I'm not quite sure why you're going through all the trouble.
Validation is supposed to work transparently in the background.

If a field in your input data has a corresponding rule in the model,  
it'll be validated, thus always ensuring sane input into your  
database. If you're only sending the new password field, than only  
that's going to be validated.

The catch is setting 'required' => true on a rule, which means that  
the field has to be present every time in your input data.

On 20 May 2008, at 11:46, Matt Huggins wrote:

>
> Alright, well I solved my own problem.  It might not be the most
> efficient method, but given that I didn't find anything within CakePHP
> to handle this, I think it's pretty good.  In case anyone else is
> looking for how to do it, here's what I did.  Within app_model.php,
> simply create the following function:
>
> /**
> * Name: validates()
> * Desc: Allow only specified fields to be validated for a model.
> */
> function validates($fieldList = null) {
>   // If no fields are specified, then simply use the standard
> validation method.
>   if ($fieldList === null) {
>   return parent::validates();
>   } else {
>   // If a single value is passed as a string, convert it to an 
> array.
>   if (!is_array($fieldList)) {
>   $fieldList = array($fieldList);
>   }
>
>   // If the validation array is not set for the model, then there 
> is
> nothing to check.
>   if (!isset($this->validate) || empty($this->validate)) {
>   return true;
>   } else {
>   // Create a placeholder for the original validation 
> array, then
> filter out
>   // any fields that do not require validation.  Perform 
> the
> validation, then
>   // restore the original validation array for the model.
>   $validate = $this->validate;
>   $this->validate = array_intersect_key($this->validate,
> array_flip($fieldList));
>   $result = parent::validates();
>   $this->validate = $validate;
>
>   return $result;
>   }
>   }
> }
>
> Now, when you want to validate specific fields, just pass an array of
> field names to the validates() function.
>
> $this->Member->set($this->data);
> $result = $this->Member->validates(array('password', 'password2'));
> >


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Validating only specific fields within a model?

2008-05-19 Thread Matt Huggins

Alright, well I solved my own problem.  It might not be the most
efficient method, but given that I didn't find anything within CakePHP
to handle this, I think it's pretty good.  In case anyone else is
looking for how to do it, here's what I did.  Within app_model.php,
simply create the following function:

/**
 * Name: validates()
 * Desc: Allow only specified fields to be validated for a model.
 */
function validates($fieldList = null) {
// If no fields are specified, then simply use the standard
validation method.
if ($fieldList === null) {
return parent::validates();
} else {
// If a single value is passed as a string, convert it to an 
array.
if (!is_array($fieldList)) {
$fieldList = array($fieldList);
}

// If the validation array is not set for the model, then there 
is
nothing to check.
if (!isset($this->validate) || empty($this->validate)) {
return true;
} else {
// Create a placeholder for the original validation 
array, then
filter out
// any fields that do not require validation.  Perform 
the
validation, then
// restore the original validation array for the model.
$validate = $this->validate;
$this->validate = array_intersect_key($this->validate,
array_flip($fieldList));
$result = parent::validates();
$this->validate = $validate;

return $result;
}
}
}

Now, when you want to validate specific fields, just pass an array of
field names to the validates() function.

$this->Member->set($this->data);
$result = $this->Member->validates(array('password', 'password2'));
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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
-~--~~~~--~~--~--~---