How to show validation messages in MVC? - model-view-controller

When a user tries to click:
Save
and they have entered in some invalid data, i want to notify them. This can be with methods such as:
directing their attention to the thing that needs their attention with a balloon hint
automatically dropping down a combo-box
triggering an animation
showing a modal dialog box
etc
What is the mechanism where a controller tells the view to show a validation message for some controls, given that different views have different notification methods?
p.s. the controller doesn't know the order that controls are physically arranged in the view (e.g. LTR locale wants to notify the user in a top-down-left-to-right visual order, while RTL locale wants to notify the user in a bottom-up-right-to-left order)

The controller can add a Validation object to the Model, which can contain the names of the fields which were invalid, specific validation messages, etc.
The View can then choose to render this Validation object however it wishes: by highlighting the incorrect fields, animating something, showing a modal dialog, etc.

If you are using ASP.Net MVC 2.0, check out this post by Scott Gu, although some of this will apply for ASP.Net MVC 1.0 as well.

Related

How to Handle Mutiple Model Bound Forms

I am buiding a UI screen for editing the details of an Ecommerce Order. The model for my view (OrderModel) has everything I need (in properties that are also ViewModels), but the UI isn't designed to be able to edit all of it at once.
For example, one part of the UI is for customer data..another for order details, and another for tracking information, each having their own "Save" buttons.
I realize that I could use one giant form and use hidden form fields to populate the non-editable fields, making each "Save" button post all the data, but that smells bad.
I'd like to segment the editable chunks into smaller ViewModels that are posted and validated individually while retaining the strong typing but I'm unsure of how to achieve this in MVC3. Will I need partial views that are called from the primary view?
FYI, I'm using ASP.NET MVC 3 with Razor syntax and client side FluentValidation.
Partial Views are a good solution. You can pass different ViewModels to each partial view. But if only sections of the overall view are updated at a time I would not do a post back on the whole page. Instead I would use Ajax calls using JQuery/Javascript to update the individual information back to the controller. I would also look into something like Knockout.js to handle the data binding on the page.

User-friendly message for when user enters html

Have an ASP.NET MVC3 app with model validation using FluentValidation. User enters some html in a regular text field, and is presented with a user-friendly error on submit (because of the "A potentially dangerous Request.Form value" error).
What I'd really like to do is show a validation message right on the form itself ("Oops, no html allowed") which shows up right next to the field - similar to model validation errors. That way, the user (malicious or not), knows about it before the entire form is actually filled out and submitted.
Would appreciate any ideas on how to go about implementing this apart from what's shown here (i.e. without having to add [AllowHtml] + a regex to almost every field in almost every viewmodel)
Thanks!
You will need to write a custom ProertyValidator for server-side FluentValidation with client-side support. I think you can follow this article, here, to create client-side jquery validation rule and the adapter.

MVC3: Showing entity relationships in a view

I am new to ASP.NET MVC and I have a question regarding viewing entity relationships.
Say I have an entity called 'Person'. This holds the usual data relating to a person (Name, Email, etc). I also have a 'Notes' entity. Under EF, a 'Person' can have many 'Notes'.
I have a Person controller where I can view and preform CRUD operations on a Person object.
I can show the notes in the view easily but what is the best way to allow a user to add/edit/delete these notes from the Person view? I am hoping to do this using AJAX and not have the user move to a completely different page to add/edit/delete a note.
Thanks in advance,
ViperMAN.
When they edit a note, popup a jQuery dialog pointing to your URL to edit or have a separate Ajax.BeginForm() on the page that the details go into. When they finish the edit call a method to refresh the notes.
So:
1. In your Notes grid (or whatever)
you have an edit link for each note called "edit"
this link looks something like the following:
This one actually uses 'notes' : )
http://www.iwantmymvc.com/dialog-form-with-jqueryui-and-mvc-3
ASP.NET MVC | Problem about showing modal dialog using jQuery dialog widget
ASP.NET MVC modal dialog/popup best practice
Also beware of this scenario for multiple links:
MVC3 - Only first row link works well with Jquery Modal Dialog
Now the urls you use to populate the dialogs would be for example
/Note/Edit/10
One thing to note - jQuery validation needs to know about these new items that are being loaded via ajax into the DOM , so in your partial view you need to tell jQuery validation to include the new items - I'll edit in a bit to add this, have to grab it from another machine.

What is difference between Rules and validaton in infopath?

I am working on Microsoft office infopath 2007.I have inserted the controls as text box and drop down. when i get right click to control there are 2 option links as Data validation and Rules. what are the main difference in between them? I am new in infopath so need guidance.
when to use rules and when to use validations. can we customize validation and rules? and can we define custom rules and validation in infopath ?
Data validation can be used to display error alerts when users enter incorrect values into a form. Rather than checking for errors after a form is completed, data validation verifies values as the form is being filled out.
You can use rules to automatically display a dialog box, set a field's value, query or submit to a data connection, switch views, or open or close a form in response to certain events and conditions.
I'm not sure that you can create your own custom rules and data validation. But infopath forms can be customized by writing programming code to respond to form and data validation events, to access and manipulate a form's underlying XML document, to implement custom data submission and merges, and to implement access to external data sources.

How to create an AJAXified form within the Zend Framework

I'm trying to create a contact form. However at the top of the form the user can select using radio buttons whether he's contacting the technical department or the marketing department. Depending on which he selects, the entire form changes.
How would this be implemented within the Zend Framework? I'm already extending Zend_Form to make my forms. Also I'm working within the MVC style and would rather not break out of it.
Right now I simply do
echo $this->form;
in the view to render the form. I'm guessing that when the visitor clicks on one of the radio buttons, the controller will need to set a different form, but I'm not too sure how to go about that without re-rendering the entire page.
Thanks!
EDIT
I'm now thinking just setting something like this in the controller:
$this->view->contactFormTechDep = $formTechDep;
$this->view->contactFormMarketingDep = $formMarketingDep;
and render both, but hiding one all using Javascript.
I think you just need to show/hide the content of the form with JavaScript, not with php.
(with jQuery this can easyli be done)
But you'll have to keep in mind to be unobtrusive for users without javascript enabled

Resources