<p>The way to do address validation with Commerce in gneral is using a plugin and listening to the <code>onBeforeSaveAddress</code> event.</p>

<p>Set up a listener with something like this:</p>



<pre class="lang-php prettyprint-override"><code>    craft()-&gt;on('commerce_addresses.onBeforeSaveAddress',
        [
            craft()-&gt;businessLogic_logic,
            'onBeforeSaveAddressHandler'
        ]
    ); 
</code></pre>

<p>And here's an example of a function that does some validation:</p>

<pre class="lang-php prettyprint-override"><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;
    }
    if(!$address-&gt;lastName){
        $address-&gt;addError('lastName','Last name must not be empty');
        $event-&gt;performAction = false;
    }
    if(!$address-&gt;phone){
        $address-&gt;addError('phone','Phone number must not be empty');
        $event-&gt;performAction = false;
    }
    if(!$address-&gt;address1){
        $address-&gt;addError('address1','Street Address line 1 must not be empty');
        $event-&gt;performAction = false;
    }
    if(!$address-&gt;city){
        $address-&gt;addError('city','Suburb must not be empty');
        $event-&gt;performAction = false;
    }
    if(!$address-&gt;zipCode){
        $address-&gt;addError('zipCode','Postcode must not be empty');
        $event-&gt;performAction = false;
    }     
    if(!$address-&gt;countryId){
        $address-&gt;addError('countryId','Country ID must not be empty');
        $event-&gt;performAction = false;
    }          
}
</code></pre>

CC BY-SA 3.0
2017-11-10T11:50:30.650Z
Thank you for this. makes sense. a little past my expertise though, so I guess i'm going to have to find someone who can create this plugin for me.
ToddPadwick
CC BY-SA 3.0
2017-11-11T00:43:49.540Z
Well, Craft Slack has a jobs section if you want to shop the work out, but you're probably going to want to get familiar with plugins if you're doing anything beyond the most basic Commerce project....it's really where all the power lies.  You can generate a lot of the plugin infrastructure using https://pluginfactory.io/ - and then just basically fill in the bits as supplied above but you will need some php skills to get it going really.  Good luck!
Jeremy Daalder
CC BY-SA 3.0
2017-11-09T01:59:44.733Z
2017-11-09T17:28:53.163Z
2017-11-09T17:28:53.163Z
Jeremy Daalder
<p>You could just create javascript/jQuery validation for your form(s). And whenever the form is submitted or the submit button clicked, validate your fields however you like, and don't submit if they're invalid.</p>

<p>This avoids a trip to the server, so it's also faster and a better UX for users. Of course it wouldn't provide any server side validation, or admin validation. But if all you care about is people not submitting orders without a phone number, it should suffice... And much easier than creating a plugin if you're not already familiar with plugin development.</p>

CC BY-SA 4.0
2018-07-19T21:54:00.970Z
And then someone like me appears with a little bit Js knowledge or a person with disabled js and destroys your validation. I know it's not that critical in this case but solutions like yours saved me money and time in the past. I would not recommend it. It's great for the user though
Robin Schambach
CC BY-SA 4.0
2018-07-20T11:31:49.537Z
One should always validate server side. But validating client side is important for UX. It's good practice to do both. Front end is for the user's benefit, back end is for the site's benefit.
foamcow
CC BY-SA 4.0
2018-07-23T19:10:38.117Z
JavaScript is used by 94.9% of all the websites. Someone with JS disabled in 2018 is atypical. I agree with @foamcow though that it's best practice to do both... However best practices aren't always practical or realistic. i.e. If you're capturing a tax id, server side is absolutely critical. But if it's a middle initial on a simple email form, and you have a deadline, spending a week learning to create a plugin probably isn't your best use of time.
Jack Sutherland
CC BY-SA 4.0
2018-07-19T21:42:51.637Z
2018-07-19T21:42:51.637Z
Jack Sutherland
<p>Because I used a similar address model to the one supplied in the Craft Commerce templates, which essentially uses the same form partials for both address and billing, and also hides and shows the form using JavaScript, I can't use HTML required attributes.</p>

<p>Is there an easy way to add in the config files a way to make 'shipping.phone' required and return an error in the same way the default required fields work?</p>

<pre class="lang-twig prettyprint-override"><code>&lt;div class="inputGroup{% if model and model.hasErrors('phone') %} error{% endif%}"&gt;
    &lt;h3 class="small"&gt;Contact details&lt;/h3&gt;
    &lt;div class="inputWrap"&gt;
        &lt;input type="text" id="{{ modelName }}-phone" class="u-full-width" name="{{ modelName }}[phone]" value="{{ model ? model.phone : '' }}"&gt;
        &lt;span class="highlight"&gt;&lt;/span&gt;
        &lt;span class="bar"&gt;&lt;/span&gt;
        &lt;label for="{{ modelName }}-phone"&gt;Telephone number&lt;/label&gt;
        &lt;span class="flash"&gt;{% if model and model.hasErrors('phone') %}{{ model.getError('phone') }}{% else %}Required{% endif %}&lt;/span&gt;
    &lt;/div&gt;
    &lt;p class="footnote"&gt;In case we need to contact you about important details regarding your order.&lt;/p&gt;
