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 h开发者_如何学JAVAave been made.
Cheers.
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.
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.
Try different approach based on client-side checking if form has changed. Starting form user interface / jQuery:
$(document).ready(function() {
var isFormChanged = false;
var isConfirmed = false;
$("form :input").change(function() {
console.log($(this).attr('name'));
isFormChanged = true;
});
$("#storeForm").submit(function(event) {
if (!isConfirmed) {
event.preventDefault();
}
if (isFormChanged) {
console.log("Form has changed");
$(":submit").addClass("btn-danger");
$(":submit").text("Confirm");
isConfirmed = true;
}
});
});
You can also make hidden field in the form to send back information to Spring to check all of those once again on server-side. It is faster than pushing back all form data to server just to check if something has changed every time.
精彩评论