This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Using the .net mvc framework I've got a form for a user to update their profile. In this profile they can upload a profile picture. But before I set the WebUser.ProfilePic value I make sure it's a valid image and resize it if need be. If it isn't valid I'm attempting to add an error to my entity on the fly but then after doing this WebUser.IsValid is true. My code looks similar to this: //Bind from the form So yeah, even if I do an .AddError, webUser.IsValid still equals true. I guess my question is, is it meant to behave like that? If so, should I instead do all the validation inside the model, i.e. setup a transient for the file content type or name to validate, or is there another way? I may have just answered my own question there... ;-) Thanks guys, |
|
|
Hi Charles, You need to do your validation from within your model class. Override the OnValidate() method and then add your errors. For example: protected override void OnValidate(){ if ((Contributor == null) && (ApprovedBy == null)) { Errors.AddError("Must have one or the other"); } } The reason you are running into issues is that when you call the IsValid property the validation logic is run which involves clearing the existing Errors collection and then adding items as required (hence you set an error and then lose it in your case). I hope that helps clarify what is happening here. John-Daniel Trask |
|
|
Hi JD, Yeah, I thought something along the lines of what your explained at the end would be happening - makes perfect sense. I really should have done the validation on the model to start with, much better idea, but because of the nature of what I was doing, I tried to sneak some validation in at the controller level. :-) Thanks, |
|