EdisonCP / sharp-architecture

Automatically exported from code.google.com/p/sharp-architecture
Other
0 stars 0 forks source link

SharpModelBinder/MvcValidationAdapter uses the wrong key when placing errors into ModelState #112

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The SharpModelBinder/MvcValidationAdapter uses the class name of the model 
instead of the actual form/parameter name. 
Referencing the sample code at the end of the post, validation errors end up as 
"RegisterUserRequest.Email" instead of 
"register.Email". This breaks some of the magic field highlighting built into 
the ASP.NET MVC HtmlHelper methods.

What steps will reproduce the problem?
1. Create a simple form using Html.Text("model.Property") syntax.
2. Add validation rules to the model.
3. Round-trip the form, using values that cause validation errors.

What is the expected output? What do you see instead?
The fields with invalid values should be highlighted ("class='invalid-...'") 
after the form is submitted.

What version of the product are you using? On what operating system?
r478

Simple example for the registration form I'm building:

# using (Html.BeginForm<LoginController>(x => x.Register())) {
<table>
  <tr><td>Email Address:</td><td>${Html.TextBox("register.Email")}</td></tr>
  <tr><td>Login:</td><td>${Html.TextBox("register.Login")}</td></tr>
  <tr><td>Password:</td><td>${Html.Password("register.Password")}</td></tr>
  <tr><td>Confirm Password:</td><td>${Html.Password("register.Password2")}</td></tr>
  <tr><td>First Name:</td><td>${Html.TextBox("register.Firstname")}</td></tr>
  <tr><td>Last Name:</td><td>${Html.TextBox("register.Lastname")}</td></tr>
 </table>
 ${Html.SubmitButton("submit", "Register")}          
# }

public class RegisterUserRequest : ValidatableObject
{
 [NotNull, NotEmpty, Email] public string Email { get; set;} 
 [NotNull, NotEmpty] public string Login { get; set;}
 [NotNull, NotEmpty] public string Password { get; set; }
 [NotNull, NotEmpty] public string Password2 { get; set;}
 [NotNull, NotEmpty] public string Firstname { get; set;}
 [NotNull, NotEmpty] public string Lastname { get; set;}
 protected override IEnumerable<PropertyInfo> GetTypeSpecificSignatureProperties()
 {
  return null;
 }
}

public ActionResult Register(RegisterUserRequest register)
{
 //LoginService.RegisterUser(register);
 return View();
}

Original issue reported on code.google.com by nnyst...@gmail.com on 9 Jul 2009 at 4:45

GoogleCodeExporter commented 8 years ago
Fix is simple, just push the bindingContext.ModelName into 
MvcValidationAdapter.TransferValidationMessagesTo. Patch attached.

Original comment by nnyst...@gmail.com on 9 Jul 2009 at 4:49

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks for the fix nnystrom!  I've included your patch in my local build and 
will
have it available in the next check-in (which should be 1.0 RTM).

Original comment by wmccaffe...@gmail.com on 15 Jul 2009 at 4:37