limcheekin / jquery-validation-ui

JQuery Validation UI Plugin - Client Side Validation without writing JavaScript
http://limcheekin.github.io/jquery-validation-ui/
Apache License 2.0
20 stars 18 forks source link

remote validation not working with command objects #17

Open pdemilly opened 12 years ago

pdemilly commented 12 years ago

When using command objects a few things don't work correctly.

1) auto wiring.

when a custom validator needs to access a service defined in the command object (same might be for domain object), the service instance variable has not been injected before calling the custom validator.

in JQueryRemoteValidatorController.groovy

                        import org.springframework.beans.factory.config.AutowireCapableBeanFactory

and add this after initiating validatableInstance in the method validate() around line 41 applicationContext.autowireCapableBeanFactory.autowireBeanProperties(validatableInstance,AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)

2) command objects don't respond to isAttached

in same method validate() around line 57 replace

          if(validatableInstance.isAttached()) validatableInstance.discard()

by if(validatableInstance.metaClass.respondsTo(validatableInstance, "isAttached") && validatableInstance.isAttached()) validatableInstance.discard()

Hope that helps

Regards

Pascal

limcheekin commented 12 years ago

Hi Pascal,

Thanks for report the issue. Could you clone the project, apply the fix and send me pull request.

Thank you

pdemilly commented 12 years ago

OK! I did. Made the change ready to push them. However I have a question. in validate of JQueryRemoveValidationController param doesn't have all the fields value of the form. Therefore the binding doesn't happen which mean I cannot have a custom validator that check the value of another field. (ie password check). Where should I look to add this?

christopherKwiatkowski commented 11 years ago

@pdemilly I just wanted to offer a suggestion, even though this thread is 3 months old and it is likely that you have already addressed the issue of not having a command object that is fully populated. I added an additional property to the data object created by the JQueryValidationService.createRemoteJavaScriptConstraints method called serializedData:

    String remoteJavaScriptConstraints = "\t${constraintName.equals('unique') || constraintName.equals('validator')?constraintName:'custom'}: {\n" +
            "\turl: '${contextPath}/validation/validate',\n" +
            "\ttype: 'post',\n" +
            "\tdata: {\n" +
            "\t\tvalidatableClass: '${validatableClassName}',\n" +
            "\t\tproperty: '${propertyName}',\n" +
            "\t\tserializedData: function() { return myForm.serialize() }\n"

As the variable name of the form is always myForm you may serialize the entire form and then use the values from the params.serializedData in the remote validation controller. This is somewhat of an un-elegant solution, but it gets the values up to the validation controller and we use them as necessary to bind to the Validateable class currently under remote validation.

jonroler commented 11 years ago

I have now pushed a fix for this problem (pull request #26) which is nearly identical to what Pascal proposed.