<p>Seems like adding validation to <code>options</code> 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.</p>
<p>Edit: this doesn't exactly answer my question, but I was able to make it work:</p>
<p><code>$event-&gt;rules[] = [['options'], 'each', 'rule' =&gt; ['each', 'rule' =&gt; ['required']]];</code></p>

CC BY-SA 4.0
2023-05-24T00:08:04.263Z
2023-06-15T13:58:00.950Z
2023-06-15T13:58:00.950Z
supazu
<p>I have Line Item options that I'm trying to validate in a module. I'm using the example code from <a href="https://craftcms.com/docs/commerce/4.x/orders-carts.html#line-item-options-and-notes" rel="nofollow noreferrer">the docs here</a> on the front-end of the cart.</p>
<p>Below that it says:</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>Using that and the <a href="https://craftcms.com/docs/commerce/4.x/extend/events.html" rel="nofollow noreferrer">Event Generator</a> I've come up with this:</p>
<pre><code>Event::on(LineItem::class, Model::EVENT_DEFINE_RULES, function (DefineRulesEvent $event) {
  $event-&gt;rules[] = [['options'], 'required'];
});
</code></pre>
<p>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.</p>
<p>Can anyone please help me get this to work?</p>

CC BY-SA 4.0
2023-05-22T11:16:01.620Z
Where is your code located, is it inside the init method of you module?
Luke Holder
CC BY-SA 4.0
2023-05-22T12:54:09.377Z
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
CC BY-SA 4.0
2023-05-23T13:50:02.573Z
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
<plugin-craftcommerce><modules>
Commerce LineItem Option validation
# Commerce LineItem Option validation

(Asked by: username_0 on 2023-05-21)

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?

**Tags:** plugin-craftcommerce, modules

> **Comments:**
> **username_1**: Where is your code located, is it inside the init method of you module?  
> **username_0**: 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.  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_0 on 2023-05-24. Score: 1 | Accepted answer

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']]];`


---

