I'd like to disable ModelState validation messages and keep my data annotation error messages.
When I update model, message "The value '' is invalid." apears in ModelState. I know that I'm trying to assign null to not nullable property, but I have my own validation and message.
I use something like this
[Required(ErrorMessage="Blahblah some error")]
I want user to see my message "Blahblah some error", but not stupid message: The value '' is invalid.
I think you're hitting a bug w/ EF that we fixed for RTM. The EF property setter is throwing when you try to set it to null, thus the generic error message.
Marked as answer by ricka6 on Mar 11, 2010 08:06 PM
pikacc
0 Points
3 Posts
How to disable ModelState validation messages (The value '' is invalid.)?
Mar 09, 2010 05:43 PM|LINK
I'd like to disable ModelState validation messages and keep my data annotation error messages.
When I update model, message "The value '' is invalid." apears in ModelState. I know that I'm trying to assign null to not nullable property, but I have my own validation and message.
I use something like this
[Required(ErrorMessage="Blahblah some error")]
I want user to see my message "Blahblah some error", but not stupid message: The value '' is invalid.
Do you have any advice?
levib
Star
7702 Points
1099 Posts
Microsoft
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 09, 2010 07:11 PM|LINK
[Required] should be winning over the default built-in messages. Can you show us your model? Also, which version of MVC are you running?
pikacc
0 Points
3 Posts
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 10, 2010 08:58 AM|LINK
I'm running ASP.NET MVC 2 RC2
Model looks like this
public class UserData_Validation { [Required(ErrorMessage = "Login missing")] public string Login { get; set; } [Required(ErrorMessage = "Email missing")] [Email(ErrorMessage = "Email invalid")] public string EmailAddress { get; set; } }[MetadataType(typeof(UserData_Validation))] public partial class UserData { }EmailAdress in edmx file looks like this (autogenerated property in autogenerated class)[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)] [global::System.Runtime.Serialization.DataMemberAttribute()] public string EmailAddress { get { return this._EmailAddress; } set { this.OnEmailAddressChanging(value); this.ReportPropertyChanging("EmailAddress"); this._EmailAddress = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false); this.ReportPropertyChanged("EmailAddress"); this.OnEmailAddressChanged(); } }Note: I need EmailAdress in database to be not nullablebradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 10, 2010 02:49 PM|LINK
I think you're hitting a bug w/ EF that we fixed for RTM. The EF property setter is throwing when you try to set it to null, thus the generic error message.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 10, 2010 04:57 PM|LINK
Can you show your EmailAttribute? More specifically, what's the result of calling the following line from elsewhere in your application?
var x = new EmailAttribute() { ErrorMessage = "Email invalid" };pikacc
0 Points
3 Posts
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 11, 2010 08:59 AM|LINK
It's not only email, same error is with login property
public class EmailAttribute : RegularExpressionAttribute { public EmailAttribute() : base(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") { } }Thanks for answers.
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: How to disable ModelState validation messages (The value '' is invalid.)?
Mar 13, 2010 04:21 AM|LINK
Please try the following:
You know the old cliche, you say you've got a problem to solve with RegEx, now you've got two problems.