2

I have a registration and checkout page where I give users the choice to register as a business or a private person (radio input).

If the user clicks on the 'business' radio, a pane slides down with inputs for company name and VAT number.

I would like to make these fields required, if chosen for business registration type.

I can do this with jQuery but can it be done with conditionals / hard coded too, in the template?

3

Unfortunately you can't make the required fields conditional. The Store/Zoo Visitor/Channel Form fields provide an option to set specific fields as being required but if you set these then they will always be required on form submission regardless of which option the user selected.

For this reason the best solution is to NOT set them as required fields in the template tags (server-side) but use JS/jQuery to handle this instead. That way EE wont grumble about form fields not being filled out when they're set to be required. You can then handle it all conditionally on the front-end and react to user selection.

| improve this answer | |
1

Based on my previous findings (Zoo Visitor: confirm delete user account) this is possible using Stash.

When you post a form, use the post info. In the redirect template you could check to see if a value exists and display an error message or redirect back/contunue

In a conditional you could so something like:

// Check to see if radio button 'is_business' is checked with value 'business'
// check to see if company name field is empty
{if "{exp:stash:get name='is_business' dynamic='yes'}" == 'business' AND "!{exp:stash:get name='company_name' dynamic='yes'}"}
    // error
{/if}

This way you can in any case provide a hard-coded way to validate you form.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.