views:

75

answers:

1

I'm using the jQuery validation plugin. On most of my input type... tags I have class='required'.

When I submit the page, via JavaScript, the controls on the page that have this class are found. However, there are a handful of checkboxes that I don't need to validate.

I've tried removing the class code completely from the input tag, also tried class='cancel', and class='required:false.

When doing any of those things though when the form submits it can't find the checkbox control.

How do I still keep the ability to do Request.Form and find my checkbox object but at the same time when the form submits don't apply validation to this particular control.

Thank you.


Edit here.

This is what I'm using without the "checked" code and ternary operator. In my input tag I'm calling a function like this...

sb.Append("     <td><input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' " + strDisabled + " value='true' " + GetPackagingSizeTypeControlValue("chkFlashedCarton") + "  />" + crlf);

Inside that function is where I check for the True or False coming back, like this.

case "chkFlashedCarton":
                strResultValue = pst.FlashedCarton.ToString();
                if (strResultValue == "True")
                {
                    strResultValue = " checked";
                }
                break;

strResultValue is what is returned back.

Does this help to see? Thank you.

+1  A: 

I don't think the checkbox not appearing is related to the validation issue. By default, inputs without values are not posted back with the form. One way around that for checkboxes is to have an hidden form field per checkbox that sets the opposite value with the same name as the checkbox. When the form is posted back with the checkbox checked, you'll get both values. If the checkbox isn't checked, you'll get the default value (from the hidden field). Thus, you only need to check if the value for the checkbox contains the non-default value and act appropriately on the server side.

<input type='checkbox' name='cb1' value='true' /> Check Me
<input type='hidden' name='cb1' value='false' />

You can then omit the required class and be assured that you will always get some value for the checkbox.

On the server side, then you do something like:

 bool cb1Flag = false;
 if (Request.Form["cb1"].ToUpper().Contains("TRUE"))
 {
      cb1Flag = true;
 }

Edit (based on your edit)

Try this:

sb.Append("     <td><input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' " + strDisabled + " value='true' " );
if (pst.FlashedCarton)
{
    sb.Append( " checked='checked'" );
}
sb.Append( "  />" + crlf);
tvanfosson
tvanfosson, this is exactly the help I needed here. Thank you very much for your time in replying and showing an example, very helpful. Thanks.
d3020
tvanfosson, may I ask you, or others here, a follow up to this?Since were setting the value='true' how would I go about setting that value dynamically? In other words, when the page loads I need to have it checked, or not, based upon a condition. In other controls where the value is not already being set to true I do it like - value='" + CallMyFunction() + "'. How would it work here though since the value is already being set to true?
d3020
Let assume that you're using ASP.NET (since you talk about Request.Form). In an MVC world, you'd base it on the model -- and probably use the HtmlHelper -- `<%= Html.CheckboxFor( model => model.CBProperty ) %>`. In a standard WebForms app, you could use binding (if in the context of a bound control) or expose a property on the page and get the value that way `<input type="checkbox" name="cb1" value='<%= this.CB1Flag ? "true" : "false" %>' />`
tvanfosson
I am using ASP.NET and it is a standard WebForms app. I'm writing out the checkbox via StringBuilder like this. Here is what I have now...sb.Append(" <td><input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' " + strDisabled + " value='true' />" + crlf);Can you clarify me how that value property would change to be true and call a function as well, please?
d3020
@d3020 - value='" + chkFlashedCarton.ToString() + "' />" -- or whatever holds the actual, current value.
tvanfosson
I can get the "True" back from the database if I do like you're saying. value - '" + CallFunction + "'. However, a couple of things. 1) I don't want to override, I don't think, the default value of true as we were talking about initially, right? 2) I just want to interpret that "True" being returned as a checked box, otherwise don't check it. My understanding would think some condition needs to be put on the checked='" + CallFunctionHere + '" but that didn't seem to work.
d3020
Thinking a bit more about having the condition in the checked property. If I did a if statement like you were saying here, something like...checked='" if this CallFunction() returns true check else false'"Would that work and how would that line look syntax wise?
d3020
@d3020 -- sorry, I misunderstood. Yes, leave true/false and add in `checked='" + CallFunction() ? "checked" : "" + ...` Note this is C# syntax, modify as needed if you're using VB.
tvanfosson
I think I may have found a solution. Not sure if it is the "best" way but it seems to work. I called "CallFunction" from my input tag. Then inside of that there are conditions that say if returnedValue is true then return "checked". That seems to work. Be interested to hear a better approach if you have time to reply. Thanks again for your time and help.
d3020
@d3020 -- I don't mean to sound whiny, but given the amount of time I've put into helping you, you might want to at least upvote my answer, if not accept it. That's sort of the way the system works. For more info, see the FAQ (http://stackoverflow.com/faq).
tvanfosson
Sorry, this is my first post here. I wasn't sure how the system worked and that I needed to do that. I think I just did it though for you. I must be missing something with the syntax. I now have this...checked='" + GetPackagingSizeTypeControlValue("chkFlashedCarton") + " ? 'checked' : '''but even when it returns False it is still checked.
d3020
Are you using C#? The syntax I was going for used the ternary operator in C# `booleanExpression ? result-if-true : result-if-false` or `checked='" + (GetPackagingSizeTypeControlValue("chkFlashedCarton") ? "checked" : "") + "' />"`. I put parenthesis around the ternary expression, though they aren't strictly necessary.
tvanfosson
Yes, C# is being used. I follow what you're saying here now but still the result is always checked with using what you provided here even if the returned value from that function is False. Strange.
d3020
Try changing it to this then: `(GetPackagingSizeTypeControlValue("chkFlashedCarton") ? "checked='checked'" : "") + " />"`
tvanfosson
Still checked even if false with that change. Just out of curiosity I put in some garbage text between 'checked' and "" like 'abc' and "abc" to what would happen. Nothing happened. It still came back checked when it was False. My guess would be that it's not even reaching those points then?? However it is calling the function because I can Response.Write the returned value and it is False.
d3020
What does the generated HTML look like?
tvanfosson
<input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' disabled='disabled' value='true' checked='' />This is the output of it being checked even though the return from the function call was False.
d3020
It's like the checked is still there in the tag but I think what were after is for it to be completely gone from the tag if the return value from the function call was False.
d3020
I'm pretty sure if you used my last sample, checked wouldn't even be in the tag. It doesn't append the `checked='checked'` unless the return value of the function is true, otherwise it simply appends an empty string.
tvanfosson
With your last sample it returns this (still checked when false is returned). <input type='checkbox' id='chkFlashedCarton' name='chkFlashedCarton' disabled='disabled' value='true' checked= />
d3020
I'd be curious as to why, but thanks again for your time with this. I've got a solution with using the if statements in the function, but it'd be cool to use this approach you're saying if it worked. Thanks again.
d3020
Post the exact code you are currently using as an edit to your question -- I'll see if I can update it to work with the ternary operator.
tvanfosson
Edit posted. Thanks.
d3020
I see it cut it off...let me try again here. This is the call to the function in my input tag - " + GetPackagingSizeTypeControlValue("chkFlashedCarton") + "
d3020