2

I need to make the address fields in craft commerce required, as well as the phone number, etc. I tried to add the 'required' tag to the input element, but it breaks the form so you can't submit it -- at least in chrome. The submit button just stops working.

Is there any way to make these fields required without a plugin? Seems that this should be a feature included (or something optionally set in the code).

Thanks!

2

You can make address fields required in two ways - at the front end using your own js form validation or html techniques (and note that the Commerce example templates are not at all intended for use with live stores, they're just examples to get you going with your own development).

The second approach is in a plugin -

Craft Commerce: Validation on Address Fields

...the ideal approach is probably to use both methods.

Currently (Dec 16) there isn't another way. I presume this is to give maximum flexibility with checkout and perhaps (and hopefully) pending the time when addresses become proper element types with custom field support. That would be nice.

| improve this answer | |
2

For those using Craft Commerce 2, additional address validation rules can be added by using to the Address::EVENT_REGISTER_ADDRESS_VALIDATION_RULES event.

use craft\commerce\events\RegisterAddressRulesEvent;
use craft\commerce\models\Address;
use yii\base\Event;

Event::on(
    Address::class,
    Address::EVENT_REGISTER_ADDRESS_VALIDATION_RULES,
    function (RegisterAddressRulesEvent $event) {
        $event->rules[] = [['attention'], 'required'];
    }
);

Source: https://docs.craftcms.com/commerce/v2/events.html#other-events

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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