<p>You can move it all into a <a href="https://craftcms.com/docs/plugins/services" rel="nofollow">Service</a> and then call it from your main plugin file if you find it is getting unwieldy.</p>

<p>file: <code>craft/plugins/businesslogic/BusinessLogicPlugin.php</code>:
    

<pre><code>class BusinessLogicPlugin extends BasePlugin
{
    public function init()
    {
        craft()-&gt;on('commerce_addresses.onBeforeSaveAddress', function(Event $event)
        {
            craft()-&gt;businessLogic_events-&gt;onBeforeSaveAddress($event);
        });
    }
}
</code></pre>

<p>file: <code>craft/plugins/businesslogic/services/BusinessLogic_EventsService.php</code>:
    

<pre><code>class BusinessLogic_EventsService extends BaseApplicationComponent
{
    public function onBeforeSaveAddress(Event $event)
    {
        $address = $event-&gt;params['address'];

        if (empty($address-&gt;firstName))
        {
            $address-&gt;addError('firstName', Craft::t('Please enter your Name'));
            $event-&gt;performAction = false;

            // ...
        }
    }
}
</code></pre>

<p>Because <code>$event</code> is an object it will be <a href="http://php.net/manual/en/language.oop5.references.php" rel="nofollow">passed by reference</a>, and you don't need to return anything.</p>

CC BY-SA 3.0
2016-08-03T22:08:04.623Z
Thanks Mike. Should it not just work inside the controllers file? Or a better question might be what is the controllers folder/file for?
Terry Upton
CC BY-SA 3.0
2016-08-04T03:00:00.323Z
Controllers can be though of as providing end point in to your plugin - i.e. they give you an action url you can hit to do something.  That's not what you want/need here - here you instead register a method to run just before saveAddress is run, and in that method you're implementing some logic to do the validation - and services are the place you store your plugins logic.  So Mike's srtucture above is the way to go (you can bung it all in the main plugin file but that soon gets messy, this is a better way to go for all but the most simple examples).
Jeremy Daalder
CC BY-SA 3.0
2016-08-03T16:09:16.157Z
2016-08-03T16:09:16.157Z
Mike Pepper
<p>I am putting together my Business Logic plugin, as I want to validate the address fields throughout the checkout process of my Cart. I understand in order to do this I need to create a plugin and us the Craft Commerce <code>commerce_addresses.onBeforeSaveAddress</code> event.</p>

<p>I decided to try and use the <a href="https://github.com/lindseydiloreto/craft-businesslogic" rel="nofollow">https://github.com/lindseydiloreto/craft-businesslogic</a> as a starting place.</p>

<p>My code is as follows;</p>

<pre><code>public function init()
{
    craft()-&gt;on('commerce_addresses.onBeforeSaveAddress', function(Event $event){
        $address = $event-&gt;params['address'];

        if (empty($address-&gt;firstName))
        {
            $address-&gt;addError('firstName', Craft::t('Please enter your Name'));
             $event-&gt;performAction = false;
        }

        if (empty($address-&gt;address1))
        {
            $address-&gt;addError('address1', Craft::t('Please enter an Address'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;city))
        {
            $address-&gt;addError('city', Craft::t('Please enter your Town/City'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;zipCode))
        {
            $address-&gt;addError('zipCode', Craft::t('Please enter your Post Code'));
            $event-&gt;performAction = false;
        }

        if (empty($address-&gt;countryId))
        {
            $address-&gt;addError('countryId', Craft::t('Please select your Country'));
            $event-&gt;performAction = false;
        }
    });
}
</code></pre>

<p>If I have this code inside my to level plugin file named <code>BusinessLogicPlugin.php</code> it seems to work and it validates some of the fields as specified. </p>

<p>However, if I move this code into the controllers folder inside <code>/controllers/BusinessLogicController.php</code> it doesn't.</p>

<p>I am unsure what I need to do to move this out of the main/top level file, as I see this file getting very logon/messy if I need to add several 'business logics' to the site.</p>

<p>Can anyone shed light on this as to why it might not work within /controllers/</p>

<p>Thanks.</p>

<plugin-development><plugin-craftcommerce><plugins><controller>
Business Logic Controller for Validating Address in Commerce
# Business Logic Controller for Validating Address in Commerce

(Asked by: username_0 on 2016-08-03)

I am putting together my Business Logic plugin, as I want to validate the address fields throughout the checkout process of my Cart. I understand in order to do this I need to create a plugin and us the Craft Commerce commerce_addresses.onBeforeSaveAddress event.
I decided to try and use the https://github.com/lindseydiloreto/craft-businesslogic as a starting place.
My code is as follows;
public function init()
{
    craft()->on('commerce_addresses.onBeforeSaveAddress', function(Event $event){
        $address = $event->params['address'];

        if (empty($address->firstName))
        {
            $address->addError('firstName', Craft::t('Please enter your Name'));
             $event->performAction = false;
        }

        if (empty($address->address1))
        {
            $address->addError('address1', Craft::t('Please enter an Address'));
            $event->performAction = false;
        }

        if (empty($address->city))
        {
            $address->addError('city', Craft::t('Please enter your Town/City'));
            $event->performAction = false;
        }

        if (empty($address->zipCode))
        {
            $address->addError('zipCode', Craft::t('Please enter your Post Code'));
            $event->performAction = false;
        }

        if (empty($address->countryId))
        {
            $address->addError('countryId', Craft::t('Please select your Country'));
            $event->performAction = false;
        }
    });
}

If I have this code inside my to level plugin file named BusinessLogicPlugin.php it seems to work and it validates some of the fields as specified. 
However, if I move this code into the controllers folder inside /controllers/BusinessLogicController.php it doesn't.
I am unsure what I need to do to move this out of the main/top level file, as I see this file getting very logon/messy if I need to add several 'business logics' to the site.
Can anyone shed light on this as to why it might not work within /controllers/
Thanks.

**Tags:** plugin-development, plugin-craftcommerce, plugins, controller

---

## 1 answers

### Answer by username_1 on 2016-08-03. Score: 4 | Accepted answer

You can move it all into a Service and then call it from your main plugin file if you find it is getting unwieldy.
file: craft/plugins/businesslogic/BusinessLogicPlugin.php:
    

class BusinessLogicPlugin extends BasePlugin
{
    public function init()
    {
        craft()->on('commerce_addresses.onBeforeSaveAddress', function(Event $event)
        {
            craft()->businessLogic_events->onBeforeSaveAddress($event);
        });
    }
}

file: craft/plugins/businesslogic/services/BusinessLogic_EventsService.php:
    

class BusinessLogic_EventsService extends BaseApplicationComponent
{
    public function onBeforeSaveAddress(Event $event)
    {
        $address = $event->params['address'];

        if (empty($address->firstName))
        {
            $address->addError('firstName', Craft::t('Please enter your Name'));
            $event->performAction = false;

            // ...
        }
    }
}

Because $event is an object it will be passed by reference, and you don't need to return anything.

> **Comments:**
> **username_0**: Thanks Mike. Should it not just work inside the controllers file? Or a better question might be what is the controllers folder/file for?  
> **username_2**: Controllers can be though of as providing end point in to your plugin - i.e. they give you an action url you can hit to do something.  That's not what you want/need here - here you instead register a method to run just before saveAddress is run, and in that method you're implementing some logic to do the validation - and services are the place you store your plugins logic.  So Mike's srtucture above is the way to go (you can bung it all in the main plugin file but that soon gets messy, this is a better way to go for all but the most simple examples).  

---

