Validation of two sets of controls - c#

ASP.NET WebForms 4.51
Having read things like How to validate two groups of controls with 1 button and a validation group?
I have a form with the following logical inputs:
Name (TextBox)
Last Name (TextBox)
Profession (Drop Down List)
Profession has the following items:
Bus Driver
Taxi Driver
Not Applicable
Now based on the profession I display a number of other text fields. I have required field validators on all the fields 1..3 as well as the fields for the profession (4..8).
I have a single submit button which triggers the validation. So my question is what is the easiest way to validate controls: 1..3 should the selection be Not Applicable for profession and 1..8 for the other professions from a single button.
I can think of a couple of ways but they are all "messy" and I was looking for a better approach:
1) Have two required field validators per field - one when validating for Not Applicable and one for when a profession is selected. Toggle the validation group when the profession is selected for the button.
I tried this but it does not seem to work as I still get both validators firing per field.
2) Write custom client side validators that take into account what is selected in the drop down for profession.
A maintenance nightmare. I would prefer to use any built in functionality MS provides.
So has anyone got any suggestions on how to validate the same sets of controls differently based on what has been selected in another control?

You could write a simple javascript validation script. You can not validate the javascript on serverside using "page.Isvalid".
Your only option is to write a custom validator. I just don't know why that should be a maintenance nightmare. If you're using the javascript validation you should validate the values in the code behind anyway. Otherwise the user could just disable javascript and enter anything he wants.
Hope it helps...

Related

Orchard Custom Form DropDownLists

After a bit of playing around with Orchards' Custom Forms module, i decided i wanted to use a dropdownlist to select a particular person with their email as the value for that selected option. While i was creating the form i couldn't see anyway you could set values to your options.
See below image for example:
Don't suppose anybody has come across this before or has a suggestion?
In your case I wouldn't worry about having different text and values for the fields. It's also potentially dangerous to make the recipient email an input of the HTML form.
The Custom Form Rule Event provided with Orchard gives you no way to look at the values of the content type created by the form. So, you're probably going to have to write your own. You should be able to base this on Orchard.CustomForms.Rules.CustomFormEvents.
Armed with this you'd be able to create new rules for each possible dropdown value and set the email address in the action for each rule.

Why do only the first two validators trigger on my ASP.NET C# website?

I have a form that asks a user for their first name, last name, phone number, ID number, username, and check boxes to choose either faculty or student in that order. The first two text boxes have two validators each, a regular expression: ^[a-zA-Z- ]*$ and required field validator. After that I created some custom validators as in addition to the text box for phone number, ID number, and username I have a "Don't know?" checkbox in case they forgot. So it validates to make sure they either typed in something and if not to make sure the check box is checked. Now, if I fire up the site and simply click the confirm button only the first two fields trigger their validation, that is the first and last name fields. Now, if I put in a first and last name and click confirm button again the rest of the validators trigger as normal. How can I get them to all trigger at once? Thanks.
Your custom validator logic may be implemented only in server side. You may have to provide the validation logic at client side by writing a javascript method and specifying it in ClientValidationFunction property. The link also provides an example on how to implement it in the client side.

Disable mvc3 client-side validations under certain conditions

I am building a search form which needs multiple fields. A radio button indicates which fields are required for input like so :
[ ] Field 1
[.] Field 2
Field 3
[ ] Field 4
In the above case, Field 2 and Field 3 are now required since the associated radio button is checked. To accomplish this, I have implemented the RequiredIf validation attribute, and it works properly.
My problem are the other validations. In this case, Field 1 also has a minimum length validation. If Field 1 has any value that does not respect the minimum length validation, the form is now invalid and cannot be submitted.
I need a way to disable validation on the fields that are not required. (And also be able to set them back as another radio button is checked).
The fields cannot be set to "disabled=disabled" which solves the issue because of client requirements.
I have tried removing the data-val attributes or setting them to false on the said fields, then parsing my form again, failing miserably.
Edit: Just making sure. The problem is client-side validations.
Remember that there are 2 validations happening: client and server side. Hence, removing the data-val attribute will not help.
Now, in your models I reckon you are using [attributes] to add these validation rules. I don't think this method will not let you do conditional validations.
In this case FluentValidation can help you. http://fluentvalidation.codeplex.com/
It is quite simple to do, and you should be able to do something like:
RuleFor(model => model.Field).NotEmpty().When(model => model.FieldEnabled);
Setting the fields as "disabled" works as intended. The client has changed his mind about this requirement. Sometimes it is the best solution.
This is still open for better solutions.
Edit :
I had not thought about searching for doc on the validation plugin. There seems to be some pretty interesting methods available to use like rules ( "remove", rules ).

How to validate non entity fields but just textboxes. Also add dynamic validations

I am new to Silverlight and want to achieve validations.
I have a form with accordion control. I have controls in each section and have to have validations like Required or optional, if given a value it has to be valid like emai, phone number..... All these validations i have Dynamically enable or disable at page load.
So How i can achieve this in Silverlight 4.
In simple words how can we have Required field validator, CUstomvalidator and range validators in SilverLight.
I have gone through RIA but i am not sure how to use that on non entity fields and also Dynamically enable or disable validations.
Example i have txtFirstName, txtLastName and txtPhone. I want firstname required and Phone optional but should be validated if given some value.
All these fields are not part of any entity. Where i can add required or other attributes.
Thanks in advance.
You can add context to the validation process and allow your custom validators to be aware of some state. Fore example, you can add your page to the context and the validator can later access it to perform some conditional logic. See "RIA Services Validation: Providing ValidationContext" by Jeff Handley
How Silverlight Validation Works by Shawn Wildermuth.

InfoPath repeating table validation

I've got several repeating tables sitting in a InfoPath form. Essentially each one lists a load of strings with checkboxes next to them. When the user clicks submit I want to ensure that at least one checkbox per repeating table has been ticked.
I was just in the middle of writing a validating event handler for the table which would set a validation variable to either true or false but I realised that the main data source is read-only on validating and changed events.
How would I go about checking that something has been ticked in the repeating table before the form is submitted?
[NOTE] I'm using repeating tables instead of the multiselection control because the form has to be browser compatible.
Thanks in advance
I think you have two options (and these are just off the top of my head / not actually building a similar form solution).
1) Build an XPath expression that will evaluate whether or not one of your checkboxes has been selected. Than add a hidden node to your form (one not bound to a control in thew view), and add this validation property to it.
2) Continue writing your event handler, to cancel the submit event after running your custom validation logic in code.
http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.formevents.submit(VS.80).aspx

Categories

Resources