I wrote a custom data validator for one of the form fields and it does the job however the form is still being submitted and no error message being returned to the user.
Is there a way I can force the error to a user to show up and block the form from being submitted ?
[CheckDateInPast(ErrorMessage = "Your date cannot be in the future.")]
.
.
.
@Html.ValidationMessageFor(model => model.MyField)
How can I make my custom validator work as a [Require] attribute so the user cannot submit anything that doesn’t fit my criteria.
Thank you in advance !
Edit. Here is the validator. I’m imply verifying that the date provided is not in the future and not less than what’s allowed in SQL Server.
public override bool IsValid(object value)
{
DateTime date;
bool success = DateTime.TryParse(value.ToString(), out date);
if (!success)
return false;
if (date < System.Data.SqlTypes.SqlDateTime.MinValue || date > DateTime.Now)
return false;
return true;
}
You need to enable client side validation by implementing IClientValidatable interface to prevent form from being submitted with invalid data.
See the following for details: