Asp.net 4 Gridview Gridview Validation - asp.net-mvc

I have some validation controls on my gridview and would like them to anly be active when I try to save/update data in a gridview row and not when cancelling using the cancel button. How can I implement this.
Thanks in advance

Set CausesValidation="False" for the Cancel Button.
Control.CausesValidation Property
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
Also, specify "ValidationGroup" for your controls:
Validation groups allow you to organize validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.
You create a validation group by setting the ValidationGroup property to the same name (a string) for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.
During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group.
Reference Links:
Control.CausesValidation Property
Specifying Validation Groups

Related

Is there a way I can simply use a data annotation attribute to add a JavaScript attribute to a form field?

I would like to add a data-other-for attribute to a text input, to link it to a select, so that it can be used to capture a value not present in the select when the user selects 'Other' in the select. The attribute's code will determine which value or description is in fact 'Other', and if so, enable the text input and maybe make it mandatory.
It seems like the only way to do this is by creating a new helper, because going via a ValidationAttribute I can only add preset validation HTML attributes to my text input. Or go large and write a whole new metadata provider.
You could try to implement a custom ModelBinder.
Say, in the select you would have:
new SelectListItem(Text = "Other", Value="bind:propertyName", Selected = False);
Then in the overriden BindModel, you simply look for bind: in the model properties and when found, copy your value from there.
After this, you should be able to add normal validation attributes to your select list.

MVC Razor Conditional Validation with Data Annotation on drop down and radio button

I am new to MVC Razor. I have to implement conditional validation on a Dropdown selection and Radio button (using data annotations). I have two conditions:
My dropdown contains three values: yes, no, and none. If the user select "none" then only required validation should be applied on the remaining field like city and state.
I have two radio buttons, yes and no. If the user selects no, then only required validation should be applied on remaining field like zipcode.
I have found many solutions, but none are ideal. If you are a master of MVC Razor please provide me demo sample zip whatever I have explained above only.
For cases with "inter-properties" validation, DataAnnotation is usually not the best (or clear, or easy to implement) tool.
Take a look at FluentValidation
http://fluentvalidation.codeplex.com/
And you will be able to write rules like (pseudoCode)
RuleFor(m => m.City)
.NotNull()
.When(m => m.<mydropdownName> == Conditions.None)
.WithMessage("You must choose a city when you chosse none");

Why can't you add a EditorExit Handler to a DynamicForm or FormItem?

This handler only exist for a ListGrid.
But if you look at the docs for DynamicForm.setValidateOnExit(), it says:
If true, form items will be validated when each item's "editorExit"
handler is fired as well as when the entire form is submitted or
validated. Note that this property can also be set at the item
level to enable finer granularity validation in response to user
interaction - if true at either level, validation will occur on
editorExit.
So how can we add a EditorExitHandler to a DynamicForm or a FormItem?
EDIT :
I want to create an error panel below the form to show all errors dynamically. Each FormITem has the possibility to validate on Exit but I do not know how to capture this validation event to check if the error panel should be updated or not.
There is one method form.getErrors() and form.showError(true). By this you can acheive that. But for that also you need to setValidator for each field.
TextItem name = new TextItem("name", "Name");
name.setRequired(true);
name.setRequiredMessage("Please specify name of the Table");
NTRegExpValidator nameValidator = new NTRegExpValidator("(^[a-zA-Z0-9][\\w\\s.()_-]+)$","It should start with alphabets and can have alphanumeric values ( )_-. and space.");
name.setValidators(nameValidator);
name.addKeyUpFieldHandler(new KeyUpHandler){
form.getErrors();
form.showErrror(true);
});
DynamicForm form = new DynamicForm();
form.setField(name);
After some research, I still don't find a convincing answer. I guess it must a dev requirement

How to persist JSF view parameters through validation

I am using JSF 2.0/CDI and PrimeFaces 2.2.1 for a number of CRUD forms that let the user view or update the attributes of an existing entity by clicking on a link in a datatable, where the identifier of the entity is passed to the CRUD form as a View Parameter. I display the entity's ID (often just an integer) on the CRUD form in a PrimeFaces InputText field with the readonly attribute set to true (since I can't let them change it), so the user knows which entity they're editing. The backing bean of the CRUD form is RequestScoped, which works fine except when validation fails. In that case, the value of the View Parameter is lost, so a 0 is displayed in the entity ID field on validation failure.
I am able to maintain the actual entity ID in a hidden field so it's available to update the database once validation succeeds, but it's rather maddening that I've not been able to find a way to maintain the value in a visible field of some sort after a validation failure. Ideally the InputText field would retain its functionality as an inputted and validated field even with its readonly (or disabled) attribute set to true, which would let me forgo the hidden field entirely. But it doesn't appear that I can make it work that way. Any suggestions besides making the backing bean ConversationScoped, which I'd prefer to avoid?
Actually, after stating what I'm looking for a little differently in a Google search I found a novel suggestion at the link below that seems to work cleanly. Instead of making the entity ID field readonly or disabled, I leave it enabled but blur it as soon as it receives focus. I'm able to get rid of the hidden field, the user can't change the value and it survives a validation failure.
<p:inputText id="entid" value="#{RequestBean.entityID}" onfocus="blur();" />
http://www.codingforums.com/archive/index.php/t-1738.html

Using a dynamic list of checkboxes in a view, how to create the model

I have an asp mvc 2 app lication where I want to display a list of check boxes that a user can select, based on a list of records in a database. To display the list my model contains a List object and the view has a foreach, and outputs Html.CheckBox for each item in the list.
Is there a way to get the model populated with the selected checkboxes, given that the model can't have specific properties for each checkbox, because the list is dynamic? Or do I have to manually iterate through the forms variables myself?
Edit: Extra details as per sabanito's comment
So in a simple view/model scenario, if my model had a property called Property1, then my view outputted a Textbox for Property1, when the form is posted via a submit button, the mvc framework will automatically populate a model with Property1 containing the text that was entered into the textbox and pass that model to the Controllers action.
Because I am dealing with a dynamic list of options the user could check, I can't write explicit boolean properties in my model and explicitly create the checkboxes in my view. Given that my list is dynamic, I'm wondering if there are ways to create my model and view so that the mvc framework is able to populate the model correctly when the form is posted.
Here's what I would do:
Are you having any issues generating the checkbox's dynamically?
If not, create a property on your ViewModel that is a:
public List<string> CheckboxResults { get; set; }
When you generate your checkbox's in the view make sure they all share the name = "CheckboxResults". When MVC see's your ViewModel as a parameter on the action method it will automatically bind and put all the "CheckboxResults" results in the List (as well as your other ViewModel properties). Now you have a dynamic List based on which checkbox's your user checked that you can send to your DomainModel or wherever.
Pretty cool stuff. Let me know if you're having issues generating the checkbox's dynamically, that's kind of a seperate issue than model binding to a list.
Use a ViewModel that reflects your view exactly, and map your domain model(s) to the viewmodel.
At first it often seems appropriate to use domain models directly in the view, for no better reason than that they're simple to use. However, as the view gets more complex over time, you end up putting a TON of conditional logic in your view, and end up with spaghetti. To alleviate this, we typically create a ViewModel that correlates 1:1 with the view.

Resources