|
From: <jue...@we...> - 2004-10-05 07:15:41
|
Dmitriy,
=20
I wouldn't build generic string trimming capability into DataBinder =
itself: We already have StringTrimmerEditor for this, which can be =
registered as custom editor.
=20
That said, I do see your use case for required fields. What about =
redefining required fields to accept empty strings? I.e. to change the =
required check as follows:
=20
if (pv =3D=3D null || pv.getValue() =3D=3D null ||
(pv.getValue() instanceof String && !StringUtils.hasText((String) =
pv.getValue()))) {
I consider redefining the required check like this, without an extra =
config property. IMO, this is sensible for a required check anyway, from =
a semantic point of view...
=20
Juergen
=20
________________________________
Von: spr...@li... im Auftrag =
von Dmitriy Kopylenko
Gesendet: Di 05.10.2004 05:00
An: spr...@li...
Betreff: [Springframework-developer] Small change in DataBinder's bind() =
behavior
Juergen,
I was browsing the source for DataBinder and co. and playing with =
"required fields" functionality and DataBinder's tests and I've noticed =
the following - it's not possible to configure the DataBinder to trim =
the unwanted spaces from the required fields. Consider the following =
test case:
TestBean alef =3D new TestBean();
DataBinder binder =3D new DataBinder(alef, "person");
binder.setRequiredFields(new String[]{"name", "touchy", "date"});
MutablePropertyValues pvs =3D new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("touchy", " "));
pvs.addPropertyValue(new PropertyValue("name", null));
binder.bind(pvs);
BindException ex =3D binder.getErrors();
assertEquals("Wrong amount of errors", 3, ex.getErrorCount());
This will fail because it will not recognize required "touchy" field as =
not being present because it contains spaces. It would really be nice to =
control trimming of such "accidentally set spaces" for required fields =
so the bind() method could then treat them as "missing required field".
I've added the flag to DataBinder ("trimSpacesInStringFields") and small =
piece of logic in DataBinder.bind(PropertyValues):
// check for missing fields
if (this.requiredFields !=3D null) {
for (int i =3D 0; i < this.requiredFields.length; i++) {
PropertyValue pv =3D =
pvs.getPropertyValue(this.requiredFields[i]);
=20
//This is what I added
if(this.trimSpacesInStringFields && pv !=3D null && =
pv.getValue() !=3D null && pv.getValue() instanceof String) {
pv =3D new PropertyValue(pv.getName(), =
((String)pv.getValue()).trim());
}
=20
if (pv =3D=3D null || "".equals(pv.getValue()) || =
pv.getValue() =3D=3D null) {
// create field error with code "required"
String field =3D this.requiredFields[i];
this.errors.addError(
new FieldError(this.errors.getObjectName(), =
field, "", true,
=
this.errors.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, field),
getArgumentsForBindingError(field), "Field =
'" + field + "' is required"));
}
}
}
...// the rest is omitted
Then by modifying test case a little, it works perfectly:
TestBean alef =3D new TestBean();
DataBinder binder =3D new DataBinder(alef, "person");
binder.setRequiredFields(new String[]{"name", "touchy", =
"date"});
=20
//Set the new flag
binder.setTrimSpacesInStringFields(true);
MutablePropertyValues pvs =3D new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("touchy", " "));
pvs.addPropertyValue(new PropertyValue("name", null));
binder.bind(pvs);
BindException ex =3D binder.getErrors();
System.out.println(alef.getTouchy().length());
System.out.println(ex);
assertEquals("Wrong amount of errors", 3, ex.getErrorCount());
I think it would be a very handy feature (for us at least), unless I =
missed something fundamental.
Thoughts?
Regards,
Dmitriy.
|