Friday, December 31, 2010

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

No comments: