Forum

Thread tagged as: Question, Forms

Conditionals in form templates

Is there a way to require a certain form field ONLY if another form field has a particular value.

                <div class="col-md-12"><h3>Payment information</h3></div>
                <div class="col-md-12">
                    <div class="form-group">
                        <perch:label for="payment_method" class="control-label">Payment method</perch:label>
                        <div class="radio">
                            <label><perch:input type="radio" id="payment_method" value="online" required="true" label="Payment method" /> Pay online with Credit/Debit card</label>
                        </div>
                        <div class="radio">
                            <label><perch:input type="radio" id="payment_method" value="invoice" required="true" label="Payment method" /> Please send me an invoice</label>
                        </div>
                        <perch:error for="payment_method" type="required">Please choose a payment method</perch:error>
                    </div>
                    <div class="form-group po-number">
                        <perch:label for="po_number" class="control-label">PO Number</perch:label>
                        <perch:input type="text" id="po_number" label="PO Number" class="form-control" />
                        <perch:error for="po_number" type="required">Please add your PO Number</perch:error>
                    </div>
                    <div class="form-group">
                        <perch:label for="discount_code" class="control-label">Do you have a discount code?</perch:label>
                        <perch:input type="text" id="discount_code" label="Discount code" class="form-control" />
                    </div>
                </div>

In the above example, I only need the po_number if they've selected "offline" as their payment method.

Paul Bell

Paul Bell 0 points

  • 3 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Paul,

You need to use Javascript for something like this.

Thanks - I thought that might be the case, at least initially. I've written some JS to show/hide the field, and toggle the "required" attribute on it, and that may be enough. I was just curious as to if there's a way to add server side validation which depends on the value of another field.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

I think you'd need to build your own form validator with the Perch API in this case.