Question

In my trade order form, if the user selects order type = "limit" then a limit price must be specified. I am trying to enforce this rule using a funcCall, but it is not working. If limit order is selected and the limit price field is left blank, the validation engine validates the form successfully. Here's my HTML:

<li>
    <label for=tradeForm_orderType>Order Type</label>
    <select id=tradeForm_orderType name=orderParams.type>
        <option value=Market>Market</option>
        <option value=Limit>Limit</option>
    </select>
</li>            

<li id=tradeForm_limitPriceItem>
    <label for=tradeForm_limitPrice>Limit Price</label>
    <input class="validate[funcCall[checkLimitOrder],custom[number]]"
        type=text name=orderParams.limitPrice.amount id=tradeForm_limitPrice>
</li>

And here's my checkLimitOrder function:

function checkLimitOrder(field, rules, i, options) {
    if ($('#tradeForm_orderType').val() === 'Limit' && field.val().length === 0) {
        return 'Please enter a limit price';
    }
}

This almost works! checkLimitOrder is indeed called and it returns the error message. However on return, the jQuery validation engine ignores the error message based on the following logic (lines 582-583):

// If the rules required is not added, an empty field is not validated
if(!required && field.val() == "") options.isError = false;

Well, I can't make the field required as it is only conditionally required. Is there any way I can work around this issue?

Was it helpful?

Solution

Answering my own question.

Turns out that the validation engine does not validate hidden fields (although this is not explicitly documented. So the solution was to hide limitPriceItem when the field was not required. Now I can put "validate[required,custom[number]]" on the limit price field and it validates only when the field is visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top