Forum
Stopping a form field from being required
I must be doing something really simple wrong, but after trying a few things I can't seem to fix this.
I want to make the phone field optional, but for some reason the below code doesn't do that.
This is my code:
<perch:label for="phone">Phone (optional)</perch:label>
<perch:input type="number" id="phone" label="Phone" required="false" />
<perch:error for="phone" type="format">
<span class="error">Please check your phone number</span>
</perch:error>
And it generates:
<div>
<label for="form1_phone">Phone (optional)</label>
<input id="form1_phone" name="phone" type="number" required="required">
</div>
I've double checked and I'm definitely working on the right form (when I edit it and upload, save the contact form and refresh things change), but I can't get rid of that required="required".
As I say, it's probably a really simple fix, but I'm at a loss. Any help much appreciated.
Try this:
I'm not sure why, but that worked! Thanks Stephen. Might be removing the <perch:error> part, but I tried that earlier and it didn't seem to fix it.
Anyway, it's working as expected now, so thank you!
Glad to help.
You don't need to specify
required="false"
if it's not required, just leave that bit off.Also the
perch:error
wasn't needed becauserequired
wasn't needed.Thanks Stephen, that makes sense, much appreciated.