&lt;/div&gt;
</code></pre>

CC BY-SA 3.0
2017-11-11T12:55:35.183Z
Thanks Jeremy... you are right, I should expand my ability for this. I’ll look into some tutorials.
ToddPadwick
<plugin-craftcommerce><form><validation><ecommerce><required>
Craft Commerce: make address phone telephone field required without JavaScript
# Craft Commerce: make address phone telephone field required without JavaScript

(Asked by: username_0 on 2017-11-08)

Because I used a similar address model to the one supplied in the Craft Commerce templates, which essentially uses the same form partials for both address and billing, and also hides and shows the form using JavaScript, I can't use HTML required attributes.
Is there an easy way to add in the config files a way to make 'shipping.phone' required and return an error in the same way the default required fields work?

    Contact details
    
        
        
        
        Telephone number
        {% if model and model.hasErrors('phone') %}{{ model.getError('phone') }}{% else %}Required{% endif %}
    
    In case we need to contact you about important details regarding your order.

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

> **Comments:**
> **username_0**: Thanks Jeremy... you are right, I should expand my ability for this. I’ll look into some tutorials.  

---

## 2 answers

### Answer by username_1 on 2017-11-09. Score: 2

The way to do address validation with Commerce in gneral is using a plugin and listening to the onBeforeSaveAddress event.
Set up a listener with something like this:
    craft()->on('commerce_addresses.onBeforeSaveAddress',
        [
            craft()->businessLogic_logic,
            'onBeforeSaveAddressHandler'
        ]
    ); 

And here's an example of a function that does some validation:
public function onBeforeSaveAddressHandler($event){

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

    if(!$address->firstName){
        $address->addError('firstName','First name must not be empty');
        $event->performAction = false;
    }
    if(!$address->lastName){
        $address->addError('lastName','Last name must not be empty');
        $event->performAction = false;
    }
    if(!$address->phone){
        $address->addError('phone','Phone number must not be empty');
        $event->performAction = false;
    }
    if(!$address->address1){
        $address->addError('address1','Street Address line 1 must not be empty');
        $event->performAction = false;
    }
    if(!$address->city){
        $address->addError('city','Suburb must not be empty');
        $event->performAction = false;
    }
    if(!$address->zipCode){
        $address->addError('zipCode','Postcode must not be empty');
        $event->performAction = false;
    }     
    if(!$address->countryId){
        $address->addError('countryId','Country ID must not be empty');
        $event->performAction = false;
    }          
}

> **Comments:**
> **username_0**: Thank you for this. makes sense. a little past my expertise though, so I guess i'm going to have to find someone who can create this plugin for me.  
> **username_1**: Well, Craft Slack has a jobs section if you want to shop the work out, but you're probably going to want to get familiar with plugins if you're doing anything beyond the most basic Commerce project....it's really where all the power lies.  You can generate a lot of the plugin infrastructure using https://pluginfactory.io/ - and then just basically fill in the bits as supplied above but you will need some php skills to get it going really.  Good luck!  

---

### Answer by username_2 on 2018-07-19. Score: 1

You could just create javascript/jQuery validation for your form(s). And whenever the form is submitted or the submit button clicked, validate your fields however you like, and don't submit if they're invalid.

This avoids a trip to the server, so it's also faster and a better UX for users. Of course it wouldn't provide any server side validation, or admin validation. But if all you care about is people not submitting orders without a phone number, it should suffice... And much easier than creating a plugin if you're not already familiar with plugin development.

> **Comments:**
> **username_3**: And then someone like me appears with a little bit Js knowledge or a person with disabled js and destroys your validation. I know it's not that critical in this case but solutions like yours saved me money and time in the past. I would not recommend it. It's great for the user though  
> **username_4**: One should always validate server side. But validating client side is important for UX. It's good practice to do both. Front end is for the user's benefit, back end is for the site's benefit.  
> **username_2**: JavaScript is used by 94.9% of all the websites. Someone with JS disabled in 2018 is atypical. I agree with @username_4 though that it's best practice to do both... However best practices aren't always practical or realistic. i.e. If you're capturing a tax id, server side is absolutely critical. But if it's a middle initial on a simple email form, and you have a deadline, spending a week learning to create a plugin probably isn't your best use of time.  

---

