14

I have a model class that is following

 public bool Saturday{ get; set; }

 public bool Sunday{ get; set; }

 public string Holiday{ get; set; }

In which I want to use the RequiredIf condition for the Holiday field using the both Saturday and Sunday fields. Can I use like following

   [RequiredIf("Sunday,Saturday",false)]
   public string Holiday{ get; set; }

So I don't know how to use the RequiredIf condition in my model class, So please someone help me

Juan Salvador Portugal
  • 1,233
  • 4
  • 20
  • 38
Md Aslam
  • 1,228
  • 8
  • 27
  • 61
  • you can create your own custom validation attribute in MVC this link will help you to create them http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC – Frebin Francis Feb 05 '15 at 05:48
  • Are your referring to foolproof's `RequiredIf` attribute? (if so, add the tag) –  Feb 05 '15 at 05:53
  • I added the tag, but doesn't work. Can you give any example code for use the foolproof's RequiredIf attribute? @Stephen Muecke – Md Aslam Feb 05 '15 at 06:13
  • No, I meant to the list of tags at the bottom of the question (if you are using foolproof) If not, then what `RequiredIf` attribute are you using? –  Feb 05 '15 at 08:10
  • I don't know exactly because I am beginner to .Net Mvc. And RequiredIfAttribute class is used to it. And also by using System.ComponentModel.DataAnnotations , I have done the attribute.If you know the solution for it please help me @ Stephen Muecke – Md Aslam Feb 05 '15 at 12:40

3 Answers3

21

Maybe try this in your model:

[Required]
public bool Saturday{ get; set; }

[Required]
public bool Sunday{ get; set; }

[NotMapped]
public bool SatSun
{
    get
    {
        return (!this.Saturday && !this.Sunday);
    }
}

[RequiredIf("SatSun",true)]
public string Holiday{ get; set; }
Chico Ribeiro
  • 279
  • 1
  • 6
2

If the need for more complex validation arises, I'd recommend implementing IValidatableObject.

public class YourModel : IValidatableObject
{
    public bool Saturday{ get; set; }

    public bool Sunday{ get; set; }

    public string Holiday{ get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var result = new List<ValidationResult>();

        if (Saturday == false && Sunday == false && string.IsNullOrEmpty(Holiday))
        {
            result.Add(new ValidationResult("Holiday is required outside weekends"));
        }

        return result;
    }
}

If you combine property checks with IValidatableObject, be sure to note this behaviour.

Terra-jin
  • 63
  • 6
  • Worth noting that passing property names as an array of strings for the second parameter of `ValidationResult` will trigger validation CSS on bound properties. – clamchoda Oct 25 '22 at 18:48
-1

My Project has RequiredIf in it.

[Required]
public int SalesID { get; set; }

[RequiredIf("SalesID==1", ErrorMessage = "License is required.")]
public string License{ get; set; }

It shows error message 'License is required.' when License is left blank only if SalesID is 1. License cannot be blank if SalesID is 1.

For your code it should be something like

[RequiredIf("Sunday,Saturday",AllowEmptyStrings=false)]
public string Holiday{ get; set; }

It means if Sunday and Saturday are true you can allow Holiday property to be an Empty String.

Sukanya1991
  • 778
  • 3
  • 17