Enable/Disable validators/attributes conditionally - c#

I'm doing some work in ASP.Net MVC(First work. Not expert in jquery). Until now [Required/Rage etc.] attribute was enough for completing my requirement. Now I need to return Required attributing conditionally. I checked lots of posts like this
I gone through IValidatableObject but it's not returning the javascript automatically. Then i went through requiredif it needs a lot of work to implement(all the scripts we only need to generate)
So is there a simple way like enabling/disabling required/Range etc attributes conditionally(on a switch)?
Editing
In my project I have multiple tabs(some will be invisible). I'm getting validation errors of the invisible tabs. So I'm planning to give validation if the visibility of the tab is true

Related

Two "forms" in one <form> clash their required attributes

The issue:
I have a single page with two email forms (one <form>, two divs inside this which look like forms to the user). When you submit either form, the C# behind looks at the values of the form and sends them in an email. This works well, and now I want a little validation, but I can't add required attributes to the inputs as the code doesn't know it's two separate "forms" and needs to handle the required attributes separately for each one.
I understand why this issue occurs, but I want to know if there's a way to tell the page to handle the required attributes in groups.
What I've tried: Both "forms" are handled by the code behind, and so need to be run server-side, so separate <form> elements wouldn't work. I have tried nesting the "forms" as <form> elements inside the server-side <form>, which separates the required attributes as desired, but seemingly breaks a number of things and I've read this is generally bad practice anyway.
What I'm not asking: To validate the form in the code behind.
Edit:
David's answer works great to validate groups of textboxes, which is what I needed.
To additionally validate a checkbox, I used javascript. onclientclick we can check if the checkbox is checked AND validate using Page_ClientValidate('validationgroup') and then return true, else return false. onclick is only fired when onclientclick returns true.
use one form with two submit buttons. Use validationgroup to distinguish
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
if you write your own validation function for each submit button it should be possible
<input type='submit' onclick='return validateFirstButton()'/>
To validate checkboxes follow the instructions here:
http://codeclimber.net.nz/archive/2007/07/19/How-to-add-a-required-validator-to-a-CheckBoxList.aspx
Because this derives from BaseValidator you will be able to use groups. Hope this helps

Sitecore Validation

This seems like a really simple task to accomplish, but I can't seem to figure it out. How do you assign validation to a particular field in Sitecore?
I can see the validation rules listed in /sitecore/system/Settings/Validation Rules/Field Rules/.
How do I assign a rule from here to a field on a template?
I've seen several blog posts about creating a custom OnSave action that evaluations the item and it's fields - that you then hook up in the web config - but that is way overkill for what I'm trying to accomplish. None of the blog posts or pdfs from Sitecore itself seem to show how to set up really simple validation (or I can't find them).
For some basic validation you can browse to your template, select the field itself (the field item underneath the template, I mean) and browse to the Validation Rules section.
There is also another simple way (did not check if it stil exists in Sitecore 7).
Go to the template field itself, as Trayek says, and therer you have 2 fields: "Validation" and "ValidationText".
In Validation you enter the regex for the validation and in ValidationText you enter the text to display.
Just set validation rule Item id in Suppressed Validation Rules field.
First check the "Standard Fields" checkbox from "view" tab of top ribbon and go on data template of selected item, select field for validation, on this field you can find Validation Rules bar here you can apply validation.
I would not just dump a regex in the validation field. Creating a rule and assigning it on a template affords you a lot more flexability

Filtering Pane, ASP MVC 3

My plan is to create a a two-pane page using ASP MVC 3. The left pane should be a small filter pane and the right the main content, showing a list of objects (say products).
Initially all products will be shown since no filter is applied. When selecting "only red", only red products are shown in the list. When further selecting a price range, only products in that price range will be shown.
Functionally the plan is to implement the filtering pane as a treeview with checkboxes (to be able to drill down to more and more specific filtering options), graphically it will probably be enhanced in some way to improve usability.
What is the best way to implement the coupling between the filter pane and the main list? Everything should work server side, but should of course use javascript (jQuery) when possible for direct feedback.
The simplest way is probably to make it closely coupled solution, calling a specific Asp MVC action using a custom-built javascript (with fallback to a form post). Doable enough, sure, but how to make the solution reusable? Also it would be nice to not loose all filtering data when navigating forward and back, i suppose GET arguments is the only decent way to do that?
Are there any best practices, any guidelines or anything to base this on to make a nice modular structure for filtering.
Victor, I recently had to solved this same problem. I'm not promising it's the best way but it's pretty clear and should even work well in case JavaScript is disabled (who even does that anymore?).
Create a that calls the action with all the field-selectable search options like "only red".
To that same form, add empty, hidden value for the things not directly as fields (paging, sorting...)
Setup your form with the excellent and very easy to use JQuery.Forms (http://www.malsup.com/jquery/form/) to make you form submit via JQuery (all your form values will be passed as JSON on form submit).
Make your back/next/paging/sorting links pass their individual values via query (no-JS fallback) and use JQuery to capture their click events. The JQuery click events on those links should assign the value of the value passed by the link (page number, sort column name...) to the corresponding hidden field in the form and call submit (with thanks to Jquery.Forms will submit via AJAX).
When you configure JQuery.Forms, you can define a callback method. In the callback method take the result (the HTML returned by your action that should contained your filtered+sorted+paged result) and replace the document (or DIV if you used a partial action + view) with that.
The result is a JQuery solution that works when JS is off. You also have very minimal JS code to write other than wiring the event handlers.
I'm not sure if it will be helpful but in MVC 3 you have access to a property called IsAjax from the view so you can do specific things to server a slightly different view when called from AJAX.
Cheers

Ideas - storing jquery parameters for later use in partial view

Ok, let me try to clearly explain what I'm attempting to accomplish here.
Basically, I have a site that is using a liberal dose of jquery to retrieve partialviews into a consolidated 'single view'. So far so good - it all works great and is very performant.
However, I would like to have the ability to 'flag' (using a button) any such set and as a consequence of flagging it, add it to a functional area that I have dubbed 'active-tasks'. What I'd like to do is to be able to then goto that 'active-tasks' panel and see a range of ui tabs that represented the consolidated views that I had added. Clicking on any tab would then re-invoke that consolodated view afresh with the parameters that had been used at the time of flagging it. This would therefore mean that I'd have to store the parameters (?) for creating that consolidated view, rather than the generated html (this part i can do at the moment).
So, any thoughts on how to elegantly store the code required to generate the consolidated view on clicking a tab button - no pressure :)
cheers - jim
Actually, after a minimal amount of research, it looks like the newly updated .data() jquery method (with the ability to add an object to the payload) may work for the above.
http://api.jquery.com/data/
basically, this allows you to add hash type keyed data to an id element for use later, so in my scenario above i could simply attach the parameters required to invoke the action method that related to my consolidated view on the tab.
I'll let you know how i progress with this...
jim

Change CSS class of validated control?

Is it possible to change the CSS class of the validated control if it's not valid without codebehind or just with less code ex. Set automatic CSS class to "invalid" of all textboxes which have a required field validator?...
I don't like to make a check for each validator (isvalid) I have, manually.
Thank you and best regards
What you need is client side validation.
JQuery is perfect for that. you can use this nice tutorial:
http://www.webreference.com/programming/javascript/jquery/form_validation/
or use a plugin:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
one way or another - no need to run to the server and back, and you have full control of the validation process and its outcomes (so you can change the css accordingly)
Enjoy!

Categories