Wicket DateTextField custom error message - wicket

I am using DateTextField for edit date field,
if user enter invalid date (eg. enter any character inseated of date)
then in on submit form DateTextField internally create invalid date message based on id.
My code is :
form.add(DateTextField.forDatePattern("orderStartDate", new DateModel(new PropertyModel<Date>(this, "defaultModelObject.startDate")), "dd/MM/yyyy"));
Now if a user enters any invalid date then in form submit, DateTextField generates the message.
"The value of 'orderStartDate' is not a valid Date."
In message 'orderStartDate' is id which i set on DateTextField;
Now I want to create custom message for this error inseated of above message;

The default value comes from IConverter resource bundle key: https://github.com/apache/wicket/blob/77b4df63f44d00e9350068686e4b091f730f167f/wicket-core/src/main/java/org/apache/wicket/Application.properties#L16
You need to provide a more specific one in YourApplication.properties to override it, e.g.:
orderStartDate.IConverter=Please provide a date with pattern dd/MM/yyyy.

The Validators usually report their class into the Error object. When Wicket generates the FeedbackMessage, it looks it up in the StringResourceModel. So if you use .properties or .properties.xml files for wording, you might want to add the FeedbackMessage using the key of IConverter (which reported the error)
<entry key="IConverter.Date">Please use the format dd/MM/yyyy!</entry>
Same story for required FormComponents
<entry key="Required">Please provide a value!</entry>
If you some day write your own validator, give it a key and optional parameters.
private void error(IValidatable<String> validatable) {
ValidationError error = new ValidationError();
error.addKey("id.exists");
error.setVariable("id", valueThatIsNotValid); // variable held in class
ekpCmp.error(error);
}
and define the StringResource
<entry key="id.exists">The value ${id} already exists.</entry>

Related

How to reset a date attribute in an object in Maximo REST API

I want to change the deligatefromdate/deligatetodate in the person object in IBM's Maximo REST API.
If I want to set a date I use this request:
POST maximo/rest/mbo/person/12345/?_format=json&delegatefromdate=2020-12-02
My person object then is returned and the value in delegatefromdate is:
"DELEGATEFROMDATE": {
"content": "2020-12-02T00:00:00+01:00"
}
But now I want to remove the date, I dont want a delegatetodate/delegatefromdate.
I have tried:
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=
Value is not changed
POST maximo/rest/mbo/person/63006/?_format=json
Value is not changed
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=null
Error: Error 400: BMXAA4143E - The date format is not valid. Use the date format defined by your locale, or use the calendar control to enter a date.
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=NULL
Error: Error 400: BMXAA4143E - The date format is not valid. Use the date format defined by your locale, or use the calendar control to enter a date.
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=~null~
Error: Error 400: BMXAA4143E - The date format is not valid. Use the date format defined by your locale, or use the calendar control to enter a date.
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=~NULL~
Error: Error 400: BMXAA4143E - The date format is not valid. Use the date format defined by your locale, or use the calendar control to enter a date.
POST maximo/rest/mbo/person/63006/?_format=json&delegatefromdate=0
Date is set to: "2000-12-01T00:00:00+01:00"
I have tried with and without &addchange=
So my question is, how do I clear this date?
In the GUI I just erase the value and save and it is gone.
IBM has a record of this problem as APAR IV93341: UNABLE TO UPDATE FIELD TO NULL THROUGH THE MAXIMO REST API. Someone found it on Maximo 7.5.0.7, and the APAR fix was included in 7.6.0.8.

Play - Custom validations with custom error message

