Forum

Thread tagged as: Problem, Forms

Perch:if which has a required field inside (form won't send)

I have setup forms that allow for the editor to select which form fields are displayed for each form, from a predefined set of fields. So in Perch the editor checks a box for each field to display and then in the code there are perch:if statements checking to see if a checkbox is checked. This all works fine until a field is set to required, which some fields if needing to be displayed; will be this way.

Checkbox in the admin area is displayed using this code

<perch:content id="show_postcode" type="checkbox" label="Show Postcode field" suppress="true" />

If the above box is checked then the below code is loaded.

<perch:if exists="show_postcode">
    <perch:label for="postcode">Postcode *</perch:label>
    <perch:input type="text" id="postcode" required="false" label="Postcode" size="40" />
</perch:if>

The above all works fine and the form submits etc.

However, some form fields are required. When you set any of the fields eg. Postcode to required like below and you don't check to display this field, then the form doesn't send even though the checkbox to show postcode isn't checked and is inside the 'if' statement.

<perch:if exists="show_postcode">
    <perch:label for="postcode">Postcode *</perch:label>
    <perch:input type="text" id="postcode" required="true" label="Postcode" size="40" />
    <perch:error for="postcode" type="required">
        <span class="error">Please add your Postcode number</span>
    </perch:error>
</perch:if>

How do I get round this?

Thanks

Mark Watts

Mark Watts 0 points

  • 4 years ago

Mark. perch:form tags are parsed in a different pass then perch:content. For this reason if any of the form tags has required set it will throw an error when submitted even if that field has been filtered out with perch:if. Forms is expecting all required fields as it's oblivious to the fact content has removed any fields.

Thanks Robert, so there isn't anyway round it then?

I was thinking maybe I could add another checkbox which would append if the field was required or not. So they are all set to NOT required then if a field is needing to be displayed then all good. But if they want the field displayed but for it also to be required form field then they (perch editor) checks another box to say make this field required?

Does that make sense?

Sorry, no way around it. I have tried extensively to get around this but it won't work.

OK so thinking about this I've come up with an untested solution. Maybe you can surround the required attribute with a perch if statement. I am on the road this morning so I can't exactly type you the example.

<perch:input type="text" id="postcode" <perch:if exists="setting">required="true" </perch:if>label="Postcode" size="40" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

The form is submitted without any indication of the context, so when the required fields are evaluated there's no way to tell if the field was displayed on not.

There's no way around it. If you want different fields, you need different forms.

OK thanks for your help and pointing out that I'm not going mad! It took me a while to understand why it wasn't working ;o)