Note that there are some explanatory texts on larger screens.

plurals
  1. POPut validation messages into database
    primarykey
    data
    text
    <p>I want to put validation messages used by Bean Validation (JSR 303) ex.:</p> <pre><code>javax.validation.constraints.AssertFalse.message=must be false javax.validation.constraints.AssertTrue.message=must be true ... </code></pre> <p>into a database so that when an administrator adds a new language he can adds the translations for that language.</p> <p>I know how to access a resource bundle mapped from a database but I can't figure out how to extend/customize the bean validation classes so they can access the validation messages from a database...</p> <p>Is it possible to obtain what I want ?</p> <p>Many thanks in advance for driving me into the right direction.</p> <p>HERE THE SOLUTION (I don't know if this is the best solution but it works...):</p> <p>As suggested by @gastaldi I've created an implementation on MessageInterpolator interface:</p> <pre><code>package giates.validation; import com.infomaxgroup.adaecommerce.bundles.DatabaseResourceBundle; import java.util.Locale; import java.util.Map; import javax.validation.MessageInterpolator; public class LocaleMessageInterpolator implements MessageInterpolator { protected final String BRACE_OPEN = "\\{"; protected final String BRACE_CLOSE = "\\}"; @Override public String interpolate(String message, Context context) { return interpolate(message, context, Locale.ITALIAN); } @Override public String interpolate(String message, Context context, Locale locale) { DatabaseResourceBundle bundle = new DatabaseResourceBundle(locale); String messageKey = context.getConstraintDescriptor().getAttributes().get("message").toString(); message = bundle.getString(messageKey.replaceAll(BRACE_OPEN, "").replaceAll(BRACE_CLOSE, "")); Map&lt;String, Object&gt; attributes = context.getConstraintDescriptor().getAttributes(); for (String key : attributes.keySet()) { String value = attributes.get(key).toString(); key = BRACE_OPEN + key + BRACE_CLOSE; message = message.replaceAll(key, value); } return message; } } </code></pre> <p>then I've created META-INF/validation.xml and added the customized messageinterpolator:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;validation-config xmlns="http://jboss.org/xml/ns/javax/validation/configuration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"&gt; &lt;message-interpolator&gt;giates.validation.LocaleMessageInterpolator&lt;/message-interpolator&gt; &lt;/validation-config&gt; </code></pre> <p>as soon as I post a model with failed validations the interpolator calls DatabaseResourceBundle and creates the resulting message...</p> <p>All works great !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload