Re: is it possible to include the failed input string in the validation message?
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 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: is it possible to include the failed input string in the validation message?
thats already possible - you don't necessarily need the view for this app_model.php: /** * Overrides the Core invalidate function from the Model class * with the addition to use internationalization (I18n and L10n) * @param string $field Name of the table column * @param mixed $value The message or value which should be returned * @param bool $translate If translation should be done here * 2010-01-22 ms */ function invalidate($field, $value = null, $translate = true) { if (!is_array($this->validationErrors)) { $this->validationErrors = array(); } if (empty($value)) { $value = true; } else { $value = (array )$value; } //TODO: make more generic? if (is_array($value)) { $value[0] = $translate ? __($value[0], true) : $value[0]; if (count($value) > 3) { # string %s %s string, trans1, trans2 $value = sprintf($value[0], $value[1], $value[2], $value[3]); } elseif (count($value) > 2) { # string %s %s string, trans1, trans2 $value = sprintf($value[0], $value[1], $value[2]); } elseif (count($value) > 1) { # string %s string, trans1 $value = sprintf($value[0], $value[1]); } else { $value = $value[0]; } } $this->validationErrors[$field] = $value; } On 31 Dez., 09: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." > > For those who use i28n, error messages need to be in our views, > otherwise they cannot be translated from our models. Would be good on > new versions (2.0?) to have this possibility included as this cleans > up a lot of code and lines in our views. 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: is it possible to include the failed input string in the validation message?
> 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." For those who use i28n, error messages need to be in our views, otherwise they cannot be translated from our models. Would be good on new versions (2.0?) to have this possibility included as this cleans up a lot of code and lines in our views. 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: is it possible to include the failed input string in the validation message?
Bit of a moot point for me... was using Username as an example, but its actually not a username, so the only validation rule I have on the field is isUnique... also not using localization... having said that, would be nice if the model validation rules supported returning the input string in the message On Thu, Dec 30, 2010 at 8:19 AM, john lyles wrote: > >> But wouldn't this error message be displayed when *any* validation error > with the username field occurs? What if I also have validation checks for > whether it's >>empty, whether it's too short, whether it includes invalid > characters? Anyway, wouldn't I want to specify validation errors in the > controller, not in the view? > > Ryan, good point about the error message being displayed for any > error. I hadn't thought that far ahead. The solution would be to > specify the keyname of the validation rule. The example in the manual > not fixed for this situation is: $this->Form->input('Model.field', > array('error' => array('tooShort' => __('This is not long enough', > true) ))); > > 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." > > > On Dec 29, 4:40 pm, Ryan Schmidt wrote: > > On Dec 29, 2010, at 11:24, john lyles wrote: > > > > > If your form looked something like this you would get the result you > > > are looking for: > > > echo $this->Form->input('username', array('error' => > $this->data['User']['username'] . ' is taken')); > > > > And of course if you wanted that to be localizable: > > > > echo $this->Form->input('username', array('error' => sprintf(__('%s is > taken', true), $this->data['User']['username']))); > > > > But wouldn't this error message be displayed when *any* validation error > with the username field occurs? What if I also have validation checks for > whether it's empty, whether it's too short, whether it includes invalid > characters? Anyway, wouldn't I want to specify validation errors in the > controller, not in the view? > > 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.comFor > more options, visit this group at > http://groups.google.com/group/cake-php?hl=en > 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: is it possible to include the failed input string in the validation message?
>> But wouldn't this error message be displayed when *any* validation error >> with the username field occurs? What if I also have validation checks for >> whether it's >>empty, whether it's too short, whether it includes invalid >> characters? Anyway, wouldn't I want to specify validation errors in the >> controller, not in the view? Ryan, good point about the error message being displayed for any error. I hadn't thought that far ahead. The solution would be to specify the keyname of the validation rule. The example in the manual not fixed for this situation is: $this->Form->input('Model.field', array('error' => array('tooShort' => __('This is not long enough', true) ))); 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." On Dec 29, 4:40 pm, Ryan Schmidt wrote: > On Dec 29, 2010, at 11:24, john lyles wrote: > > > If your form looked something like this you would get the result you > > are looking for: > > echo $this->Form->input('username', array('error' => > > $this->data['User']['username'] . ' is taken')); > > And of course if you wanted that to be localizable: > > echo $this->Form->input('username', array('error' => sprintf(__('%s is > taken', true), $this->data['User']['username']))); > > But wouldn't this error message be displayed when *any* validation error with > the username field occurs? What if I also have validation checks for whether > it's empty, whether it's too short, whether it includes invalid characters? > Anyway, wouldn't I want to specify validation errors in the controller, not > in the view? 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: is it possible to include the failed input string in the validation message?
On Dec 29, 2010, at 11:24, john lyles wrote: > If your form looked something like this you would get the result you > are looking for: > echo $this->Form->input('username', array('error' => > $this->data['User']['username'] . ' is taken')); And of course if you wanted that to be localizable: echo $this->Form->input('username', array('error' => sprintf(__('%s is taken', true), $this->data['User']['username']))); But wouldn't this error message be displayed when *any* validation error with the username field occurs? What if I also have validation checks for whether it's empty, whether it's too short, whether it includes invalid characters? Anyway, wouldn't I want to specify validation errors in the controller, not in the view? 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: is it possible to include the failed input string in the validation message?
thanks! On Thu, Dec 30, 2010 at 3:24 AM, john lyles wrote: > You can override the error message using FormHelper :http:// > book.cakephp.org/view/1401/options-error > If your form looked something like this you would get the result you > are looking for: > echo $this->Form->input('username', array('error' => $this- > >data['User']['username'] . ' is taken')); > > On Dec 29, 11:07 am, Greg Skerman wrote: > > Hi, > > > > I'm trying to make my validation messages a little more meaningful. > > > > I would like to include the failed input in the validation message > itself. > > > > So rather than a message like "That username is already taken" > > I would like to show "The username is already taken" > > > > Is this possible? > > 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.comFor > more options, visit this group at > http://groups.google.com/group/cake-php?hl=en > 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: is it possible to include the failed input string in the validation message?
You can override the error message using FormHelper :http:// book.cakephp.org/view/1401/options-error If your form looked something like this you would get the result you are looking for: echo $this->Form->input('username', array('error' => $this- >data['User']['username'] . ' is taken')); On Dec 29, 11:07 am, Greg Skerman wrote: > Hi, > > I'm trying to make my validation messages a little more meaningful. > > I would like to include the failed input in the validation message itself. > > So rather than a message like "That username is already taken" > I would like to show "The username is already taken" > > Is this possible? 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
is it possible to include the failed input string in the validation message?
Hi, I'm trying to make my validation messages a little more meaningful. I would like to include the failed input in the validation message itself. So rather than a message like "That username is already taken" I would like to show "The username is already taken" Is this possible? 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