I am using the Play framework in Scala to develop a small blog website. I currently have a form (successfully) set up for an easy registration of users. This login page just accepts a username (ie. no password yet), verifies that is of the appropriate length and doesn't exist yet, and adds this user to the database (currently still in memory). Length can be verified using just the basic form functionality, however, the uniqueness of this username required me to use custom validations.
Now, this all works, except for the custom error message. When a normal form requirement is not fulfilled, an error message is returned and displayed in the view (eg. "The minimum length is: 5"). I want to display a similar message when the name is not unique. In the first link I provided there is an example of custom validations which seems to have an argument that represents such custom error message for validations you write of your own. However, this does not display in the view, while the others do.
Current validation code:
private val myForm: Form[Account] =
Form(mapping("name" -> text(3, 24))(Account.apply)(Account.unapply).verifying(
"Account is not in the DB.",
fields =>
fields match {
case data: Account => accountExists(data.name).isDefined
}
)
)
Anyone has any ideas?

Spring binding - dealing with numeric inputs?

I'm dealing with a Webflow application where I may have to submit the current form in order to delete a child record (complex workflow, sorry).
The problem is that if the user enters junk data into the form and then presses the "delete" button, the binding and/or validation will fail and the form will never be submitted.
Hence, if they enter junk data, they cannot delete the record.
What is the preferred way of dealing with users entering "junk" data in web forms, particularly entering non-numeric data in numeric fields? I have a Spring form backing object that looks like this:
public class MyFormInfo implements Serializable {
private String myName;
private Integer myNumber;
}
If the user enters junk in the myName field I can ignore that during validation. However if they enter junk in the myNumber field and the binding fails, I have no good way to trap that and I can't submit the form.
Anybody have a good way to deal with this?
Have a look at this answer as well, but in summary there is no good way to add an error message in the case of type mismatch at conversion time.
The mechanisms available (property editors, converters, bean validation) are not meant to deal with a type mismatch.
The best solution is probably to do the validation on the client side via Javascript via some field mask that only accepts numerics. Then on the server a type mismatch would only occur in case of a bug, so the unhandled error could be acceptable.
For doing this on the server, it's possible to add a String property to the DTO, and apply a bean validation:
#Pattern(regexp = "{A-Za-z0-9}*")
String numericField;
Then via bean validation is possible to add error messages to the page, see this example.
If you want to avoid decimal number input in Integer fields, you can do it like this:
In the HTML-form you can do:
<div class="form-outline">
<input type="number" min=1 max=100 required/>
</div>
In the Java-Form you can do:
#NotNull
#Max(value = 100)
#Min(value = 1)
#NumberFormat
private Integer countOfRooms = null;

GWT validation framework internationalization for messages in annotations

After having read this page I found a way of internationalizing validation messages. It is done by placing translations of each error type into ValidationMessages.properties files.
javax.validation.constraints.Size.message=The property must be between {min} and {max}
javax.validation.constraints.NotNull.message=The property must not be null
It is error type-specific and it's not what I need in my app. I need a unique message for each of my fields in validated beans. With no internationalization it can be done using annotations.
#Pattern(regexp = UiLogicUtils.EMAIL_REGEX, message = "Email is invalid.")
private String requesterEmail;
So how can I make translations of the "Email is invalid" text just for this specific field?
I don't know if this is the answer, because I have not used #Pattern, but in the docs, it says that the message field of the #Pattern is not a text, but a key into the messages file. Look here:
http://docs.oracle.com/javaee/6/api/javax/validation/constraints/Pattern.html#message()
By default message="{javax.validation.constraints.Pattern.message}" which looks like a valid key in ValidationMessages.properties file. I suppose you only have to specify a custom key and use it.

Zend Validate, Display one message per validator

I am validating an email address using zend_validate_email.
For example, for email address aa#aa it throws several error messages including very technical describing that DNS mismatch (:S).
I am trying to make it display only 1 message that I want it to (for example: "Please enter a valid email").
Is there any way of doing it elegantly, apart from creating a subclass and overriding the isValid method, clearing out the array of error messages?
Thanks!
$validator = new Zend_Validate_EmailAddress();
// sets the message for all error types
$validator->setMessage('Please enter a valid email');
// sets the message for the INVALID_SEGMENT error
$validator->setMessage('Something with the part after the # is wrong', Zend_Validate_EmailAddress::INVALID_SEGMENT);
For a full list of errors and message templates see the Zend_Validate_EmailAddress class