Results 1 to 5 of 5

Thread: Validator Alphanumeric chars entered for numeric command property

  1. #1

    Default Validator Alphanumeric chars entered for numeric command property

    Hi,

    Im trying to understand what happens in the following case..

    If I have a simple command object like the following..

    Code:
    class MyCommand
    {
    private int mMyCommandValue;
    
    public int getMyCommandValue()
    {
    return mMyCommandValue;
    }
    
    public void setMyCommandValue(int value)
    {
    mMyCommandValue = value;
    }
    }
    My jsp page has the following form

    Code:
    <form:form>
    <form:input path="myCommandValue"/>
    <input type="submit" value="submit/>
    </form:form>
    And heres my validators validate method...

    Code:
    public void validate(Object object, Errors errors)
    {
    MyCommand myCommand = (MyCommand) object;
    
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "myCommandValue", "error.required", "Required Field");
    
    }
    Now in the examples It shows that you can further validate the command object by doing such things as

    Code:
    if (myCommand.getMyCommandValue() < 10)
    {
    errors.rejectValue("myCommandValue", "error.too_low", "Value too low");
    }
    But what if i want to ensure that the user hasnt entered a character into the form field? Does the BeanWrapper set the commands value to null, or does it leave it as it is?

    How do i tell the user that they entered a non numeric value into the form?

    Regards

    Ben

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Well how about parsing the entered String into an Integer? If it is a correct number, it will succeed. If not you will get an Exception.

    Code:
    try {
     int val = Integer.parseInt(yourValue);
    } catch (Exception e) {
       errors.rejectValue("myCommandValue", "notanumber", "Not a numeric value");
    }
    Or if you find that cheesy you can always use a regular expression to check it.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3

    Default

    But where do you get the 'yourValue' from. You havent access to the HttpServletRequest and the command value is an int, so the BeanWrapper cant set the nonnumeric value entered into the form into the command object.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    I guess it is time to go home, I totally missed the fact that you are already in a Validator.

    If you enter for instance 'abc' as the value, Spring won't be able to create an integer and will create a BindException. Further validation doesn't occur and the form is shown again.

    you will then have an error code with typeMismatch.[commandName].[fieldname]. So if you put that in your resource bundle you will get a message. Or you can even make it more global and enter typeMismatch.java.lang.Integer and that message will then appear for all the Integers.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5

    Default

    Thanks Marten

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •