our app has a phone number field like ###-###-#### x###### on the page
<http://apache-wicket.1842946.n4.nabble.com/file/t375760/phone_number_fields.png>
We define a phone number class as FormComponentPanel in our app like below
public class PhoneFormComponent extends FormComponentPanel<PhoneNumberType>
{
private static final long serialVersionUID = 1L;
private TextField<String> areaField;
private TextField<String> threeField;
private TextField<String> fourField;
private TextField<String> extField;
private String area;
private String three;
private String four;
private String ext;
public PhoneFormComponent(String id, IModel<PhoneNumberType> phone) {
super(id, phone);
init();
}
private void init() {
if (getModelObject() != null) {
if
(StringUtils.isNotBlank(getModelObject().getNumber())) {
String phoneNumber =
getModelObject().getNumber();
area = phoneNumber.substring(0, 3);
three = phoneNumber.substring(4, 7);
four = phoneNumber.substring(8, 12);
}
ext = getModelObject().getExtension();
}
add(areaField = new TextField<String>("area",
new PropertyModel<String>(this, "area")));
areaField.add(new PatternValidator("\\d{3}")).setLabel(
new StringResourceModel("Phone", areaField,
null));
add(threeField = new TextField<String>("three",
new PropertyModel<String>(this, "three")));
threeField.add(new PatternValidator("\\d{3}")).setLabel(
new StringResourceModel("Phone", threeField,
null));
add(fourField = new TextField<String>("four",
new PropertyModel<String>(this, "four")));
fourField.add(new PatternValidator("\\d{4}")).setLabel(
new StringResourceModel("Phone", fourField,
null));
add(extField = new TextField<String>("ext", new
PropertyModel<String>(
this, "ext")));
extField.add(new PatternValidator("\\d{0,6}")).setLabel(
new StringResourceModel("Extension", extField,
null));
areaField.add(new INullAcceptingValidator<String>() {
/** Default serial id */
private static final long serialVersionUID = 1L;
@Override
public void validate(IValidatable<String> validatable) {
if
(StringUtils.isNotBlank(areaField.getConvertedInput()) != StringUtils
.isNotBlank(threeField.getConvertedInput())
&&
StringUtils.isNotBlank(threeField
.getConvertedInput()) != StringUtils
.isNotBlank(fourField.getConvertedInput())) {
PhoneFormComponent.this.error(new
ValidationError()
.addMessageKey("Incomplete"));
}
}
});
}
@Override
protected void convertInput() {
// note that earlier versions did override updateModel, which
looked
// somewhat better, but wasn't useful for when you want to do
// validations with either normal validators or form validators
PhoneNumberType phoneNumber = new PhoneNumberType();
if (StringUtils.isNotBlank(areaField.getConvertedInput())
&&
StringUtils.isNotBlank(threeField.getConvertedInput())
&&
StringUtils.isNotBlank(fourField.getConvertedInput())) {
StringBuffer phoneString = new StringBuffer();
phoneString.append(areaField.getConvertedInput());
phoneString.append("-");
phoneString.append(threeField.getConvertedInput());
phoneString.append("-");
phoneString.append(fourField.getConvertedInput());
phoneNumber.setNumber(phoneString.toString());
phoneNumber.setExtension(extField.getConvertedInput());
setConvertedInput(phoneNumber);
} else if (StringUtils.isNotBlank(areaField.getConvertedInput())
||
StringUtils.isNotBlank(threeField.getConvertedInput())
||
StringUtils.isNotBlank(fourField.getConvertedInput())) {
error(new
ValidationError().addMessageKey("Incomplete"));
setConvertedInput(null);
} else {
setConvertedInput(null);
}
}
.....
bypassing setter and getting
......
the component class uses the PatternValidator to implement validations
then in the panel class, cellPhone code is added as below :
PhoneFormComponent cellPhone = new PhoneFormComponent("cellPh",new
PropertyModel<PhoneNumberType>(targetModel, "cellPh"));
certificationForm.add(cellPhone);
certificationForm.add(new StyledFeedback("cellPhError",new
ContainerFeedbackMessageFilter(cellPhone)));
then, since there are four textfields for phone number component, if a user
puts wrong characters on all of those fields, the feedback messages will
show up multiple times.
<http://apache-wicket.1842946.n4.nabble.com/file/t375760/wrong_validation_message.png>
Is there any way to override ContainerFeedbackMessageFilter so as to
restrict the message to be shown up only once ?
--
Sent from: http://apache-wicket.1842946.n4.nabble.com/Users-forum-f1842947.html
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]