Multiple Conditions for Rules

49 views
Skip to first unread message

Chip Pinkston

unread,
Mar 21, 2014, 2:13:15 PM3/21/14
to valida...@googlegroups.com
Hi All,

I'm running into an issue trying to get conditional rules working correctly.  Basically, I'm attempting to validate that once one field has been selected, either one of two child fields has content.

I'm listed a sample form and rule file below.  The following form has a select with 3 options - Fruit, Candy, or Ice Cream. If the user selects either Fruit or Ice Cream, the conditional rule that a 'Name' needs to be provided for each one of those works perfectly.  However, for Candy - I would like to give the user a list of options OR the ability to specify a new one.

I saw Bob has a note in 'Multiple Conditions in a single Rule' (https://groups.google.com/d/msg/validatethis/9ij4io7n5os/Poz-H3DwIcQJ) to combine the two rules into a new rule - but I'm not sure I follow how to do that.  So I tried to combine the two into one general rule:

{"name":"isCandy",
"serverTest":"snack EQ 2 AND (!Len(candyType) OR !Len(candyName))",
"clientTest":"$(\"[name='snack']\").getValue() == 2 && (!$(\"[name='candyType']\").val() === '' || !$(\"[name='candyName']\").val() === '')"
},

The Server side test validates correctly, and will error if no value is selected.  However the clientTest doesn't appear to be working.  Can anyone see something I'm overlooking?


FORM:
------------------------------------------
 <!-- rendered by cfUniForm v4.6.0 (http://www.quackfuzed.com/demos/cfUniForm/) -->
<form action="/index.cfm/?action=step2" method="post" enctype="multipart/form-data" id="form-step1" class="uniForm ">
    <fieldset class="inlineLabels" id="cfU-C775D07C-E411-5B29-02901692DD0F9008">
        <legend>Snack Time<span id='requiredFieldsTitle'><span class='red'>*</span> Required fields</span></legend>
        <div class="cfu-fs-inner-wrap">
            <div class="ctrlHolder">
                <label for="snack" class="requiredField"><em>*</em> Snack Selector</label>
                <select name="snack" id="snack" class="selectInput required" >
                    <option value="" selected="selected" class="">--- SELECT ---</option>
                    <option value="1" class="">Fruit</option>
                    <option value="2" class="">Candy</option>
                    <option value="3" class="">Ice Cream</option>
                </select>
            </div>
            <div class="ctrlHolder">
                <label for="fruitName" class="">Fruit Name</label>
                <input name="fruitName" id="fruitName" value="" size="35" type="text" class="textInput" /> 
            </div>
            <div class="ctrlHolder">
                <label for="candyType" class="">Candy Type</label>
                <select name="candyType" id="candyType" class="selectInput" >
                    <option value="" selected="selected" class="">--- SELECT ---</option>
                    <option value="1" class="">Snickers</option>
                    <option value="2" class="">Mars</option>
                </select>
            </div>
            <div class="ctrlHolder">
                <label for="candyName" class="">Candy Name</label>
                <input name="candyName" id="candyName" value="" size="35" type="text" class="textInput" />
            </div>
            <div class="ctrlHolder">
                <label for="flavorName" class="">Flavor Name</label>
                <input name="flavorName" id="flavorName" value="" size="35" type="text" class="textInput" /> 
            </div>
</div>
    </fieldset>
    <div class="buttonHolder">
        <button type="submit" class="primaryAction " name="validate-step1" id="validate-step1">Step 2 &gt;</button>
    </div> 
<input type="hidden" id="formID" name="formID" value="form-step1" />
</form>

RULES:
-------------------------------------------
{
"validateThis" : {
"conditions" : [
{"name":"isFruit",
"serverTest":"snack EQ 1",
"clientTest":"$(\"[name='snack']\").getValue() == 1"
},
{"name":"isCandy",
"serverTest":"snack EQ 2 AND (Len(candyType) OR Len(candyName))",
"clientTest":"$(\"[name='snack']\").getValue() == 2 && ($(\"[name='candyType']\").length() || $(\"[name='candyName']\").length())"
},
{"name":"isIceCream",
"serverTest":"snack EQ 3",
"clientTest":"$(\"[name='snack']\").getValue() == 3"
}
],
"objectProperties" : [
{"name":"snack","desc":"Snack Type",
"rules": [
{"type":"required","failureMessage":"You must select a snack"}
]
},
{"name":"fruitName","desc":"Name of a Fruit",
"rules" : [
{"type":"required","condition":"isFruit","failureMessage":"You must specify a fruit name"}
]
},
{"name":"candyType","desc":"Standard Candy",
"rules" : [
{"type":"required","condition":"isCandy","failureMessage":"You must Select a Candy - or - specify one below"}
]
},
{"name":"candyName","desc":"Optional Candy",
"rules" : [
{"type":"required","condition":"isCandy","failureMessage":"You must Specify a Candy - or - select one above"}
]
},
{"name":"flavorName","desc":"Name of an Ice Cream Flavor",
"rules" : [
{"type":"required","condition":"isIceCream","failureMessage":"You must specify a Flavor"}
]
}
]
}
}

John Whish

unread,
Mar 26, 2014, 3:49:48 PM3/26/14
to valida...@googlegroups.com
I don't think you want the "not" in your JS if you are checking it equals ''.

I closer version of your CF code in JS would be:

"serverTest":"snack EQ 2 AND (!Len(candyType) OR !Len(candyName))"

"clientTest":"$(\"[name='snack']\").getValue() == 2 && (!$(\"[name='candyType']\").getValue().length || !$(\"[name='candyName']\").getValue().length)"

I haven't tested that, but it looks about right :)


--
You received this message because you are subscribed to the Google Groups "ValidateThis" group.
To unsubscribe from this group and stop receiving emails from it, send an email to validatethis...@googlegroups.com.
To post to this group, send email to valida...@googlegroups.com.
Visit this group at http://groups.google.com/group/validatethis.
For more options, visit https://groups.google.com/d/optout.

Chip Pinkston

unread,
Apr 3, 2014, 2:30:09 PM4/3/14
to valida...@googlegroups.com, john....@googlemail.com
Hi John,

Thanks!  It looks like I was missing the 'getValue()' calls before the lengths.  Also, because it's an either or, I just moved the negator check outside of the or comparison.

Final Rule:
{"name":"isCandy",
"serverTest":"snack EQ 2 AND (!Len(candyType) OR !Len(candyName))",
"clientTest":"$(\"[name='snack']\").getValue() == 2 && !($(\"[name='candyType']\").getValue().length || $(\"[name='candyName']\").getValue().length)"
},
Reply all
Reply to author
Forward
0 new messages