Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate to see if form has changed

I am writing a validator which needs to test if a spring form object has changed.

In the validator if no changes have been made to the form, an error should be displayed.

Is there a spring mechanism to do this?

This is because a very expensive webservice update call is made when I submit, and I need to prevent the webservice call from being made if no changes have been made.

Cheers.

like image 987
Mitch Avatar asked Apr 20 '11 06:04

Mitch


People also ask

How do you check that an HTML form has been changed?

This page demonstrates FormChanges(string FormID | DOMelement FormNode), a generic, standalone JavaScript function which detects when the user has made any updates to a form. It returns an array of changed elements – an empty array indicates that no changes have been made.

How do you check if a form is validated?

Using the email type, we can check the validity of the form field with a javascript function called… checkValidity() . This function returns a true|false value. checkValidity() will look at the input type as well as if the required attribute was set and any pattern="" tag .

How do I validate my form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.


2 Answers

I'm not aware of any built-in Spring mechanism to handle this. I'd save a copy of the original object, and the modified object. I'd override the Form.equals() method appropriately (possibly delegating to org.apache.commons.lang.builder.EqualsBuilder.reflectionEquals(), if all of the fields are primitives/strings) and use Form.equals() to check whether the form had changed.

I have seen two distinct responses here which imply that overriding the "hashCode" method is a good way to go. It is not in the contract of hashCode() to guarantee inequality, this could lead to problems. Be sure to override equals() as well. Of course it's a one in a trillion chance of a hash collision, but it's poor design to rely on hashCode() alone when you're trying to check if two objects are different.

like image 53
piepera Avatar answered Oct 19 '22 13:10

piepera


Override your hashCode() function to ensure a different value is returned when form values change. Save the return value of the function and test to see whether it's changed.

public class Person {
  String first;
  String last;
  int prevHash;
  @Override
  public int hashCode() {
    // does not include the value of currHash
    String stringHash = String.format("%1$-29s%2$-29s", first,last);
    return stringHash.hashCode();
  }
}

public class PersonValidator  implements Validator {
  public void validate(Object obj, Errors e) {
    Person p = (Person) obj;
    if (p.hashCode() == p.getPrevHash()) {
      e.reject("person", "unchanged");
    } 
  }
  ...
}

I don't think there's a Spring-provided validation test to check whether a form backing object has changed

Note that you could perform additional tests on the client side before allowing the user to submit the form.

like image 27
climmunk Avatar answered Oct 19 '22 13:10

climmunk