MVC server side validation conflicts with client side validation - asp.net-mvc

With ASP.NET MVC 5, how can one mix server side business rules and logic with client side basic validation? Specifically, using #Html.ValidationMessageFor()?
Here's a common scenario; imagine a login form with username + password. The username has to be an email address, and is of course required. So you have a model with Data Annotations of [Required] and [EmailAddress] on the username field, but when you submit your form to the controller, you check to see if that email address is a valid user/email address registered at your site. If it is not, you might do something like:
ModelState.AddModelError( "Username", "This username isn't registered at this site.")
Unfortunately, when that goes back to the view, client side unobtrusive validation sees that your field has a valid email address in it, so it removes your custom server side supplied error at page load! (Evidently MS' implementation of jquery validate does an automatic validation on page load.)
This seems to create a conflict with a very common scenario of additional server side validation rules... and due to client design requirements, we can't simply put the message into a ValidationSummary -- so please, don't suggest that. We need the messages to appear in their place under the fields themselves, where the other normal 'required' and 'must be an email address' validation messages would appear.
Finally, this is just one example of a business logic validation rule; there are more -- custom validation data attributes are likewise not a valid option for everything, and neither is remove (ajax) validation!
Seems like a fairly significant oversight?

Related

Is there any way to keep validate field optional in rails?

I have User model which includes 7 fields. for all these fields validation is written.i have two form where i am displaying fields depend on condition. in one form i have name password and city and other form i have role,phone and name.
When i try to submit the first form i got the error which says phone and role field are required resulting into failure of form.
Is there any way by which i can submit both form without getting the validation errors ??
Note : i want my logic to be in model only.. Please help me with this problem.
You could use a conditional validation to achieve what you want:
See here: http://guides.rubyonrails.org/active_record_validations.html#conditional-validation
However, this can quickly get hard to manage. Depending on the condition you're switching on, it'd probably be a cleaner design to use a 'Form Object' which will give you more control and let you do validations without the messy conditional logic.
See section #3 of this blog post for more detail:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Using this pattern, you would check for your condition in the controller then determine which form object to send to the view.

ZF2 Validation - Field required if another element is not empty

I've had experience with making custom validation in Zend Framework 2 (using version 2.0.5 at present). I'm interested in creating a change password section in an "edit profile" form. What I want to be able to do, is have 3 fields:
Current Password,
New Password,
Confirm New Password.
Then I want to validate as follows:
If a new password is set, current password must also be set (and authenticated), and confirm new password should match new password.
If a current password is set, the new password and confirm new password must also be required.
If none are set, allow the edit of the rest of the profile, so continue validation.
I think you can get the gist of what I'm after, I'm looking for a reusable way to do this using Zend Framework 2. Ideally, creating a custom validator so that the forms can be reused, thought I suspect a factory approach may be better. Anything so I don't have to check it in the controller/service layer and repeat myself wherever I want to use this.
Kind Regards,
ise
You could add a custom filter, but I think this could also be done using validation groups and separately validate groups of inputs.
you could add the password fields to a separate group and only validate that group if the main password is not empty.
http://framework.zend.com/manual/2.0/en/modules/zend.input-filter.intro.html
http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html#validation-groups-for-fieldsets-and-collection

How do i filter and validate form fields in symfony 1.4?

Im trying to integrate a content filtering API. My plan was to use pre/post validators but I've lost may way somehow.
What i need to do is send the values to the content filtering service. If the response comes back that the content has been filtered it will also return a modified value for the field (basic profanity filtering... matches are replace with asterisks). Thats all well and good i can throw validation errors no problem - simple stuff.
However i dont want just throw errors. What needs to happen is that validation errors are thrown as normal, but the values are modified in the form for re-display.
Basically if someone posts something naughty i want them to get a validation error saying their post has been modified, they can re-submit the now "clean" post, or they can go about editing it to make it clean without the word replacements.
But do clean on a validator either throws an error OR returns cleaned values, not both. How can i go about implementing both? This will be used on many different forms with many different field names, so modifying methods on the form or a form base class isnt really an option - it needs to happen in the validation sub-framework somehow.
You can adjust this plugin for your needs http://www.symfony-project.org/plugins/WebPurifyPlugin

how to return url in form file in strut

hai all,
i have one problem regarding to return the form file value is validation fail. i have one form that have a few field to be fullfil by the user and one attachment field.if one of the field is blank the system will give the error when submit the form..my problem is, all the field will have the value that we entered before this but for form file it disappear.
Let's see if I understand your issue: You have a form with several input fields, and among them a <input type=file> for uploading some file content. In case of validation error, you return to that page (with struts2 result = INPUT, I guess) and the content of the previously filled fields appears, except for the FILE field.
This makes sense, if you understand how the other fields are "refilled" in Struts2 in this scenario (just from the action, which has typically mapped the parameters to properties). The server doesnt know the (clientside) fullpath of the file, it is not sent along the http request (it would be a privacy issue), it's the content of the file what is uploaded (and perhaps the filename without path). You can't escape that.
Anyway, I think this a case in which validation should signal an error early (in javascript, in the client side). Think about it: the user is uploading a file (potentially large) together with other info, and AFTER uploading it the server checks the fields and instructs the user to refill the fields and retry (including the upload). This can be unacceptable. My advise is, then, to include client-side validation (and if it pass, and the server validation fails, then the user must resign to refill the FILE field). If the file is large, one could split the input form in two pages, a first one to fill the several fields, and afterwards another to upload the file.

.NET MVC Multiple Versions of Same form on One View

I have a section of my site where users can add addresses to their account. They may add as many as they need (shipping, billing, etc).
I set things up so that after an address is added, the users sees the address in an update form with a "save" and "delete" button. The user can adjust any of the addresses they have added.
The problem I am having is with validation. Let's say Line 1 is required. If I am updating the second of three addresses and leave Line 1 empty the controller is raising an error (using the same technique from Nerd Dinner, BTW). This is good. What is bad is that all of the address info on all of the addresses listed in the view, now show as values from the address where the error was raised.
I know this has something to do with model binding, but I am confused, as the form data is set up as follows:
<%= Html.TextBox("Line1", Model.Address.Line1)%>
The Model that is passed in is unique to the address we are on in the list of client addresses. I am not sure why the value in "Model.Addres.Line1" is being overridden by data in the ModelState ModelErrors Collection. I guess the default behavior is to use the values from the errors collection when they are present. This is a problem when there is more than one form on the View and the form is using the same Names for input fields as each of the other forms.
Is my only work-around to avoind the Html Helper function here and hard-code the inputs in HTML?
Correct me if you're wrong, but it sounds like all 3 addresses will have the same field names in the same form? If so, you've got bigger problems than just the validation message.
Regardless (even if you're using separate forms), I would suggest you preface the field names with the address ID:
<%= Html.TextBox("Address" + Model.Address.ID + ".Line1", Model.Address.Line1)%>
You won't be able to automatically bind (in the method parameters), but you can use UpdateModel() and set the prefix to
"Address" + Address.ID
I solved this by getting rid of the html helper methods on the form(s). If someone sees a means of keeping the helper methods please let me know.

Resources