Usually, it's better to report the validation error, and leave it at that. Altering the data to suite a validation rule could lead to issues down the line.
There are some cases where you might want to manipulate data before validation.
For example, a controller that accepts a phone number, and stores it in a model. The model may validate on one specific format (lets say, no spaces, and an international format), however, the controller may accept a variety of formats (spaces, local formats, parenthesis, dashes, etc). Then you have the issue of automatic transformation, and if that transformation will be reflected to the user if the form should not validated.
For these steps, you may want to transform the value in the controller (though have the transform function on the model), and save the altered value to the request, before calling validate on the specific format.
For this particular example, you could also use a behaviour to automatically convert the phone number to different formats, depending on system requirements (either via a stored field indicating the format on the model, or simply attaching the behaviour to the model from the controller).
But that's a lot of messing around, and far removed your example. For me, the best practice is to keep it simple, and not to alter the data, unless specifically requested by the key user or project requirements.
Regards
Reuben Helms
On Saturday, 23 November 2013 11:15:54 UTC+10, advantage+ wrote:
I know that.
I can simply set the validation to a-z and return the error to the user saying reason why error.
I am asking is it better to simply remove it in beforeValidate() then try to save it or simply skip beforeValidate() and send all the entered data direct to validation.
User enters $data['city'] = N3W Y0rk, now that's not a real city so remove the 3 and the zero in before validate or just bypass it all together.
That's all.
Thanks,
Dave
From: cake...@googlegroups.com [mailto:cake...@googlegroups.com] On Behalf Of Charles Blackwell
Sent: Friday, November 22, 2013 9:31 PM
To: cake...@googlegroups.com
Subject: Re: Best Practise - beforeValidate()
You could use validation and create a custom method to validate to your specification.
http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules
On Friday, 22 November 2013 18:43:34 UTC-5, advantage+ wrote:
What is the best way when saving data for example take city or first name field.
You want it to only be a-z and a space - or.(period)
Should you in beforeValidate do a preg_replace and remove anything that you do not want then save(attempt) it or simply use the validation rules to check and save / return false explaining why?
Just curious as to how people approach this and reasons for doing it either way.
Thanks,
Dave
--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.
--
Like Us on FaceBook
https://www.facebook.com/CakePHP Find us on Twitter
http://twitter.com/CakePHP ---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at
http://groups.google.com/group/cake-php.
For more options, visit
https://groups.google.com/groups/opt_out.