I have an extended class of RequiredAttribute that doesn't send error messages back. If I check it in the debugger the text is there alright.
public class VierRequired : RequiredAttribute
{
public VierRequired(string controlName)
{
//...
}
public string VierErrorMessage
{
get { return ErrorMessage; }
set { ErrorMessage = value; }
}
// validate true if there is any data at all in the object
public override bool IsValid(object value)
{
if (value != null && !string.IsNullOrEmpty(value.ToString()))
return true;
return false; // base.IsValid(value);
}
}
I call it like this
[VierRequired("FirstName", VierErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }
And the mvc-view
<%: Html.TextBoxFor(model => model.FirstName, new { @class = "formField textBox" })%>
<%: Html.ValidationMessageFor(model => model.FirstName)%>
It works if I use the normal Required annotation
[Required(ErrorMessage = "Please enter your name")]
public string FirstName { get; set; }
But the custom does not send any error message back
value.ToString()
is actually doing what you think it is. You needvalue as String
instead. – Wrestling