Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ASP.Net MVC 4 C# -- conditional Model validation ?

Posted on 2013-06-12
2
Medium Priority
?
3,236 Views
Last Modified: 2013-06-28
I have one app, but each car/truck has different
"WORK_ORDER" database table requirements.

How can I setup WORK_ORDER.cs conditional validation
since some controllers have different "REQUIRED fields" ?
--------------------------------------------------
WORK_ORDER.cs CURRENT
  ** [Required(ErrorMessage = "Supplier is required")]
     public string Supplier { get; set; }

WORK_ORDER.cs DESIRED
  ** if "car/truck" field is BUICK, then
     [Required(ErrorMessage = "Supplier is required")]
     public string Supplier { get; set; }
  ** else
     public string Supplier { get; set; }
--------------------------------------------------
Example
1. create a new \Controllers\BUICK_HeaderController.cs
2. system automatically creates \Views\BUICK_Header\Create.cshtml, Delete.cshtml, etc
3. repeat steps for OtherCars_DetailController.cs, OtherTrucks_DetailController.cs, etc
4. setup WORK_ORDER.cs conditional validation since
   some controllers have different "REQUIRED fields"
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Stephan earned 2000 total points
ID: 39243512
Hi, You probably need to create a new type of attributes that specifies your condition.

Here is version I mostly use:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class RequiredIfAttribute : ConditionalAttributeBase, IClientValidatable
    {
        private RequiredAttribute _innerAttribute = new RequiredAttribute();

        public string DependentProperty { get; set; }
        public object TargetValue { get; set; }

        public RequiredIfAttribute(string dependentProperty, object targetValue)
            : this(dependentProperty, targetValue, null)
        {
        }

        public RequiredIfAttribute(string dependentProperty, object targetValue, string errorMessage)
            : base(errorMessage)
        {
            this.DependentProperty = dependentProperty;
            this.TargetValue = targetValue;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (ShouldRunValidation(value, this.DependentProperty, this.TargetValue, validationContext))
            {
                if (!_innerAttribute.IsValid(value))
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName), new[] { validationContext.MemberName });
            }

            return ValidationResult.Success;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule()
            {
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
                ValidationType = "requiredif",
            };

            string depProp = BuildDependentPropertyId(metadata, context as ViewContext);

            string targetValue = (this.TargetValue ?? "").ToString();
            if (this.TargetValue != null && this.TargetValue.GetType() == typeof(bool))
                targetValue = targetValue.ToLower();

            rule.ValidationParameters.Add("dependentproperty", depProp);
            rule.ValidationParameters.Add("targetvalue", targetValue);

            yield return rule;
        }

        private string BuildDependentPropertyId(ModelMetadata metadata, ViewContext viewContext)
        {
            return QualifyFieldId(metadata, this.DependentProperty, viewContext);
        }

        public override string FormatErrorMessage(string name)
        {
            if (!String.IsNullOrEmpty(this.ErrorMessageString))
                _innerAttribute.ErrorMessage = this.ErrorMessageString;
            return _innerAttribute.FormatErrorMessage(name);
        }
    }

Open in new window


It would be nice to add some client-side validation as well,
copy this code and place it after the unobstrusive validation script is loaded:
(function ($) {
    $.validator.addMethod('requiredif',
        function (value, element, parameters) {
            var id = '#' + parameters['dependentproperty'];
            var targetvalue = parameters['targetvalue'];
            targetvalue =
              (targetvalue == null ? '' : targetvalue).toString();
            var control = $(id);
            var controltype = control.attr('type');
            var actualvalue =
                controltype === 'checkbox' ?
                control.attr('checked').toString() :
                control.val();

            if (targetvalue === actualvalue)
                return $.validator.methods.required.call(
                  this, value, element, parameters);

            return true;
        }
    );

    $.validator.unobtrusive.adapters.add(
        'requiredif',
        ['dependentproperty', 'targetvalue'],
        function (options) {
            options.rules['requiredif'] = {
                dependentproperty: options.params['dependentproperty'],
                targetvalue: options.params['targetvalue']
            };
            options.messages['requiredif'] = options.message;
        }
    );
});

Open in new window


Then it is possible to use it like this:
[RequiredIf("TransportType", "BUICK", ErrorMessage = "Supplier is required")]
public string Supplier { get; set; }
0
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 39243529
Hi,

You can also use the IValidatableObject interface to provide model code validation.

Here is an example and more information IValidatableObject.


Regards,
Mishu
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question