A question about configuring the jQuery Validation plugin

5 views
Skip to first unread message

BobS

unread,
Sep 16, 2009, 8:54:14 AM9/16/09
to ValidateThis
I received the following question via email. I am including it here,
with the answer, in case anyone else has a similar question:

-----------------

Your ValidateThis project is great and i'm really getting into it. I
was just wondering how I can add the "errorClass" and "errorPlacement"
attributes to the generated JS code.

Any help would be appreciated.

Regards

James

-----------------

Hi James,

I'm glad you're finding VT useful. I'm assuming from your question
that you are using the features built into VT to load your JS
libraries and initialize the JS, and that's where the problem is
coming from. If you were to load the JS libraries manually, you could
do your own setup of the jQuery validation plugin in your page, prior
to calling any VT methods. Something like this:

<!--- Your own code in your template --->
<script src="/js/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="/js/jquery.field.min.js" type="text/javascript"></script>
<script src="/js/jquery.validate.pack.js" type="text/javascript"></
script>

<script type="text/javascript">
$(document).ready(function() {
jQuery.validator.setDefaults({
errorClass: 'errorField',
errorElement: 'p',
errorPlacement: function(error, element) {
error.prependTo( element.parents('div.ctrlHolder') )
},
});
});
</script>

<!--- Now call VT's methods --->
<cfoutput>
#application.ValidateThis.getInitializationScript(JSIncludes=false)#
#application.ValidateThis.getValidationScript(objectType="User")#
</cfoutput>

I believe that that would solve the problem that you are currently
facing.

Your question does give me an idea for an enhancement, however.
Perhaps I should allow for a struct of defaults to be passed into the
getInitializationScript() method, which could then be used for
configuring the plugin. If I implement that then one could continue
to allow VT to set up all of the JS (including the libraries) and
still be able to configure the plugin. I suppose a more generic
approach to that would be to simply allow for a block of code to be
passed into the method, and it would then be included after the
libraries are loaded but before any other script is output. That
would allow for that feature to be used with other JS implementations.

What do people think of that idea? Does that sound like a useful
addition to the framework?

James, does the information above answer your question?

Cheers,
Bob

Carlito ElGuapo

unread,
Sep 16, 2009, 10:37:48 AM9/16/09
to ValidateThis
Hi Bob,

This worked a treat. Thanks for your help.

James
Reply all
Reply to author
Forward
0 new messages