leapfrog
08-17-2004, 05:46 AM
We've been trying to create a custom property editor for a Float value to allow display of nulls as "N/A" and conversion of any non-float to null on set. The code for the custom editor is included below.
After some investigation however it looks as though the getAsText method will never be called when the value of the property is null. This can be seen in the getFieldValue method of org.springframework.validation.BindException. Should the check for value == null be present? It seems to me the check for a binding failure should be enough..
If this can be removed how likely it'll be in the next release? To me it makes sense that if you have a custom property editor you want to handle all values, not just the non-null ones.
BindException.getFieldValue
public Object getFieldValue(String field) {
field = fixedField(field);
FieldError fe = getFieldError(field);
// use rejected value in case of error, current bean property value else
Object value = (fe != null) ? fe.getRejectedValue() : getBeanWrapper().getPropertyValue(field);
// apply custom editor, but not on binding failures like type mismatches
if (value != null && (fe == null || !fe.isBindingFailure())) {
PropertyEditor customEditor = getBeanWrapper().findCustomEditor(null, field);
if (customEditor != null) {
customEditor.setValue(value);
return customEditor.getAsText();
}
}
return value;
}
Custom Editor:
package emt.application;
import java.beans.PropertyEditorSupport;
public class FloatCustomPropertyEditor extends PropertyEditorSupport
{
public String getAsText()
{
System.out.println("getAsText()");
if (getValue() == null)
{
return "N/A";
}
else
{
return getValue().toString();
}
}
public void setAsText(String text) throws IllegalArgumentException
{
System.out.println("setAsText(" + text + ")");
try
{
Float maxRunningSpread = new Float(text);
setValue(maxRunningSpread);
}
catch (NumberFormatException e)
{
setValue(null);
}
}
}
After some investigation however it looks as though the getAsText method will never be called when the value of the property is null. This can be seen in the getFieldValue method of org.springframework.validation.BindException. Should the check for value == null be present? It seems to me the check for a binding failure should be enough..
If this can be removed how likely it'll be in the next release? To me it makes sense that if you have a custom property editor you want to handle all values, not just the non-null ones.
BindException.getFieldValue
public Object getFieldValue(String field) {
field = fixedField(field);
FieldError fe = getFieldError(field);
// use rejected value in case of error, current bean property value else
Object value = (fe != null) ? fe.getRejectedValue() : getBeanWrapper().getPropertyValue(field);
// apply custom editor, but not on binding failures like type mismatches
if (value != null && (fe == null || !fe.isBindingFailure())) {
PropertyEditor customEditor = getBeanWrapper().findCustomEditor(null, field);
if (customEditor != null) {
customEditor.setValue(value);
return customEditor.getAsText();
}
}
return value;
}
Custom Editor:
package emt.application;
import java.beans.PropertyEditorSupport;
public class FloatCustomPropertyEditor extends PropertyEditorSupport
{
public String getAsText()
{
System.out.println("getAsText()");
if (getValue() == null)
{
return "N/A";
}
else
{
return getValue().toString();
}
}
public void setAsText(String text) throws IllegalArgumentException
{
System.out.println("setAsText(" + text + ")");
try
{
Float maxRunningSpread = new Float(text);
setValue(maxRunningSpread);
}
catch (NumberFormatException e)
{
setValue(null);
}
}
}