Forum

Thread tagged as: Problem, Forms

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.

Fred Rivett

Fred Rivett 0 points

  • 6 years ago

Try this:

<perch:label for="phone">Phone (optional)</perch:label>
<perch:input type="number" id="phone" label="Phone" />

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.

    <perch:input type="number" id="phone" label="Phone" required="false" />

Also the perch:error wasn't needed because required wasn't needed.

<perch:error for="phone" type="format">
  <span class="error">Please check your phone number</span>
</perch:error>

Thanks Stephen, that makes sense, much appreciated.