I have Line Item options that I'm trying to validate in a module. I'm using the example code from the docs here on the front-end of the cart.

Below that it says:

Commerce does not validate the options and note parameters. If you’d like to limit user input, use front-end validation or use the Model::EVENT_DEFINE_RULES event to add validation rules for the LineItem model.

Using that and the Event Generator I've come up with this:

Event::on(LineItem::class, Model::EVENT_DEFINE_RULES, function (DefineRulesEvent $event) {
  $event->rules[] = [['options'], 'required'];

But no validation is performed; I can continue in the cart without any options input. My options show based on the quantity of the line item; for example, if the line item quantity is 3, 3 input fields show.

Can anyone please help me get this to work?

  • Where is your code located, is it inside the init method of you module? Commented May 22, 2023 at 11:16
  • Thanks for the reply, Luke. I use the new Generator plugin to generate the module so it's inside the attachEventHandlers which is called inside the init method. It actually peforms the validation on a cart in the control panel, just not on the front-end.
    – supazu
    Commented May 22, 2023 at 12:54
  • Ok so it seems to be because options are submitted as an empty string inside the array? So it's seeing option[name] = "" and thinking that meets the required rule. Changing it to $event->rules[] = [['options'], 'string', 'min' => 2]; indeed trips it up. Now I need to work out how I can make that work for my situation.
    – supazu
    Commented May 23, 2023 at 13:50

1 Answer 1


Seems like adding validation to options is difficult (in this case, I want to enforce an email but not another field.) So I'm going to use front-end validation instead.

Edit: this doesn't exactly answer my question, but I was able to make it work:

$event->rules[] = [['options'], 'each', 'rule' => ['each', 'rule' => ['required']]];

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.