How to make model field conditionally required in MVC3? - asp.net-mvc-3

I have a view with a dropdown and a Rich Textbox. This view is associated with a model. I would like to make Rich Textbox field is required based on the value selected in dropdownlist.
is there any out of the box feature available in MVC3 to do this?

I have had similar requirements in the past, I solved them using FoolProof. It provides extra validation objects such as requiredIf etc.
Only issue I have had is with the JS file, it can be a bit buggy around dates and date handling, other than that, it is ace.
Foolproof site

Related

Sitecore WFFM make 1 of 2 fields required

I'm trying to create a WFFM form and I need to set one of two fields to be required. Do I need to create custom fields? Or is this something that I can achieve with the module itself?
You could possibly use the Morph module, which you can download from the Sitecore Marketplace.
From it's description:
Selection-dependent inputs require users to enter additional
information related to an initial selection before they can complete a
form. In almost all cases, it allows making web forms simpler and more
responsive to user actions and personalizing forms depending on
visitors’ features.
If memory serves it uses the Sitecore Rules Engine to decide which fields to show/ hide based on inputs. However, I'm not 100% sure whether you can use the Required checkbox there, but worth a try.
No custom field types are needed. The WFFM Form Designer has a Required checkbox for each field row and most standard fields support this along with a custom validation message.

Which dojo widget should be used for DropDown

I wish to create dynamic dropdown, meaning the value of the second dropdown changes with the change in selection on the first. I was looking through the Dojo docs and it seems there are 3 different widgets that I can use,
dijit/form/ComboBox
dojox/form/DropDownSelect
dijit.form.Select
Now I am confused as to which one should i use for creating Dynamic DropDowns?
You can choose anyone depending upon what extra features you want. While dijit.form.Select is your normal HTML select, combobox and filteringselect offer more features.
Follow http://kennethfranqueiro.com/2010/06/combobox-vs-filteringselect/ for a comparison between the two. You can also play with them to know how they work.
I had used FilteringSelect in my app for the same behavior as need.
Differences between dojo dropdown :
Select It is simple combobox like select in HTML with no validation
and not provide any search facility inside select options.
ComboBox It is pure form of combobox and name as ComboBox again it
will not provide any default validation but it provide search
facility within its options.
FilteringSelect It is an advance form of select have default
facility of validation and search facility. And it also has property
to take value as input tag take value in HTML.
In dojo you can also try custom validation which is provided inside dojox library. I hope it will help you.

MVC3 why use html.editorfor

I am looking at MVC3 razor examples and seeing html.editorfor being utilized and also asked about a lot on this forum. Why can't i use html.textboxfor and passwordfor? Thanks
EditorFor has the advantage that it will try to render an editor associated to the data type.
For example: If you design your own Editor Templates they will automatically be rendered based upon the property's type or UIHint
A useful editor template might be one that generates a date picker when the property's type is a DateTime.
There are some other scenarios as well where the 'smart' EditorFor will generate the 'best' editor for the property such an example is when it renders a <textarea> when tagging the property with MultilineText
Using TextBoxFor and PasswordFor are perfectly fine for those cases where you don't require 'a special editor'. It might even simplify your life when having to set HtmlAttributes.
Ref Differences between Html.TextboxFor and Html.EditorFor in MVC and Razor for clear the doubt about this..
The HtmlTextboxFor always creates a textbox (<input type="text" ...).
While the EditorFor looks at the type and meta information, and can
render another control or a template you supply.
For example for DateTime properties you can create a template that
uses the jQuery DatePicker.
if you decide to change something to the aspect of how your textboxes are rendered like wrapping them in a div you could simply write a custom editor template (~/Views/Shared/EditorTemplates/string.cshtml) and all your textboxes in your application will automatically benefit from this change whereas if you have hardcoded Html.TextBoxFor you will have to modify it everywhere.

Integrating Ext.grid.panel validation and Ext.data.Model.validations

I've been learning ExtJS4 after having done quite a bit of dev in ExtJS3. I'm quite intrigued by the new class Ext.data.Models, but I would love to integrate these validations with the validation function in Ext.grid.Panel.
Can anyone point me in the direction of any examples of using the validations property of Ext.data.Model in a Grid panel?
I've tried adding the validations to the model and putting invalid values in the grid, but it doesn't seem to throw an errors or the normal red lines.
Any ideas?
Model validation against grid data is not supported out of the box currently.
Here is a working extension for model validation against form fields though.
And here is an incomplete attempt for model validation against a grid (what you were going for).
#Drew
The grid provides RowEditing and CellEditing plugins for row/cell editing. In the background these plugins use Form panel for the validation of the input. So, you can use the form panel extension that #Geronimo has mentioned along with the extensions of RowEditing and CellEditing classes and use them in your grid to validate the data entered in the grid against the model associated with the row. And since, the validate() method is on a model, which can be used to validate a complete row data or a particular cell data. In case you are looking for bulk validation, you can override the sync() method of the Ext.data.Store class to achieve that.

MVC3 selectively validate client side

Each form in the application has a set of radiobuttons. Once selected, only certain fields associated with that radiobutton will need to be validated.
I am using MVC 3 and need the validation to work client side.
Simply using DataAnnotations I can only validate all fields on the form.
IValidatableObject doesn't work clientside.
IClientValidatable looks like it might do the job, but it seems I would have to write a new attribute for every standard DataAnnotation attribute.
RemoteValidation works with one field at a time.
Another option would be to drop MVC3 validation and do it all using jQuery. I don't have a problem with this as such but would like to use MVC3 and reduce coding/maintenance in preparation for a much larger project.
Could I still use MVC3 validation but then use jQuery to add/remove validation fields from validation whenever a radiobutton is selected?
If anyone can help with some suggestions as to the best way to approach this, it would be much appreciated.
MVC 3 uses jQuery's validation plug-in by default and that plug-in will not validate disabled fields. Are the fields that you don't want to validate no longer needed if certain radio buttons are selected? If so, then you can just disable those elements and they won't be validated (and note that those disabled fields won't be posted to the server either).
e.g.
$('input').attr('disabled', 'disabled');
For complex validation it is best to hand code these.
Data Annotations work great for 90% of your validation needs, but fail dismally with What/If scenarios.
For the client side use an event driven custom validation presented via jQuery Validation Plugin. For the server, use the CustomValidation attribute:
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(v=vs.95).aspx
Using IClientValidatable is great if you have reusable custom validation, however it is wasted time for one off validations.
Alternatively use RemotValidation with a CustomValidation attribute that invalidates multiple fields.

Resources