How to access field value in a JSR message with spring boot 2? - spring

I would like to customize my error message in the following way:
Assume following declaration of a class Person:
#Size(min=10, max=250, message="{size.name}")
private String name;
Within the declared error message in ValidationMessages.properties I would like to output the field value as well, i.e. I would like to do something like this:
size.name = The name '${validatedvalue}' is invalid, its size must be between {min} and {max}
Assume the content of the field 'name' is “xyz”. Then the error message should look like this:
The name 'xyz' is invalid, its size must be between 10 and 250
The substitution for min and max works, but filed value i am getting as '' ,how can I do this for the field value?

Related

How to remove label prefix in errors

How do I remove the prefix that is added to errors?
For example, given this error:
activerecord:
errors:
models:
my_model:
my_custom_error: Incorrect value
I'd like the input to show the error as exactly "Incorrect value". But instead SimpleForm always adds the name of the attribute as a prefix. So it shows as "my_custom_error Incorrect value".
How can I remove the prefix for some inputs?
Seems like the prefix is added here: https://github.com/heartcombo/simple_form/blob/main/lib/simple_form/components/errors.rb#L30
The label + message format comes from this I18n:
errors:
format: "%{attribute} %{message}"
So you need to redefine it to:
errors:
format: "%{message}"

can .proto file fields be without values?

I received "missing field number" error. Is it possible to declare fields without value? since i do not have a default value to the fields.
syntax = "proto3";
package tutorial;
message Person {
required string name;
required string email;
}
The field number is not refer to a default value but each field in the message definition has a unique number. These numbers are used to identify your fields in the message binary format, and should not be changed once your message type is in use.
More info here in the doc

<logic:messagesPresent> tag in Struts1 not looping through multiple errors in Action Messages

I have a field in a bean that is failing 2 validations, as such 2 messages are being inserted in ActionMessages with the following command:
validationErrors.add("field1", new ActionMessage("Phone number is greater than 10 digits", false));
validationErrors.add("field1", new ActionMessage("Phone number has invalid characters", false));
Although I see the errors in the ActionMessages object (by setting a breakpoint in the debugger), only the first one gets displayed in my JSP, where I have:
<logic:messagesPresent message="true">
<html:messages id="message" property="field1" message="true">
<logic:present name="message">
<c:out value="${message}"/>
</logic:present>
</html:messages>
</logic:messagesPresent>
Why is only the first message displayed, when <html:messages> should loop through all the messages where the property is "field1"?
I ended up figuring out my issue and it had to do with how I am creating the new ActionMessage.
When you use:
public ActionMessage(<error message>, false)
While it allows you to display a literal value by using the <html:messages> tag in conjunction with either a <bean:write> or <c:out>, it won't iterate over multiple messages for a given property, why I don't know.
I tested and found that if I use a resource bundle and create the ActionMessage with a standard:
public ActionMessage(<key in resource bundle>)
I am able to display multiple messages for a single property.
Unfortunately, because I am using hibernate validator I don't want to use a resource bundle and struts to replace the values (would rather have the hibernate validator annotation replace values) and will likely just display a single message at a time for now.
Struts <logic:messagesPresent> tag checks that the messages exist on the current request.
Messages are found in the request under the key Globals.MESSAGE_KEY. If you use attribute message only messages are checked.
By default the tag will retrieve the request scope bean it will
iterate over from the Globals.ERROR_KEY constant string, but if this
attribute is set to true the request scope bean will be retrieved
from the Globals.MESSAGE_KEY constant string. Also if this is set to
true, any value assigned to the name attribute will be ignored.
The <html:messages> tag is used to display the messages if specified attribute message is true.
Now you have used a property attribute that filters messages for the given property.
Name of the property for which messages should be displayed. If not
specified, all messages (regardless of property) are displayed.
If you have only one message with the field1 property, then only one message will be displayed.
Look here how can you use action messages object
ActionMessages messages = new ActionMessages();
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.common.field1.required");
saveMessages(request, messages); // storing messages as request attributes
Properties file:
error.common.field1.required = Field1 is required.
And display messages
<logic:messagesPresent message="true">
<html:messages id="message" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>
It will loop all massages under the global message key. If you want to use custom key you can use it with the parameter of ActionMessage
messages.add("field1", new ActionMessage("error.common.field1.required");
to retrieve the message
<logic:messagesPresent message="true">
<html:messages id="message" property="field1" message="true">
<bean:write name="message"/><br/>
</html:messages>
</logic:messagesPresent>

how to send arguments on messages.properties when JSR 303 validation

I have a class something like
class Sample{
#Min(1) #Max(20) private int num_seats;
...
}
and messages.properties like
Min.sample.num_seats = the number must be bigger than 1
Question is
how can I set the message dynamically by sending arguments as like "the number must bigger than {MIN_VALUE}"?
how can I share the message? such like "Min.* = the number .... " is possible?
Thank you Ralph it has helped me a lot to find a solution.
I would just add this:
I would use #Range in this case (except if you want two different message for min and max).
In Sample class
#Range(min = 1, max = 20)
private int num_seats;
And in messages.properties file
Range.sample.num_seats=The number must be between {2} and {1}.
Note that the min is argument numbered {2} and max is numbered {1} !
According to SPR-6730 (Juergen Hoellers comment) it should work in this way:
#Min(value="1", message="the number must be higher than {1}")
I have not tested it, but this is the way, I have understand the issue comment.
second question: You can share the text, be putting them in a message properties file.
If you use the same key as the default does, then you override the default message. If you want not to override the default message, then you need an other key, and need to write the key in currly brackets in the message attribute.
message properties file
javax.validation.constraints.Min.message=My mew default message
someOtherKey=Some Other Message
Using the other key:
#Min(value="1", message="{someOtherKey}")

Flask-WTF: How to allow zero upon DataRequired() validation

I have defined a form like this:
class RecordForm(Form):
rating = IntegerField('Rating')
If no value is inserted I get a default message like this:
Not a valid integer value
I would like to have a custom message instead, so I came up with this:
class RecordForm(Form):
rating = IntegerField('Rating',[validators.DataRequired("Helllo???")])
The custom message works now, but I get a side effect. 0 (zero) is no longer accepted as an integer value. What are my options here please?
Use InputRequired instead:
class RecordForm(Form):
rating = IntegerField('Rating',[validators.InputRequired("You got to enter some rating!")])
From the docs:
Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data.
(Emphasis mine)

Resources