开发者

General error messages for validation rules in Kohana, regardless the field name

I'm using Kohana validation library and I want to set up my own user friendly error messages. The problem is that I genera开发者_运维百科te the form dynamically, so field names are not know during development.

Can a set up error messages for the different validation rules (required, digit, ...) regardless the field name? How?

Note: I'm using Kohana v2.3.4


I know about this problem.

What I ended up doing, is something like this (though this does not know what type of error occurred, it worked for me in my situation).

Assume $errors are the errors returned from the validation library.

My view

<input type="text" id="input-something" name="something" />
<?php if (isset($errors['something']): ?>
<label for="input-something" class="error">Something didn't go right!</label>
<?php endif; ?>

Usually I would echo the $errors['something'] as the text node of the label element, but because they are defined dynamically, I just printed a general purpose error.

It's not a great solution, but you may be able to get away with it.


If someone comes across this using Kohana 3.2, the solution is, that you just add validation.php to messages folder and add your default values, for example:

return array(
    'not_empty' => "Yo dawg, this field can't be empty!",
    '[other rule]' => "[other message]",
);

You can look in to Kohana's source, and just copy the validation.php with default messages to your apps message folder and then just translate all of them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