Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-24T00:08:04.263Z
Answers.LastActivityDate: 2023-06-15T13:58:00.950Z
Answers.LastEditDate: 2023-06-15T13:58:00.950Z
Answers.UserDisplayName: supazu
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-05-22T11:16:01.620Z
Comments.Text: Where is your code located, is it inside the init method of you module?
Comments.UserDisplayName: Luke Holder
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-05-22T12:54:09.377Z
Comments.Text: 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.
Comments.UserDisplayName: supazu
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-05-23T13:50:02.573Z
Comments.Text: 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.
Comments.UserDisplayName: supazu
Tags: <plugin-craftcommerce><modules>
Title: Commerce LineItem Option validation
ThreadText: # 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](https://craftcms.com/docs/commerce/4.x/orders-carts.html#line-item-options-and-notes) 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](https://craftcms.com/docs/commerce/4.x/extend/events.html) 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, username_1. 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']]];`
  
  
  ---
  
  