1

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?

2 Answers 2

3

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

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

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.