mmutilva
mmutilva

Reputation: 18994

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 generate 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

Upvotes: 1

Views: 1289

Answers (2)

Mārtiņš Briedis
Mārtiņš Briedis

Reputation: 17762

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.

Upvotes: 0

alex
alex

Reputation: 490233

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.

Upvotes: 1

Related Questions