<p>Currently, there isn't a native way.</p>

<p>You could write some JS that does client-side validation, fork the repo add it yourself or if you're looking for a more complete form-building solution, <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/forms" rel="nofollow">hit up these guys</a>.</p>

CC BY-SA 3.0
2014-10-15T20:49:46.667Z
2014-10-15T20:49:46.667Z
Brad Bell
<p>You could also hand this over to HTML since it <a href="http://diveintohtml5.info/forms.html#required" rel="nofollow">supports form validation</a> as of version 5.
Depends on your browser support requirements though. <a href="http://caniuse.com/#search=required" rel="nofollow">This is what “Can i use” says.</a></p>

CC BY-SA 3.0
2014-11-13T19:09:45.220Z
Client-side form validation is generally best thought of as an enhancement rather than a replacement for server-side validation. You can never know for sure that user input has been validated on the client side, so you can't rely on it.
Nick F
CC BY-SA 3.0
2014-11-11T10:33:52.797Z
2014-11-11T10:33:52.797Z
Marcus Scheller
<p>It's relatively simple to tweak the Contact Form plugin to change the required fields. If you open <code>models/ContactFormModel.php</code> (in the plugin's folder), you can add <code>'required' =&gt; true,</code> to the <code>fromName</code> attribute definition:</p>

<pre><code>'fromName'   =&gt; array(AttributeType::String, 'required' =&gt; true, 'label' =&gt; 'Your Name'),
</code></pre>

<p>I tried this out, and it seemed to work as expected.</p>

CC BY-SA 4.0
2018-05-21T08:37:56.907Z
Not good this one. If you actively update your CMS which you should, then this is not a solution.
Laurence Cope
CC BY-SA 3.0
2014-11-10T21:58:38.430Z
Just be aware that those changes will be lost if you ever update the plugin.
Lindsey D
CC BY-SA 3.0
2014-11-13T19:13:40.243Z
Yep, good point - this change does effectively amount to forking the plugin. You could potentially still update the plugin via a git merge, but certainly the process will be more complicated, and you'll need to remember to do it this way rather than overwriting it with a newer version.
Nick F
CC BY-SA 3.0
2014-11-10T18:43:37.673Z
2014-11-10T18:43:37.673Z
Nick F
<p>Sprout Forms works great for making fields required. It nicely places the error info on the page (if you have the error tags in place) without losing the form data. Slick!</p>

CC BY-SA 3.0
2014-10-16T18:21:46.643Z
2014-10-16T18:21:46.643Z
glider
<p>Unfortunately, the Craft devs have not made this as easy as it should be. Luckily, we can utilize that it's built on Yii2.</p>

<p>The following solution makes <code>fromName</code>required, and are compatible with <strong>Contact Form Extensions</strong>.</p>

<hr>

<p>Make a new folder inside <code>modules</code> and call it <code>contact_form</code>.
Create the following file structure:</p>

<pre><code>modules\
  contact_form\
    controllers\
      SendController.php
    models\
      Submission.php
    Module.php
</code></pre>

<p>And enter the following into the files:</p>

<hr>

