2

I have built a simple date check to validate if the date from the form is smaller then the current date.

ValidationAttribute

public class DateIsValidAttribute : ValidationAttribute
{
    private const string DefaultErrorMessage = "Datumet är ogiltigt";
    public string DateNotValidErrorMessage { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        DateTime? d = Convert.ToDateTime(value);

        if (d > DateTime.Now)
        {
            return ValidationResult.Success;
        }

        return new ValidationResult(DateNotValidErrorMessage ?? DefaultErrorMessage);
    }

}

ViewModel

[Required(ErrorMessage = "Prenumerationen måset ha ett startdatum")]
[Display(Name = "Startdatum")]
[DateIsValid(DateNotValidErrorMessage = "Ogiltigt datum.")]
public DateTime? StartDate { get; set; }

HTML

<div class="form-group">
<label asp-for="StartDate"></label>
<input type="text" asp-for="StartDate" class="form-control" id="startDate" />
<span asp-validation-for="StartDate" class="text-danger"></span>
</div>

The validation it self works as expected but the ErrorMessage wont show. All the other ErrorMessages I have in the ViewModel works. They use Default ValidationAttribute.

AllramEst
  • 1,319
  • 4
  • 23
  • 47
  • I just tried to reproduce this using your code above but it works as expected. I see either "Prenumerationen måset ha ett startdatum" or "Ogiltigt datum." depending upon whether or not I set `DateNotValidErrorMessage` as in your example. Your custom attribute doesn't support client-side validation: could it be to do with that? – Kirk Larkin Aug 22 '18 at 09:01
  • It could be that the custom attribute doesn't support client-side validation. but I am a bit confused because some examples out there only show what I have done with the code above and then it should work. And it does but not the ErrorMessage. After reading your post I found this: [client-side](https://thewayofcode.wordpress.com/tag/custom-unobtrusive-validation/) From that post I gather I have to implement the rules I want to the client-side. Am I on the right track? How did you manage to get it to work? – AllramEst Aug 22 '18 at 09:28
  • The only real difference is that I disabled client-side validation in my example project (I just removed the script tags). In theory, server-side validation on its own should still work even if client-side validation is enabled, so it might not be your problem but it's the only real difference. You might want to consider creating a [mcve]. – Kirk Larkin Aug 22 '18 at 09:30
  • I will se too that... – AllramEst Aug 22 '18 at 09:31
  • Could you undelete your last post? If my comment helps you, I want to add an answer.:-) – Joy Wang Aug 23 '18 at 09:07
  • 1
    Unfortunately not. We decided that a custom Validation wasn't needed in this case. – AllramEst Aug 23 '18 at 10:00

1 Answers1

0

I assume that the default ValidationAttribute class does something with the ErrorMessage property inside it's IsValid method. In your case you are doing nothing about that property inside your IsValid method, so nothing happens with the ErrorMessage property.

Check if you can override an IsValid method that returns a ValidationResult instead, you can send your errormessage in that as a parameter. If your caluculation succeeds, you can return ValidationResult.Success or whatever you want.

Philzax
  • 196
  • 6