<p>I've achieved something similar but used <code>commerce_lineItems.onBeforeSaveLineItem</code>.</p>

<p>This event is fired every time a line item is created or edited. Fired on <code>/updateCart</code> (product page) or <code>/updateLineItem</code> (cart page).</p>

<p>You might run into problems because the lineItems in the event won't contain a post request unless its the lineItem that triggered the event through a form.</p>

<p>You need to grab the events line item and check it against the lineItemId in the post request before doing any comparisons - unless its a new lineItem.</p>

<pre><code>// Get the line item being saved from the event
$item = $event-&gt;params['lineItem'];

// Is this a new line item?
$isNewLineItem = $event-&gt;params['isNewLineItem'];

// Get the line item id that triggered the save from the post request
// (this will only exist if we're updated a line item that currently exists)
$lineItemId = craft()-&gt;request-&gt;getPost( 'lineItemId' );

// If the current line item being saved is new
// OR
// If its an existing line item and matches the one that triggered the event
if ( $isNewLineItem || $item-&gt;id == $lineItemId ) {
  //rest of the code
}
</code></pre>

<p>I then looked to see if the option was in the post request. </p>

<p><code>$text = craft()-&gt;request-&gt;getPost( 'options.Text' );</code></p>

<p>If it wasn't I added a flash message and stopped the action.</p>

<pre><code>$event-&gt;performAction = false;
craft()-&gt;userSession-&gt;setFlash( "itemError", "You must add some text before adding this product to your basket" );
</code></pre>

CC BY-SA 3.0
2016-06-28T16:24:54.420Z
If you run into problems with line items being new/edited check my latest edits
Luke Pearce
CC BY-SA 3.0
2016-06-28T16:18:41.747Z
2016-06-29T14:11:36.343Z
2016-06-29T14:11:36.343Z
Luke Pearce
<p>I have a lineItem option that needs validation. It’s actually an input field that can’t be blank, and different to the lineItem’s note.</p>

<p>The docs for <a href="https://craftcommerce.com/docs/add-to-cart#line-item-options-and-notes" rel="nofollow">Add to Cart</a> mention using <a href="https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart" rel="nofollow">onBeforeAddToCart</a> but it also needs to be validated if a customer decides to edit this field in the cart so perhaps <code>commerce_cart.onBeforeAddToCart</code> is not the right event here.</p>

<p>Can anyone offer an example of how to do this? This needs to be server-side validation, not front-end.</p>

<plugin-development><plugins><plugin-craftcommerce>
Validate a lineItem option within a plugin?
# Validate a lineItem option within a plugin?

(Asked by: username_0 on 2016-06-28)

I have a lineItem option that needs validation. It’s actually an input field that can’t be blank, and different to the lineItem’s note.

The docs for [Add to Cart](https://craftcommerce.com/docs/add-to-cart#line-item-options-and-notes) mention using [onBeforeAddToCart](https://craftcommerce.com/docs/events-reference#commerce_cart.onbeforeaddtocart) but it also needs to be validated if a customer decides to edit this field in the cart so perhaps `commerce_cart.onBeforeAddToCart` is not the right event here.

Can anyone offer an example of how to do this? This needs to be server-side validation, not front-end.

**Tags:** plugin-development, plugins, plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2016-06-28. Score: 5 | Accepted answer

I've achieved something similar but used commerce_lineItems.onBeforeSaveLineItem.
This event is fired every time a line item is created or edited. Fired on /updateCart (product page) or /updateLineItem (cart page).
You might run into problems because the lineItems in the event won't contain a post request unless its the lineItem that triggered the event through a form.
You need to grab the events line item and check it against the lineItemId in the post request before doing any comparisons - unless its a new lineItem.
// Get the line item being saved from the event
$item = $event->params['lineItem'];

// Is this a new line item?
$isNewLineItem = $event->params['isNewLineItem'];

// Get the line item id that triggered the save from the post request
// (this will only exist if we're updated a line item that currently exists)
$lineItemId = craft()->request->getPost( 'lineItemId' );

// If the current line item being saved is new
// OR
// If its an existing line item and matches the one that triggered the event
if ( $isNewLineItem || $item->id == $lineItemId ) {
  //rest of the code
}

I then looked to see if the option was in the post request. 
$text = craft()->request->getPost( 'options.Text' );
If it wasn't I added a flash message and stopped the action.
$event->performAction = false;
craft()->userSession->setFlash( "itemError", "You must add some text before adding this product to your basket" );

> **Comments:**
> **username_1**: If you run into problems with line items being new/edited check my latest edits  

---

