Messages
Messages
Trivule comes with default messages, but it offers you the option to rewrite a message, or translate it into given languages.
Trivule does not impose where to display the error message; it offers the possibility to select where to display your message, giving you the opportunity to better design your error without having to perform calculations or injections yourself.
By default, validation messages are displayed in English on an HTML element (such as a div, p, or span) with the attribute @v:feedback="file". Like with rules, you can use either the declarative or imperative mode to modify the messages.
<input
type="file"
@v:rules="required|mimes:text/plain"
@v:msg.required="This field is required"
@v:msg.mimes="Provide a plain text file"
name="file"
/>
<p @v:feedback="file"></p>or
Trivule.message("required", "This field is required");
Trivule.message("mimes", "Provide a plain text file");Trivule displays error messages at the nearest feedback element to the input. If you use a general selector, it will be the first encountered element around the input.
Displaying the message
When Trivule needs to display error messages, it first determines the language in which these messages should be displayed. Here's how it works:
Via HTML
NOTE: I think that we don't need to manage the language with the lib. The use can provide a list of new message that we will use to validate our form.
First, Trivule checks the language set on the lang attribute of the root html element:
<html lang="en"></html>If Trivule supports the language defined in the lang attribute, or if you have added your own languages, the error messages will be displayed in that language.
If, for any reason, you don't want to use the language defined in the lang attribute, you can use the @v:lang attribute on the root element to specify the language in which the messages should be displayed.
<html @v:lang="en"></html>The @v:lang attribute can also be used on a form element to specify the language for that specific form.
<form @v:lang="en"></form>This means that error messages will be specific to each form.
Via JavaScript
Trivule offers another way to specify the language of error messages using JavaScript:
Trivule.local('es');This tells Trivule to display messages in Spanish. This method overrides all other methods of language assignment for displaying error messages.
Modifying an Existing Message
If you want to modify an existing message in Trivule, you can use the rewrite(lang, rule, message) method. For example, if you want to customize the message for the min rule in English, you can do the following:
Trivule.message('min', 'Your custom message for the min rule');Trviules.messages({
required: "This field is required",
mimes: "Provide a plain text file"
});It is important to provide the error messages in the respective order of the rules.
Once you have modified or added messages, Trivule will use the updated versions of the messages for the corresponding rules.
Parameters in Messages
Sometimes, you may want to include specific information such as the field name in error messages. You can do this by using parameters in the messages. For instance, to include the field name in a message, use :field in the message. For example:
The field :field is required.When the error message is generated, :field will be replaced with the actual name of the field.
If you have multiple parameters to pass in a message, you can represent them with :arg1, :arg2, etc. For example, if you have a min rule with a parameter of 9MB (@v:rules="min:92"), you can use the following message:
The size must exceed :arg0.The result would be:
The size must exceed 9MB.If you have multiple possible values for a rule, you can use ...arg to display the list of parameters. For example, if you have an in rule with the parameters active, inactive, etc. (@v:rules="in:active,inactive"), you can use the following message:
The field value must be one of the following: ...arg.The result would be:
The field value must be one of the following: active, inactive, etc.By using these customizations for error messages, you can make Trivule validations more tailored to your specific translation and customization needs.