This project is read-only.

Custom rule ajax support

Apr 23, 2009 at 3:10 PM
I've been using xVal for my new project and it is a great helper :)
I am creating my own attributes for validation and want them to be always server-validated, like username duplicate detection, and so. This require to invoke ajax calls on the client side.

Now I am doing that this way (example starting letter validation):
1. Create validation attribute definition implementing ICustomRule

    public class FirstLetterAttribute:ValidationAttribute,ICustomRule
        public string FirstLetter { get; set; }

        public FirstLetterAttribute()
        { }

        public FirstLetterAttribute(string theLetter)
            FirstLetter = theLetter;

        public override bool IsValid(object item)
                return FirstLetterValidate(FirstLetter, (string)item); //call to the common validation function

         public xVal.Rules.CustomRule ToCustomRule()
            return new CustomRule(
                new { firstLetter = FirstLetter },
                "The text must start with letter: " + FirstLetter

2. Creating ajax validator
        public JsonResult IsFirstLetterValid(string letter, string text)
            JsonResult result = new JsonResult();
            result.Data =  (FirstLetterValidate(letter, text); //call to the common validation function
            return result;

3. Implementing JS function for ajax call
        function FirstLetter(value, element, params) {
            var valid = false;

                type: "GET",
                url: "/Validator/IsFirstLetterValid",
                data: { letter: params.firstLetter.toString(), text: value.toString() },
                async: false,
                dataType: "json",
                success: function(jsonResponseObject) {
                    valid = jsonResponseObject;
                beforeSend: function() { $("#debugee").html("Loading..."); },
                complete: function() { $("#debugee").html(""); }

            return valid;

Is it possible that xVal will create the JS part automatically?
May 4, 2009 at 4:29 PM
I am not sure if anybody has found my question at the end of previous post.?
So I would like to ask again if future version of xVal could support custom validation attributes with generation of automatic javascript calls for ajax-based validation.

This way some complex or data-based validations may be performed always on the server side, but with great support on client side via ajax calls.

Thanks for your reply.