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
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: