Why is my validation message generic? ASP.NET MVC

I wrote a custom validation handler for this ASP.NET MVC application. Here is a screenshot of the verification messages:

alt text

As you can see, the title and director works great, but there is no date. Code for Create view:

<fieldset>
    <legend>Fields</legend>
    <p>
        <%= Html.Label("Title", "Title:") %>
        <%= Html.TextBox("Title") %>
        <%= Html.ValidationMessage("Title", "*") %>
    </p>
    <p>
        <%= Html.Label("Director", "Director:")%>
        <%= Html.TextBox("Director") %>
        <%= Html.ValidationMessage("Director", "*") %>
    </p>
    <p>
        <%= Html.Label("ReleaseDate", "Release Date:")%>
        <%= Html.TextBox("ReleaseDate") %>
        <%= Html.ValidationMessage("ReleaseDate", "*") %>
    </p>
    <p>
        <%= Html.Submit("Create") %>
    </p>
</fieldset>

      

and here is the code that handles the validation logic:

public bool ValidateMovie(Movie movieToValidate)
{
    if (movieToValidate.Title.Trim().Length == 0)
        _validationDictionary.AddError("Title", "Title is required.");

    if (movieToValidate.Director.Trim().Length == 0)
        _validationDictionary.AddError("Director", "Director is required.");

    if (movieToValidate.ReleaseDate.ToString().Trim().Length == 0)
        _validationDictionary.AddError("ReleaseDate", "Release Date is required.");

    return _validationDictionary.IsValid;
}

      

I assumed that the key value for the validation dictionary should match the value provided for that field (ReleaseDate in this case), but that doesn't seem to be happening for me. I also tried adding space between words, in case he searched for it by display name, but also no luck.

Can anyone help me on this?

Edit

Following Joseph's suggestion , I went through the app to see where the other validation error is happening. Unfortunately I didn't find any clues ... at least clear to me. Here is a screenshot of my window:

alt text

As expected, it has three keys: title, directory, and release date. There is no sign of a fourth (third on the list? See first screenshot) validation failure.

Any further suggestions would be appreciated! Thank!

+2


source to share


2 answers


I suspect the problem is that the error is flagged when the model binder tries to bind a null value to a model property that is not valid. You might want to clear the ModelState of any errors in that particular property and then add your own model validation error. If ModelState contains multiple errors for the same property, you can only get the first one that matches.



You can also make the ReleaseDate property null (DateTime?) And then just check if the date is provided. This might be the simplest change if possible, although if the model is from the database and the field is not null, you need to provide a view model to execute it.

+2


source


I would venture to say that you have two problems.



  • I am assuming ReleaseDate is DateTime. If so, then doing movieToValidate.ReleaseDate.ToString () is going to return "1/1/1900 blah blah". You shouldn't check the length, you should check it like movieToValidate.ReleaseDate == DateTime.MinValue maybe.
  • I got the value "A". before i thought i should get an error. It had something to do with my model, which did not have something specific that was considered necessary. For example, the relationship of FK to another object has not been established or something else. If you are debugging the application in your controller, you should be able to see your ModelErrors and go to that particular error and see where it comes from.
+1


source







All Articles