Frage

I am trying to implement a custom validation on my ASP.NET MVC3 form.

The first custom validation is only validating if a file has been selected in the file upload input.

It worked fine when I had only one client validation method. When I tried to add a second one. The second validation method is never triggered.

The GetValidationRules method in my attribute class

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
    var rule = new ModelClientValidationRule
                   {
                       ValidationType = "file",
                       ErrorMessage = "ResumeEmptyError".Translate("fordia_subform")
                   };

    var rule2 = new ModelClientValidationRule
    {
        ValidationType = "extension",
        ErrorMessage = "ResumeFileFormatError".Translate("fordia_subform")
    };

    var list = new List<ModelClientValidationRule>();

    list.Add(rule2);
    list.Add(rule);
    return list;
}

My javascript code in my view

<script type="text/javascript">
    jQuery.validator.addMethod("file", function (value, element) {
        return $('#ResumeFileName').val() != '';
    });

    jQuery.validator.addMethod("extension", function (value, element) {
        return $('#ResumeFileName').val() == 'a';
    });
    jQuery.validator.unobtrusive.adapters.add("file", function (options) {

        options.rules["file"] = options.params.param;
        if (options.message) {
            options.messages['file'] = options.message;
        }
    });
    jQuery.validator.unobtrusive.adapters.add("extension", function (options) {

        options.rules["extension"] = options.params.param;
        if (options.message) {
            options.messages["extension"] = options.message;
        }
    });
 </script>

When I look at my HTML source, I have the following HTML attributes on my input element :

<input data-val="true" data-val-extension="Erreur: format error" data-val-file="Required" id="Resume" name="Resume" type="file" value="" class="input-validation-error">

What am I missing to have multiple client validation methods on this form?

War es hilfreich?

Lösung

In the script you have shown you seem to be using some options.params.param parameter which is never declared nor passed from your validation attribute. So at its present form your script won't work even with a single rule. You said it was working but I guess you must have changed something in the code because what you have shown has no chance of working.

So if you don't have parameters here's what you could do (notice the empty array passed as second argument to the add adapter method):

jQuery.validator.addMethod("file", function (value, element) {
    return $('#ResumeFileName').val() != '';
});
jQuery.validator.unobtrusive.adapters.add("file", [], function (options) {
    options.rules["file"] = options.params;
    if (options.message) {
        options.messages['file'] = options.message;
    }
});

jQuery.validator.addMethod("extension", function (value, element) {
    return $('#ResumeFileName').val() == 'a';
});
jQuery.validator.unobtrusive.adapters.add("extension", [], function (options) {
    options.rules["extension"] = options.params;
    if (options.message) {
        options.messages["extension"] = options.message;
    }
});

and if you have parameters you will need to declare them first on the validation rules returned by the attribute and then use them in the adapter as shown in this post.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top