<p><strong>SendController.php</strong> (This is almost an exact copy of <code>craft\contactform\controllers</code>, but we're pointing it to our own Submission model)
    

<pre><code>namespace modules\contact_form\controllers;

use Craft;    
use modules\contact_form\models\Submission;
use craft\contactform\Plugin;
use craft\web\UploadedFile;
use yii\web\Response;

class SendController extends \craft\contactform\controllers\SendController
{

    public function actionIndex()
    {
        $this-&gt;requirePostRequest();
        $request = Craft::$app-&gt;getRequest();
        $plugin = Plugin::getInstance();
        $settings = $plugin-&gt;getSettings();

        $submission = new Submission();
        $submission-&gt;fromEmail = $request-&gt;getBodyParam('fromEmail');
        $submission-&gt;fromName = $request-&gt;getBodyParam('fromName');
        $submission-&gt;subject = $request-&gt;getBodyParam('subject');

        $message = $request-&gt;getBodyParam('message');
        if (is_array($message)) {
            $submission-&gt;message = array_filter($message, function($value) {
                return $value !== '';
            });
        } else {
            $submission-&gt;message = $message;
        }

        if ($settings-&gt;allowAttachments &amp;&amp; isset($_FILES['attachment']) &amp;&amp; isset($_FILES['attachment']['name'])) {
            if (is_array($_FILES['attachment']['name'])) {
                $submission-&gt;attachment = UploadedFile::getInstancesByName('attachment');
            } else {
                $submission-&gt;attachment = [UploadedFile::getInstanceByName('attachment')];
            }
        }

        if (!$plugin-&gt;getMailer()-&gt;send($submission)) {
            if ($request-&gt;getAcceptsJson()) {
                return $this-&gt;asJson(['errors' =&gt; $submission-&gt;getErrors()]);
            }

            Craft::$app-&gt;getSession()-&gt;setError(Craft::t('contact-form', 'There was a problem with your submission, please check the form and try again!'));
            Craft::$app-&gt;getUrlManager()-&gt;setRouteParams([
            'variables' =&gt; ['message' =&gt; $submission]
            ]);

            return null;
        }

        if ($request-&gt;getAcceptsJson()) {
            return $this-&gt;asJson(['success' =&gt; true]);
        }

        Craft::$app-&gt;getSession()-&gt;setNotice($settings-&gt;successFlashMessage);
        return $this-&gt;redirectToPostedUrl($submission);
    }
}
</code></pre>

<hr>

<p><strong>Submission.php</strong>
    

<pre><code>namespace modules\contact_form\models;

class Submission extends \craft\contactform\models\Submission
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['fromEmail', 'message', 'fromName'], 'required'],
        ];
    }
}
</code></pre>

<hr>

<p><strong>Module.php</strong>
    

<pre><code>class Module extends \yii\base\Module
{
    public function init()
    {
        $this-&gt;controllerNamespace = 'modules\\contact_form\\controllers';
        parent::init();
    }
}
</code></pre>

<hr>

<p><strong>config\app.php</strong></p>

<p>Update to include your module:</p>

<pre><code>return [
    'modules' =&gt; [
        ...
        'contact' =&gt; \modules\contact_form\Module::class
        ...
    ],
    ...
];
</code></pre>

<hr>

<p><strong>Your view</strong>
And finally update your form code to use your new controller:</p>

<pre><code>&lt;input type="hidden" name="action" value="contact/send"&gt;
</code></pre>

