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
It will be possible in the next release (commerce 2-beta.5):
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!