Hi!
how to translate messages com.haulmont.cuba.gui.components.validators validation package?
Thank You
Hi!
how to translate messages com.haulmont.cuba.gui.components.validators validation package?
Thank You
Hello,
If you use a standard validator from the com.haulmont.cuba.gui.components.validators package (e.g. PatternValidator), you should specify either a message or a message key in your screen XML.
Example with message:
<fieldGroup>
<field id="number">
<validator class="com.haulmont.cuba.gui.components.validators.PatternValidator"
message="Input error!"
pattern="\d{3}"/>
</field>
</fieldGroup>
Example with message key:
<fieldGroup>
<field id="number">
<validator class="com.haulmont.cuba.gui.components.validators.PatternValidator"
message="msg://validationError"
pattern="\d{3}"/>
</field>
</fieldGroup>
In the latter case you should also add the message to the message pack of your screen:
validationError = Input error!
If you don’t specify a message, the hardcoded message in English is shown.
Perhaps this is not clear from the current documentation, we will fix it.
How do you specify the validation message in code?
I have something like
@Inject
private TextField email;
@Override
public void init(Map<String, Object> params) {
email.addValidator(new EmailValidator("how do i pass the error message?"));
}
EmailValidator can take an Element object and a messagesPack string but I’m not exactly clear on how to use those.
EmailValidator is designed to be used declaratively from XML, for example:
<textField id="email">
<validator class="com.haulmont.cuba.gui.components.validators.EmailValidator"
message="Invalid email format"></validator>
</textField>
or with getting the message from the screen’s message pack:
<textField id="email">
<validator class="com.haulmont.cuba.gui.components.validators.EmailValidator"
message="msg://invalidEmail"></validator>
</textField>
If you need to set a validator for a field programmatically, create your own com.haulmont.cuba.gui.components.Field.Validator implementation or extend an existing one from the com.haulmont.cuba.gui.components.validators package.