1

Currently I'm working on a MVC 3 Environment and I need to add a Validation that validates 2 Fields in Dataannotation Models just like the validation of [Required(ErrorMessage="Required!")] that is triggered onsubmit.

Currently , The Validation [Required] is working but i need to check if the bool and dateTime Datatype

Heres the Code in my Model

public class NameInfo: IValidatableObject
    {

        [Display(Name = "Name")]
        [Required(ErrorMessage = "Required!")]
        public string Name{ get; set; }

        [Display(Name = "Start Date")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        public Nullable<DateTime> StartDate { get; set; }

        [Display(Name = "Already Starting")]
        public bool IsStarting{ get; set; }


        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (!IsStarting&& !StartDate.HasValue)
            {
                yield return new ValidationResult("Required.", new[] { "StartDate" });

            }  
        }
    }

Currently im using IValidatableObject but its not validating unless the name Field modelstate is already Valid

Thanks in Advance!

Enrique Gil
  • 754
  • 4
  • 19
  • 50
  • 1
    Are you want to ensure that if `IsStarting` is `true` then `StartDate` must have a value? –  Sep 02 '15 at 08:21
  • @StephenMuecke yes, because this two fields are connected to each other, if `IsStarting` is `false` and `StartDate` is `null` then the validation will be shown – Enrique Gil Sep 02 '15 at 08:24
  • Forget the `IValidatableObject`. Look at using a [foolproof](http://foolproof.codeplex.com/) `[RequiredIfTrue]` or similar attribute (and its easy enough to write your own). That way you get both client side and server side validation –  Sep 02 '15 at 08:27
  • @StephenMuecke is this an addon? – Enrique Gil Sep 02 '15 at 08:29
  • @StephenMuecke if this is an addon, I'm not allowed to use an addtional addon to the project that ive been working on – Enrique Gil Sep 02 '15 at 08:30
  • foolproof is (you can download the nuget package). But there are plenty of example of how to write your own. –  Sep 02 '15 at 08:30
  • @StephenMuecke can you supply some examples, been searching for 1 hour and tried it all , yet none of them worked – Enrique Gil Sep 02 '15 at 08:34
  • 1
    I have my own, so I will post it (need a break so give me 30 min or so) –  Sep 02 '15 at 08:36
  • @StephenMuecke thanks man, will wait your answer – Enrique Gil Sep 02 '15 at 08:38

1 Answers1

1

Currently im using IValidatableObject but its not validating unless the name Field modelstate is already Valid

DataAnnotations and IValidatableObject will not validate together. What I mean by that is the IValidatableObject's validate method will only be called if your DataAnnotations are valid.

Either write your own ValdiationAttribute or add the required validation to your IValidatableObject.

Here is an example of the latter...

public class NameInfo : IValidatableObject
{

    [Display(Name = "Name")]
    public string Name { get; set; }

    [Display(Name = "Start Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public Nullable<DateTime> StartDate { get; set; }

    [Display(Name = "Already Starting")]
    public bool IsStarting { get; set; }


    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {

        if (string.IsNullOrEmpty(Name))
        {
            yield return new ValidationResult("Name is required!");
        }


        if (!IsStarting && !StartDate.HasValue)
        {
            yield return new ValidationResult("Required.", new[] { "StartDate" });

        }

    }
}
heymega
  • 9,215
  • 8
  • 42
  • 61