Announcement Announcement Module
Collapse
No announcement yet.
Validator Alphanumeric chars entered for numeric command property Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          Thanks Marten

          Comment

          Working...
          X