PDA

View Full Version : Numeric Validation


rajyalakshmi.suman
Sep 12th, 2007, 03:36 AM
Hi

I have return java class for numeric check.
How can i bind it to a textfield string using spring+portlet+hibernate.

Eg:Date field


protected void initBinder(PortletRequest request,PortletRequestDataBinder binder)
throws Exception {
logger.info("inside initBinder of AddController");

SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");

binder.registerCustomEditor(
Date.class,
null,
new CustomDateEditor(dateFormat, true));

Thank u
Lakshmi

johnalewis
Sep 12th, 2007, 12:21 PM
I'm afraid I don't fully understand your question, so please feel free to be more specific.

Numeric values will get checked at binding as they are converted from the string value in the input field parameter to the numeric value in your command class -- if the number cannot be converted, then binding will fail and you will have appropriate entries in your errors object. Are you looking to do more extensive validation of your numeric value (like range checking or something)? If so, you need to create a Validator for your command class and register it accordingly with your Controller.

rajyalakshmi.suman
Sep 13th, 2007, 02:32 AM
For example we for date we use

binder.registerCustomEditor(
Date.class,
null,
new CustomDateEditor(dateFormat, true));
which is provided by spring framework,but i f am binding/registering my own validation class it doesnt aallow.

rajyalakshmi.suman
Sep 13th, 2007, 02:38 AM
I have Integer field:bu_Nr
integer value has to be entered in textfiled if i enter a alpha string and click on submit it should show:"pLEASE ENTER Numeric Data"

So i have written a Java class for numeric check:

public static boolean isNumeric(String numericString) {

boolean retrunType = false;
int j = 0;
int digit = 0;
if (numericString == null || (numericString != null && numericString.length() == 0)) {

return retrunType;
}
else{
for (j = 0; j < numericString.length(); j++) {

char ch = numericString.charAt(j);
if (Character.isDigit(ch)) {

retrunType = true;

}

}
}

return retrunType;

}


Now i have to bind it in controller by using register.bind...which is not allowing to bind .

pmularien
Sep 13th, 2007, 09:24 AM
Spring will already handle primitive type conversion for you, if you're binding properly to a primitive (int, long, etc) property. If you're binding to a wrapper type (Integer, Long, etc) look at the CustomNumberEditor (http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/beans/propertyeditors/CustomNumberEditor.html) property editor.
Now i have to bind it in controller by using register.bind...which is not allowing to bind .
Without controller code (please use [ code ] tags!) or an error message, it is going to be hard for us to guess what your problem is.

rajyalakshmi.suman
Sep 14th, 2007, 12:25 AM
Hi ,

Controller class code is attached:while bind "boolen is undefined for CustomNumberEditor"


protected void initBinder(PortletRequest request,PortletRequestDataBinder binder)
throws Exception {


String numericString = request.getPortletSession().getAttribute("BU_NR").toString();
boolean numericFormat = false;

if (numericString!= null ){
numericFormat = BDCompanyNumericValidator.isNumeric(numericString) ;
}

binder.registerCustomEditor(
BDCompanyNumericValidator.class,
null,
new CustomNumberEditor(numericFormat, true));




My Numeric Validator class is:

public static boolean isNumeric(String numericString) {

boolean retrunType = false;
int j = 0;
int digit = 0;
if (numericString == null || (numericString != null && numericString.length() == 0)) {

return retrunType;
}
else{
for (j = 0; j < numericString.length(); j++) {

char ch = numericString.charAt(j);
if (Character.isDigit(ch)) {

retrunType = true;

}

}
}

return retrunType;

}

Jörg Heinicke
Sep 14th, 2007, 01:25 AM
Validation is a completely different issue from conversion of strings to objects. So your whole setup seems to be screwed. From what I understand you do not need BDCompanyNumericValidator at all, because Spring takes care if the conversion fails. There is no need to pre-check for number or not (it would return true for every String ending with a digit char like abc3 anyway). The way your register the CustomDateEditor you have to do it for the CustomNumberEditor as well. Something like
binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));

Joerg

rajyalakshmi.suman
Sep 14th, 2007, 01:39 AM
Hi Jorg,

I have BUNR Textfiled-of type integer ,i have binded in my controller class.
when i enter 'aa2' in textfield and onclik of button,Am getting nullpointer exception at intibinder

Jörg Heinicke
Sep 14th, 2007, 09:31 AM
when i enter 'aa2' in textfield and onclik of button,Am getting nullpointer exception at intibinder

Where exactly? Actually initBinder() should not have anything to do with the request parameters and so it should not matter at all if the value is aa2, 123 or anything else.

Joerg