<p>It will be possible in the next release (commerce 2-beta.5):</p>

<p><a href="https://github.com/craftcms/commerce/issues/342" rel="nofollow noreferrer">https://github.com/craftcms/commerce/issues/342</a></p>

CC BY-SA 4.0
2018-05-15T06:42:17.947Z
2018-05-15T06:42:17.947Z
Luke Holder
<p>Address validation is, in Commerce v1 at least, generally handled via a plugin.</p>

<p>(I'll assume you already have a business logic plugin on the go, but if not head over to <a href="https://pluginfactory.io" rel="nofollow noreferrer">https://pluginfactory.io</a> and use that to generate your plugin skeleton).</p>

<p>Then, you basically need to add a listener to <code>onBeforeSaveAddress</code> and do your validation in that.</p>

<p>In your plugins <code>init</code>:</p>

<pre><code>    craft()-&gt;on('commerce_addresses.onBeforeSaveAddress',
        [
            craft()-&gt;businessLogic_logic,
            'onBeforeSaveAddressHandler'
        ]
    ); 
</code></pre>

<p>And then the actual handler in my case this is in my <code>BusinessLogic_LogicService</code>:</p>

<pre><code>public function onBeforeSaveAddressHandler($event){

    $address = $event-&gt;params['address'];

    if(!$address-&gt;firstName){
        $address-&gt;addError('firstName','First name must not be empty');
        $event-&gt;performAction = false;
    }
    ...etc, anything else you want really
}
</code></pre>

<p>...that's the essence of it really.  You can do any validation you like, and just add errors as you need them and set <code>$event-&gt;performAction = false;</code> to stop the dodgy address being saved.</p>

<p>....and looks like Luke is creating something easier for V2, but this is at least effective and works for now!</p>

CC BY-SA 4.0
2018-05-15T06:50:39.540Z
2018-05-15T06:50:39.540Z
Jeremy Daalder
<p>As default Craft has only <code>First Name</code> and <code>Last Name</code> as required. Is it possible to set more as this two fields as required fields? And is it also possible to set custom Fields as required too?</p>

<plugin-craftcommerce><form><validation>
Craft Commerce — Billing / Shipping Address validation / required Fields
# Craft Commerce — Billing / Shipping Address validation / required Fields

(Asked by: username_0 on 2018-05-15)

As default Craft has only `First Name` and `Last Name` as required. Is it possible to set more as this two fields as required fields? And is it also possible to set custom Fields as required too?

**Tags:** plugin-craftcommerce, form, validation


---

## 2 answers

### Answer by username_1 on 2018-05-15. Score: 3

It will be possible in the next release (commerce 2-beta.5):

<https://github.com/craftcms/commerce/issues/342>


---

### Answer by username_2 on 2018-05-15. Score: 3

Address validation is, in Commerce v1 at least, generally handled via a plugin.
(I'll assume you already have a business logic plugin on the go, but if not head over to https://pluginfactory.io and use that to generate your plugin skeleton).
Then, you basically need to add a listener to onBeforeSaveAddress and do your validation in that.
In your plugins init:
    craft()->on('commerce_addresses.onBeforeSaveAddress',
        [
            craft()->businessLogic_logic,
            'onBeforeSaveAddressHandler'
        ]
    ); 

And then the actual handler in my case this is in my BusinessLogic_LogicService:
public function onBeforeSaveAddressHandler($event){

    $address = $event->params['address'];

    if(!$address->firstName){
        $address->addError('firstName','First name must not be empty');
        $event->performAction = false;
    }
    ...etc, anything else you want really
}

...that's the essence of it really.  You can do any validation you like, and just add errors as you need them and set $event->performAction = false; to stop the dodgy address being saved.
....and looks like Luke is creating something easier for V2, but this is at least effective and works for now!


---

