Forum
Required attribute not showing on form inputs
Hi,
I have a Perch form within an editable content template so that editors can manage the form labels and error messages. Everything is working as I'd expect except that the required attribute is not being set on the inputs even though I have included required="true".
Here is the template code:
<perch:form id="contact-form" method="post" app="perch_forms" prefix="none" action="#contact-form">
<perch:content id="body_text" type="textarea" size="m" label="Body content" html="true" editor="markitup" textile="true" help="Body content pasted here" />
<perch:error for="all" type="general">
<p class="alert alert--warning"><perch:content id="form_general_error_text" type="text" size="xl" label="Error message" html="true" textile="false" help="Content displayed to user when validation errors are present" required="true" divider-before="General error message" /></p>
</perch:error>
<div class="form-item">
<perch:label for="name" class="required"><perch:content id="form_label_name" required="true" type="text" size="l" label="Name label" html="false" textile="false" help="e.g. Your name" divider-before="Form field labels and validation messages" /></perch:label>
<perch:input type="text" id="name" required="true" label="Name" />
<perch:error for="name" type="required"><p class="alert-form alert--warning"><perch:content id="form_error_name" required="true" type="text" size="l" label="Name validation" html="false" textile="false" help="e.g. Please provide your name" /></p></perch:error>
</div>
<div class="form-item">
<perch:label for="email" class="required"><perch:content id="form_label_email" required="true" type="text" size="l" label="Email label" html="false" textile="false" help="e.g. Your email" /></perch:label>
<perch:input type="email" id="email" required="true" label="Email" />
<perch:error for="email" type="required"><p class="alert-form alert--warning"><perch:content id="form_error_email" required="true" type="text" size="l" label="Email validation" html="false" textile="false" help="e.g. Please provide your email" /></p></perch:error>
<perch:error for="email" type="format"><p class="alert-form alert--warning"><perch:content id="form_error_email_format" required="true" type="text" size="l" label="Email validation (format)" html="false" textile="false" help="e.g. Please check your email address" /></p></perch:error>
</div>
<div class="form-item">
<perch:label for="tel" class="required"><perch:content id="form_label_telephone" required="true" type="text" size="l" label="Phone label" html="false" textile="false" help="e.g. A contact number" /></perch:label>
<perch:input type="text" id="tel" required="true" label="Tel" />
<perch:error for="tel" type="required"><p class="alert-form alert--warning"><perch:content id="form_error_tel" required="true" type="text" size="l" label="Tel validation" html="false" textile="false" help="e.g. Please provide a contact number" /></p></perch:error>
</div>
<div class="form-item">
<perch:label for="message"><perch:content id="form_label_message" required="true" type="text" size="l" label="Email label" html="false" textile="false" help="e.g. Your message" /></perch:label>
<perch:input type="textarea" id="message" label="Message" rows="6" />
</div>
<div class="form-item">
<perch:input type="submit" id="submit" class="c-btn c-btn--contact" value="<perch:content id="form_button_value" required="true" type="text" size="l" html="false" textile="false" help="e.g. Send info request" divider-before="Form button text" />" />
</div>
<perch:success>
<div class="alert alert--success">
<perch:content id="form_success_text" type="textarea" size="s" label="Success message" html="true" editor="markitup" textile="true" help="Content displayed to user on a successful form submission" required="true" divider-before="Success message" />
</div>
</perch:success>
</perch:form>
Any help or ideas would be much appreciated!
What do you mean by the required attribute is not being set? What happens? Can you see it in the source?
Hi Rachel,
The required attribute isn't set on the form input in the source, and so the form just submits without any browser validation.
I can also see that the input I have for the email field is set to type="email" in my template but is output in the source as type="text"
Are you viewing source as opposed to using DevTools?
Hi, Both source and dev tools show the same thing: no required attribute set on the input.
Can we see your diagnostics?
Hi again. I've had a look in the error logs and can see the following occurs when I submit the form.
PHP Fatal error: Uncaught Error: Call to a member function label() on null in /Applications/MAMP/htdocs/xxxxxxxxxx/httpdocs/perch/addons/apps/perch_forms/runtime.php:25
I've also tried throwing the same template into another Perch project and it seems to work as expected there.
I can try a fresh install and see if that helps, but if you have any ideas about the error message, that would be useful.
Thanks,
Simon
Diagnostics would be useful.
Hi Drew, yes, sure, I hadn't seen your reply when I sent my last comment. I'll post the Diagnostics shortly. Thanks.
Hi, here's the extended diagnostics report:
Thanks!
You are using Safari which doesn't support the HTML5 attributes. Is it these you are not seeing? Or are you also not seeing error messages set through Perch.
Hi Rachel.
Yes, it's the HTML5 required attribute that I'm not seeing, plus the input types I ask for in the template (e.g. type="email") display as type="text". So because there is no browser validation, the form is processed immediately and I see the error messages I've set in Perch.
I'm using Chrome actually, but I also get the same behaviour in FF and IE. As I said earlier, I've tried throwing the same template at another Perch project and it works as expected, and doesn't produce the php error that I mentioned earlier. I'll try a fresh install of Perch and see if that does anything.
Hi there,
I dropped the database and ran through set up again and everything is working exactly as it should. No idea what was happening to cause it not to work but I'm pleased it's all ok.
Thanks very much for your time.