On Dec 31, 2010, at 02:27, netusco wrote: >> As for the second point about handling this in the controller. It's >> the manual itself that says this should be handled with FormHelper. >> From this page here: http://book.cakephp.org/view/1143/Data-Validation >> , it says: "For more information about how to handle the displaying of >> validation errors, check out the section covering FormHelper."
First of all, when it refers us to the FormHelper for information on how to display validation errors, I read that as saying that the view will be *displaying* the validation errors, yes, but that they will be *generated* elsewhere, e.g. by the model. But the book is contradictory on this point. On the one hand, on the very page you link to above, it shows the validation error message being provided by the model; it's part of the $validate array. This is also how things get set up if you use "cake bake" to bake your model. This makes sense to me and is how I am doing it in my application. If I'm going to be defining validation rules in my model, then it makes sense that the error message for failure to validate that same rule should be defined right there as well. On the other hand, the FormHelper page does show an example of how to "override the model error messages", and the example shows using the __() function to localize the message: http://book.cakephp.org/view/1401/options-error I cannot think of a case where I would want to do such a thing; in my opinion the validation error message is intimately tied to the model, so should be defined there. The book also says "displaying localized content is done using the __() convenience function, or one of the other translation functions all of which are globally available, but probably be best utilized in your views": http://book.cakephp.org/view/1230/Localization-in-CakePHP I am at a loss to explain why the book would suggest that these localization functions are best used from views; in my opinion, they're best used wherever it is that I'm defining text that needs to be localized, and if that happens to be in my model, then that's the best place to use it. Certainly I will be using it in views, for labels and such, but there's no reason why I shouldn't use it any other place in my app that I'm defining localizable text. > For those who use i28n, error messages need to be in our views, > otherwise they cannot be translated from our models. Sure they can. Certainly, if you take a validation rule baked by the default templates, for example: var $validate = array( 'password' => array( 'rule' => array('minLength', '8'), 'message' => 'Your password must be at least 8 characters long.', ), ); ...and you try to localize that: var $validate = array( 'password' => array( 'rule' => array('minLength', '8'), 'message' => __('Your password must be at least 8 characters long.', true), ), ); ...you will quickly discover that this is in fact a PHP syntax error: Parse error: syntax error, unexpected '(', expecting ')' You cannot call a PHP function while initializing a class variable like this. It's just how the PHP language works. However, you can easily change the initialization of the validate array so that it occurs inside a constructor: function __construct() { parent::__construct(); $this->validate = array( 'password' => array( 'rule' => array('minLength', '8'), 'message' => __('Your password must be at least 8 characters long.', true), ), ); } This is what I am using in my app; I've also changed my baking templates to generate this code for me for new models. I can't think of a reason why I wouldn't want to do it this way. Can you? 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 [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
