JQueryValidator should parse "rules" parameter

1 view
Skip to first unread message

Andre Loker

unread,
May 6, 2008, 12:15:55 PM5/6/08
to Castle Developer Group
The Configure method of JQueryValidator parses all options except "rules". I think the following line should be added:

AddParameterToOptions( parameters, JQueryOptions.Rules, false );

Regards,
Andre

Gildas

unread,
May 7, 2008, 11:33:17 AM5/7/08
to Castle Project Development List
Hi Andre,

No, this is intended. You should use the JQueryValidator AddCustomRule
method to add your own custom rules.

Actually, if look at the code, the "rules" option is generated from a
list of custom rules and this list is already populated with two
custom rules which are supplied by default.

However, what I can do if you really need it, is to check whether this
option has been supplied before I generate code for it. In this case,
you will not have any of the custom rules supplied by default
(notEqualTo, greaterThan and lesserThan).

Just as a note, also I can understand you want to supply your own
rules option, isn't it a PITA to supply it your view code ? (I mean
Dictionary code + javascript code...).

Well, just tell me if you really need this.

Cheers,

Gildas

Andre Loker

unread,
May 7, 2008, 12:33:13 PM5/7/08
to castle-pro...@googlegroups.com
Hi Gildas,

I think that I don't use the validator as it is supposed. Bare with me,
I'm quite new to MonoRail.

> No, this is intended. You should use the JQueryValidator AddCustomRule
> method to add your own custom rules.

I don't think whether AddCustomRule does what I need. As far as I can
see it registers new JS functions to extend the default set of
validation functions (like equalTo, required, rangelength etc) by
calling jQuery.validator.addMethod.

The method that modifies the rules parameter of the validate function is
JQueryConfiguration.AddMethod, which is called by the SetXXX methods of
IBrowserValidationGenerator.
The problem is that as far as I can see this interface is only used by
Castle.Component.Validator which I currently do not use.

For your information I'll give you an overview of what I'm doing now. In
my local working dir I added the line mentioned in my first post. I then
generated some "ValidationRuleBuilder" that generates the values of the
rules and message parameters. In the view I use the builder like that:
(I use AspView as ViewEngine)

<%
var validation = NewValidation()
.Check("Personal.UserName")
.IsRequired("User name required")
.Range(3, 50, "User name must have 3-50 characters")
.Check("Personal.Email")
.IsRequired("E-mail address is required")
.Check("Personal.Password")
.IsRequired("Password is required")
.RangeLength(6, 40, "Password must have 6-40 characters")
.Check("Personal.PasswordConfirm")
.IsRequired("Password confirmation is required")
.IsEqualTo("Personal_Password", "Please repeat the password")
.Compile();
%>

The final call to Compile() creates a dictionary with two entries (rules
and messages) that is then passed to the form like that:

<%= Helpers.Form.FormTag(SiteMap.SignUp.SaveStep1().Url, validation) %>

The generated JS code looks like this (I formatted it for readability):

<script type="text/javascript">
jQuery("#form1").validate( {
rules:{
'Personal.UserName': {required:true,range:[3, 50]},
'Personal.Email': {required:true},
'Personal.Password': {required:true,rangelength:[6, 40]},
'Personal.PasswordConfirm':
{required:true,equalTo:"#Personal_Password"}
},
messages:{
'Personal.UserName': {
required:'User name required',
range:'User name must have 3-50 characters'},
'Personal.Email': {
required:'E-mail address is required'},
'Personal.Password': {
required:'Password is required',
rangelength:'Password must have 6-40 characters'},
'Personal.PasswordConfirm': {
required:'Password confirmation is required',
equalTo:'Please repeat the password'}}} );

// below the code that is generated by JSQueryValidator
jQuery.validator.addMethod('notEqualTo', function(value, element, param)
{ return value != jQuery(param).val(); }, 'Must not be equal to {0}.' );
jQuery.validator.addMethod('greaterThan', function(value, element,
param) { return ( IsNaN( value ) && IsNaN( jQuery(param).val() ) ) || (
value > jQuery(param).val() ); }, 'Must be greater than {0}.' );
jQuery.validator.addMethod('lesserThan', function(value, element, param)
{ return ( IsNaN( value ) && IsNaN( jQuery(param).val() ) ) || ( value <
jQuery(param).val() ); }, 'Must be lesser than {0}.' );</script>
</form>

> Just as a note, also I can understand you want to supply your own
> rules option, isn't it a PITA to supply it your view code ? (I mean
> Dictionary code + javascript code...).

With the API as described above, it's no PITA at all, its just the code
to build the rules, no JS involved. However, I feel like it would be
better to use the attribute based Component.Validator facility instead,
wouldn't it? While of course I double check the rules in by domain
layer, it's just that: duplicated code. It does not feel right.

I could not find much information on validation w/ MonoRail, so I'd
appreciate input in that direction.

Sorry for the lengthy post!

Regards,
Andre


Gildas

unread,
May 8, 2008, 10:35:25 AM5/8/08
to Castle Project Development List
Hi Andre,

There are two ways of using the jQuery validate plugin. You can, as
you do, add the validation rules by code; this offers a lot of
flexibility at the cost of more code to write and maintain.

The other way is to simply add css classes and custom html attributes
on your elements but in this case, you must include the jquery
metadata plugin as well. This allows you to write code like the
following (in case you don't want to use the Castle Validators
components) in brail :

${ Form.FormTag( "action.rails" ) }

${ Form.TextField( "EditedBlog.Title", { 'class':'required' }

${ Form.EndFormTag() }

And voila, your myTarget input field is now required.

However, I would advise you to use the Castle Validators Components if
you already use ActiveRecord. You could then write this code :

[ActiveRecord]
public class Blog
{
[Property( Length = 100, NotNull = true ) ]
[ValidateNonEmpty]
[ValidateLength( int.MinValue, 100 )]
public string Title
{
// getter / setter
}
}

And if your validation code would be made for you by MonoRail. Don't
foget to tell the FormHelper to use the JQueryValidator.

For a more detailled sample, look at this post :
http://www.winterdom.com/weblog/2007/08/16/ValidationInMonorail.aspx

May the Force be with you :)

Gildas

Andre Loker

unread,
May 8, 2008, 11:39:51 AM5/8/08
to castle-pro...@googlegroups.com
Hi Gildas,

Thank you for your advice. I knew I could use css classes for validation
but at first it did not feel right to me to "abuse" html/css syntax for
validation. Maybe I should give it a second chance :-)

I am not using AR (neither the framework nor the pattern) at the moment.
My current projects use repositories with NHibernate for persistence.
Can I use the attribute based validation approach without using AR,
especially without having my domain model classes derive from specific
base classes?

Regards,
Andre

P.S.: I suppose this discussion now belongs more into the castle user group


Gildas schrieb:

Gildas

unread,
May 8, 2008, 3:34:39 PM5/8/08
to Castle Project Development List
You're welcome. As you said, this discussion now belongs more into the
castle user group ;-)

Anyway, AR and the Validators components are two separated projects so
you can use the Validators attributes without AR.

You really should ask about them more informations on the user group.

Good luck !

Gildas
Reply all
Reply to author
Forward
0 new messages