Back Forum Reply New

Number validation

I'm trying to submit a form which contains a readonly input fields. These fields should not be fill by user(these fields are in form so that when user wants to see the details or wants to edit object, he could see these field values). These fields are basically long and int in pojo. The validation for these fields are failing because these fields return empty string(quot; quot;); I'm getting the follwing error.Code:
[Failed to convert property value of type [java.lang.String] to required type [long] for property 'addedBy'; nested exception is java.lang.NumberFormatException: For input string: quot;quot;]
Here is the codes from my controller and jsp.Code:
lt;labelgt;Modified By: lt;input name=quot;modifiedByquot; readonly=quot;readonlyquot; value=quot;${modifiedBy}quot; id=quot;attachmenttypeModifiedByquot;gt;lt;/labelgt;lt;labelgt;Modified Count: lt;input name=quot;modifiedCountquot; readonly=quot;readonlyquot; value=quot;${modifiedCount}quot; id=quot;attachmentTypeModifiedCountquot;gt;lt;/labelgt;   lt;labelgt;Added By: lt;input name=quot;addedByquot; readonly=quot;readonlyquot; value=quot;${addedBy}quot; id=quot;attachmentTypeAddedByquot;gt;lt;/labelgt;   lt;labelgt;Group ID: lt;input name=quot;groupIdquot; readonly=quot;readonlyquot; value=quot;${groupId}quot; id=quot;attachmentTypeGroupIdquot;gt;lt;/labelgt;

Code:
@Override   protected void initBinder(PortletRequest request, PortletRequestDataBinder binder) throws Exception {       System.out.println(quot;inside initBinder methodquot;);       binder.registerCustomEditor(AttachmentType.class, new AttachmentTypeEditor());       SimpleDateFormat df = new SimpleDateFormat(quot;MM/dd/yyyyquot;);       binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));       binder.registerCustomEditor(Number.class, new CustomNumberEditor(Number.class, true));   }
Thanks,
anita

Found a solution, just have to changed the jsp, instead of readonly fields these fields shold be declared as disabled = quot;truequot;.

Thanks,
anita

Instead of changing jsp, override onBind method and set all empty string values for Numbers to default 0.

OK, I'm back with same problem. This time when I try to add new object the readonly fields are returning empty string(quot;quot;)(I can not change these fields to disabled because then they always returned null and when I try to update these values for object gets overwritten by null

) and I'm getting the following errorCode:
list.get(i).toString() = Field error in object 'attachmenttype' on field 'addedBy': rejected value []; codes [typeMismatch.attachmenttype.addedBy,typeMismatch.addedBy,typeMismatch.long,typeMismatch]; arguments [org..context.support.DefaultMessageSourceResolvable: codes [attachmenttype.addedBy,addedBy]; arguments []; default message [addedBy]]; default message [Failed to convert property value of type [java.lang.String] to required type [long]
I tried extending CustomNumberFormat class by overridding setAsText method to

Code:
@Override   public void setAsText(String text) throws IllegalArgumentException {       if (this.allowEmpty || StringUtils.hasText(quot;quot;)) {System.out.println(quot;inside customIJUSNumberEditorquot;);
// Treat empty String as null value.
setValue(null);
}

else {
// Use default valueOf methods for parsing text.
setValue(NumberUtils.parseNumber(text, this.numberClass));
}
}
   @Override   public String getAsText() {      Object value = getValue();
if (value == null) {
return quot;quot;;
}
return value.toString();   }
I donot see this setAsText method being called ever. Why is that? Since I'm registering this editor in initBinder method.Code:binder.registerCustomEditor(Number.class, new CustomIjusNumberEditor(Number.class, true));
I'm also overwitting onBind method Code:
@Override   protected void onBind(PortletRequest request, Object command, BindException errors) throws Exception {       System.out.println(quot;inside onBind method.-1quot;);       obj = (AttachmentType) command;       if (request.getParameter(quot;addedByquot;).equals(quot;quot;)){obj.setAddedBy(0);       }       if (request.getParameter(quot;modifiedByquot;).equals(quot;quot;)){obj.setModifiedBy(0);       }       if(request.getParameter(quot;modifiedCountquot;).equals(quot;quot;)){obj.setModifiedCount(0);       }       if(request.getParameter(quot;groupIdquot;).equals(quot;quot;)){obj.setGroupId(0);       }      // super.onBind(request, command, errors);   }
Here is th e codes for AttachmentTypeValidation classCode:
public class AttachmentTypeValidator implements Validator {

/** a logger for error reporting */
private static Log _log = LogFactory.getLog(AttachmentTypeValidator.class);
   /*** The default constructor for the AttachmentTypeValidator class.  It initializes* all required fields and sets up a new AttachmentTypeValidator.*/
public AttachmentTypeValidator() {
super();
}

@Override
public boolean supports(Class clazz) {       System.out.println(quot;inside supports method of validation class.quot;);
return AttachmentType.class.isAssignableFrom(clazz);
}

@Override
public void validate(Object target, Errors errors) {       System.out.println(quot;inside validate method of AttachmentTypeValidator - 2.quot;);
AttachmentType attachmentType = (AttachmentType) target;       System.out.println(quot;attachmenttype.addedBy in AttachmentTypeValidation = quot; + attachmentType.getAddedBy());
validateCode(attachmentType, errors);     System.out.println(quot;Found quot; + errors.getErrorCount() + quot; errors with quot; + errors.getFieldErrorCount() + quot; field errorsquot;);  List list = errors.getAllErrors();  for (int i = 0; i lt; list.size(); i++){      System.out.println(quot;list.get(i).toString() = quot; + list.get(i).toString());  }  System.out.println(quot;errors.getFieldError() = quot; + errors.getFieldError());  System.out.println(quot;errors.getFieldError(code) = quot;+errors.getFieldError(quot;codequot;));
//_log.debug(quot;Found quot; + errors.getErrorCount() + quot; errors with quot; + errors.getFieldErrorCount() + quot; field errorsquot;);
}

public void validateCode(AttachmentType attachmentType, Errors errors) {       System.out.println(quot;inside validateCode method.quot;);
ValidationUtils.rejectIfEmpty(errors, quot;codequot;, quot;form.requiredquot;, quot;Required field.quot;);

}
}
I donot know what else I can do to pass these validation. I'll appreciate any help.

Thanks,
anita

Ok, I finally found the solution. I needed a customLongEditor which extend PropertyEditorSuppot and need to overwrite method getAsText() and setAsText(String Text) likeCode:
@Override   public void setAsText(String text) throws IllegalArgumentException {       System.out.println(quot;text = quot; + text);       if (text.equals(quot;quot;)|| text == null) {System.out.println(quot;inside customLongEditorquot;);
// Treat empty String as null value.Long longValue = new Long(0);
setValue(longValue);System.out.println(quot;after setValue method call in setAsText.quot;);
}

else {
// Use default valueOf methods for parsing text.
setValue(Long.parseLong(text));
}
}
   @Override   public String getAsText() {      Long value = (Long)getValue();
if (value == null) {
return quot;quot;;
}
return value.toString();   }
And then I needed to register this editor in my controller likeCode:binder.registerCustomEditor(Long.TYPE, new CustomLongEditor());
Now everything works fine either it is an add or update.

Thanks,
anita
¥
Back Forum Reply New