CC BY-SA 4.0
2018-10-02T11:13:00.587Z
2018-10-02T11:13:00.587Z
Jørgen
<p>When using the Pixel &amp; Tonic contact form (<a href="https://github.com/pixelandtonic/ContactForm" rel="nofollow">https://github.com/pixelandtonic/ContactForm</a>) how do you make the fromName field required?</p>

<p>When using the form as seen on github, only the message and email field are required.</p>

<p>Thank you!</p>

<plugin-contactform>
How to make fields required on the contact form?
# How to make fields required on the contact form?

(Asked by: username_0 on 2014-10-15)

When using the Pixel & Tonic contact form (<https://github.com/pixelandtonic/ContactForm>) how do you make the fromName field required?

When using the form as seen on github, only the message and email field are required.

Thank you!

**Tags:** plugin-contactform


---

## 5 answers

### Answer by username_1 on 2014-10-15. Score: 0 | Accepted answer

Currently, there isn't a native way.

You could write some JS that does client-side validation, fork the repo add it yourself or if you're looking for a more complete form-building solution, [hit up these guys](http://sprout.barrelstrengthdesign.com/craft-plugins/forms).


---

### Answer by username_2 on 2014-11-11. Score: 3

You could also hand this over to HTML since it [supports form validation](http://diveintohtml5.info/forms.html#required) as of version 5.
Depends on your browser support requirements though. [This is what “Can i use” says.](http://caniuse.com/#search=required)

> **Comments:**
> **username_3**: Client-side form validation is generally best thought of as an enhancement rather than a replacement for server-side validation. You can never know for sure that user input has been validated on the client side, so you can't rely on it.  

---

### Answer by username_3 on 2014-11-10. Score: 2

It's relatively simple to tweak the Contact Form plugin to change the required fields. If you open models/ContactFormModel.php (in the plugin's folder), you can add 'required' => true, to the fromName attribute definition:
'fromName'   => array(AttributeType::String, 'required' => true, 'label' => 'Your Name'),

I tried this out, and it seemed to work as expected.

> **Comments:**
> **username_5**: Just be aware that those changes will be lost if you ever update the plugin.  
> **username_3**: Yep, good point - this change does effectively amount to forking the plugin. You could potentially still update the plugin via a git merge, but certainly the process will be more complicated, and you'll need to remember to do it this way rather than overwriting it with a newer version.  
> **username_4**: Not good this one. If you actively update your CMS which you should, then this is not a solution.  

---

### Answer by username_6 on 2014-10-16. Score: 1

Sprout Forms works great for making fields required. It nicely places the error info on the page (if you have the error tags in place) without losing the form data. Slick!


---

### Answer by username_7 on 2018-10-02. Score: 1

Unfortunately, the Craft devs have not made this as easy as it should be. Luckily, we can utilize that it's built on Yii2.
The following solution makes fromNamerequired, and are compatible with Contact Form Extensions.

Make a new folder inside modules and call it contact_form.
Create the following file structure:
modules\
  contact_form\
    controllers\
      SendController.php
    models\
      Submission.php
    Module.php

And enter the following into the files:

SendController.php (This is almost an exact copy of craft\contactform\controllers, but we're pointing it to our own Submission model)
    

namespace modules\contact_form\controllers;

use Craft;    
use modules\contact_form\models\Submission;
use craft\contactform\Plugin;
use craft\web\UploadedFile;
use yii\web\Response;

class SendController extends \craft\contactform\controllers\SendController
{

    public function actionIndex()
    {
        $this->requirePostRequest();
        $request = Craft::$app->getRequest();
        $plugin = Plugin::getInstance();
        $settings = $plugin->getSettings();

        $submission = new Submission();
        $submission->fromEmail = $request->getBodyParam('fromEmail');
        $submission->fromName = $request->getBodyParam('fromName');
        $submission->subject = $request->getBodyParam('subject');

        $message = $request->getBodyParam('message');
        if (is_array($message)) {
            $submission->message = array_filter($message, function($value) {
                return $value !== '';
            });
        } else {
            $submission->message = $message;
        }

        if ($settings->allowAttachments && isset($_FILES['attachment']) && isset($_FILES['attachment']['name'])) {
            if (is_array($_FILES['attachment']['name'])) {
                $submission->attachment = UploadedFile::getInstancesByName('attachment');
            } else {
                $submission->attachment = [UploadedFile::getInstanceByName('attachment')];
            }
        }

        if (!$plugin->getMailer()->send($submission)) {
            if ($request->getAcceptsJson()) {
                return $this->asJson(['errors' => $submission->getErrors()]);
            }

            Craft::$app->getSession()->setError(Craft::t('contact-form', 'There was a problem with your submission, please check the form and try again!'));
            Craft::$app->getUrlManager()->setRouteParams([
            'variables' => ['message' => $submission]
            ]);

            return null;
        }

        if ($request->getAcceptsJson()) {
            return $this->asJson(['success' => true]);
        }

        Craft::$app->getSession()->setNotice($settings->successFlashMessage);
        return $this->redirectToPostedUrl($submission);
    }
}


Submission.php
namespace modules\contact_form\models;

class Submission extends \craft\contactform\models\Submission
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['fromEmail', 'message', 'fromName'], 'required'],
        ];
    }
}


Module.php
class Module extends \yii\base\Module
{
    public function init()
    {
        $this->controllerNamespace = 'modules\\contact_form\\controllers';
        parent::init();
    }
}


config\app.php
Update to include your module:
return [
    'modules' => [
        ...
        'contact' => \modules\contact_form\Module::class
        ...
    ],
    ...
];


Your view
And finally update your form code to use your new controller:


---

