<p>There's no clean way doing this without making changes to the plugin itself. The  <a href="https://github.com/pixelandtonic/ContactForm#the-contactformbeforesend-event" rel="nofollow"><code>contactForm.beforeSend</code></a> event makes it possible to prevent the email from being sent from another plugin, but you're only being passed the compiled email message as a string. It's probably possible to post you checkbox data with that string and parse for it in your event listener, but you asked for a clean solution! :)</p>

<p>So fork the plugin and add a new attribute to the ContactFormModel:</p>

<pre><code>'agreedToTerms' =&gt; array(AttributeType::Enum, 'required' =&gt; true, 'values' =&gt; array(true)),
</code></pre>

<p>And make the ContactFormController get the attribute value from your form:</p>

<pre><code>$message-&gt;agreedToTerms = craft()-&gt;request-&gt;getPost('agreedToTerms');
</code></pre>

<p>You're now all set add a checkbox "agreedToTerms" to your form and add a custom error message in case the model attribute failed validation:</p>

<pre><code>&lt;label&gt;&lt;input type="checkbox" name="agreedToTerms" value="true" {{ message is defined and message.agreedToTerms ? 'checked' }}&gt; Agree the Terms and Conditions&lt;/label&gt;

{% if message is defined and message.hasErrors('agreedToTerms') %}
    &lt;ul class="errors"&gt;&lt;li&gt;{{ 'You must agree the Terms and Conditions'|t }}&lt;/li&gt;&lt;/ul&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-08-05T08:32:20.060Z
2015-08-05T08:32:20.060Z
carlcs
<p>What is the cleanest way to validate if a checkbox (i.e. agree to legal terms) is checked? </p>

<validation><plugin-contactform>
Contact Form Plugin: Validate if a checkbox is checked?
# Contact Form Plugin: Validate if a checkbox is checked?

(Asked by: username_0 on 2015-08-04)

What is the cleanest way to validate if a checkbox (i.e. agree to legal terms) is checked?

**Tags:** validation, plugin-contactform

---

## 1 answers

### Answer by username_1 on 2015-08-05. Score: 3 | Accepted answer

There's no clean way doing this without making changes to the plugin itself. The  contactForm.beforeSend event makes it possible to prevent the email from being sent from another plugin, but you're only being passed the compiled email message as a string. It's probably possible to post you checkbox data with that string and parse for it in your event listener, but you asked for a clean solution! :)
So fork the plugin and add a new attribute to the ContactFormModel:
'agreedToTerms' => array(AttributeType::Enum, 'required' => true, 'values' => array(true)),

And make the ContactFormController get the attribute value from your form:
$message->agreedToTerms = craft()->request->getPost('agreedToTerms');

You're now all set add a checkbox "agreedToTerms" to your form and add a custom error message in case the model attribute failed validation:
 Agree the Terms and Conditions

{% if message is defined and message.hasErrors('agreedToTerms') %}
    {{ 'You must agree the Terms and Conditions'|t }}
{% endif %}


---

