Re: beforeSave vs beforeValidate

2010-03-13 Thread j0n4s.h4rtm...@googlemail.com
It depends,

it depends on your use case. For instance if one may enter text into a
huge textfield, maybe richtext for instance, its better to sanitize
after READING it from the database, not upon writing (beforesave).

If you want to change things on the fly before validation its no
problem. For instance if you require first and last name, and both do
neither start nor end with whitespace, tab, linebreak but with some
other character and are at least 3 letters long, you can strip those
whitespaces (lets say with trim()) in beforeValidate. The result will
also be that on validation issues the result in the form will be
modified (while beforesave the modification is not visible to the user
at any chance).

There is no general rule where it is best to do do, beforeValidate,
beforeSave, afterRead

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: beforeSave vs beforeValidate

2010-03-12 Thread Dr. Loboto
The flow is beforeValidate -> validate -> beforeSave -> save

First beforeValidate() callback is to check data and make global
decision go further / this data cannot be validated/saved. If answer
is no save() immediately returns false.
If beforeValidate() was successful actual validation occurs. If it
fails save() returns false.
If validation sucseed, beforeSave() is called. Here you can as modify
data if you want some fields go to DB in other format that user
entered as make some additional validation. Again, you can return
false and abort save.
Only if beforeSave() was successful actual save takes place.

So callbacks named exactly as they behave.

On Mar 13, 4:04 am, "Dave"  wrote:
> Most tutorials or tips online refer to before Save () as a good before
> saving to clean data and what not but never have i seen anything on
> beforeValidate as beforeValidate seems to be better to use i would think.
> No?
>
> For example city has validation rules say min 5 characters if someone enters
> "b"  into the city field that meets the minimum 5 character yes then
> you beforeSave clean out the html so you left with "b" as the city name yet
> thats not 5 charaters you were wanting to ensure. so you save "b" as the
> city name
>
> Is it not best to say use beforeValidate to clean up your data make sure it
> meets validation if so then do your beforesave on that data?
>
> I was using beforeSave and noticed i could trick cake into thinking it was
> ok and could actually just put html tags into fields to meet min
> requirements, a-z-0-9 and actually save an entire form record and every
> field inthe db was completely blank because it said it was valid and the
> data that was safe to save was actually nothing yet i set up notempty =>
> true.
>
> Any thoughts?
>
> Dave

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


beforeSave vs beforeValidate

2010-03-12 Thread Dave
Most tutorials or tips online refer to before Save () as a good before
saving to clean data and what not but never have i seen anything on
beforeValidate as beforeValidate seems to be better to use i would think.
No?
 
For example city has validation rules say min 5 characters if someone enters
"b"  into the city field that meets the minimum 5 character yes then
you beforeSave clean out the html so you left with "b" as the city name yet
thats not 5 charaters you were wanting to ensure. so you save "b" as the
city name
 
Is it not best to say use beforeValidate to clean up your data make sure it
meets validation if so then do your beforesave on that data?
 
I was using beforeSave and noticed i could trick cake into thinking it was
ok and could actually just put html tags into fields to meet min
requirements, a-z-0-9 and actually save an entire form record and every
field inthe db was completely blank because it said it was valid and the
data that was safe to save was actually nothing yet i set up notempty =>
true.
 
Any thoughts?
 
Dave

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: beforeSave vs beforeValidate

2009-12-03 Thread naidim
It appears that's it. Thank you.

"beforeSave: Called before each save operation, after validation.
Return a non-true result to halt the save."

On Dec 2, 9:38 am, "j0n4s.h4rtm...@googlemail.com"
 wrote:
> Check what those methods have to return, at least(maybe both) one of
> them (I "think" it was beforeSave) has to return true;
>
> On Dec 1, 10:46 pm, naidim  wrote:
>
> > Sorry, I meant to say the code is in the user MODEL not controller.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: beforeSave vs beforeValidate

2009-12-02 Thread j0n4s.h4rtm...@googlemail.com
Check what those methods have to return, at least(maybe both) one of
them (I "think" it was beforeSave) has to return true;

On Dec 1, 10:46 pm, naidim  wrote:
> Sorry, I meant to say the code is in the user MODEL not controller.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


Re: beforeSave vs beforeValidate

2009-12-01 Thread naidim
Sorry, I meant to say the code is in the user MODEL not controller.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en


beforeSave vs beforeValidate

2009-12-01 Thread naidim
When I use this function as beforeValidate in users_controller it
works. When I change it to beforeSave (and remove the
parent::beforeValidate(); call) it returns to /users/edit (built via
scaffolding) with a flash "Please correct errors below." with no
errors identified in the form. Slug has no validation and state only
requires 2 case insensitive letters.

function beforeValidate() {
parent::beforeValidate(); // Just sanitizes input

// Automatically uppercase state string
if (!empty($this->data)) {
$this->data['User']['state'] = strtoupper($this->data
['User']['state']));
$this->data['User']['slug'] = Inflector::slug(strtolower
($this->data['User']['username']));
}
}

Since it works as beforeValidate I guess I have no need to modify it,
just curious why it doesn't work as beforeSave.

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en