Answers.Body: <p>To repopulate the form fields you can use the data from the post request using something like:</p>
  
  <p><code>value="{{ craft.request.getPost( 'options.text' ) }}"</code></p>
  
  <p>I also think I've got a rough solution for handling the errors.</p>
  
  <pre><code>//Add the cart to your event
  $cart = craft()-&gt;commerce_cart-&gt;getCart();
  
  //Check for field/fields
  $text = craft()-&gt;request-&gt;getPost( 'options.text' );
  
  //Check if it's got a value
  if ( $text === '' )
  {
    $event-&gt;performAction = false;
  
    //Build the handle we'll attach the error to
    //Use the purchasable id (variant) and then add the field we're checking for
    $lineItemErrorHandle = $item-&gt;purchasable-&gt;id . '-options[text]';
  
    //Add the error to the cart
    $cart-&gt;addError( $lineItemErrorHandle, 'The text is required' );
  }
  </code></pre>
  
  <p>Then in your template you display it with the following where <code>product.defaultVariant.id</code> needs to be the id of the purchasable that attempted to be added to the cart.</p>
  
  <pre><code>{% set string = product.defaultVariant.id ~ '-options[text]' %}
  {% if cart.getError( string ) %}
    We've got an error its {{ cart.getError( string ) }}
  {% endif %}
  </code></pre>
  
  <p>I had to build the string outside of the <code>.getError</code> method because string interpolation wouldn't work using <code>'#{product.defaultVariant.id}-options[text]'</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T14:58:01.050Z
Answers.Text: If the form has multiple items there could be multiple options.text fields though, some valid and some not, would this work?
Answers.UserDisplayName: James
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T14:59:44.417Z
Answers.Text: What does the post request look like for multiple items? The purchasable id makes each error added to the cart unique to the item that through the error. I have only tested this with a single product page though.
Answers.UserDisplayName: Luke Pearce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T15:09:36.827Z
Answers.Text: We’re using MultiAdd so the post looks like `items[num][options][text]` –  Perhaps we could loop through `items[]` and match the `purchasable->id`?
Answers.UserDisplayName: James
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T15:10:06.140Z
Answers.Text: Looking at the MultiAdd docs it looks like you can use `{{ craft.request.getPost( items[{{ loop.index }}][options][text] ) }}` to populate the fields. In the plugin you would then loop through each item in the `items` post array and do the same as if it were a single item.
Answers.UserDisplayName: Luke Pearce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T15:45:23.147Z
Answers.Text: Also, having trouble accessing the post data as `'options.text'` actually has a name containing spaces. For example, `option[Item text]`. How do you target that in a template?
Answers.UserDisplayName: James
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T15:49:21.093Z
Answers.Text: `'options.text'` is the syntax within the plugin. Perhaps take a look at the dev tools to see how the post request is formatted for use inside the template (using `craft.request.getPost()`)
Answers.UserDisplayName: Luke Pearce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-30T09:39:17.480Z
Answers.Text: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/41859/discussion-between-luke-pearce-and-james).
Answers.UserDisplayName: Luke Pearce
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-06-29T14:40:01.710Z
Answers.LastActivityDate: 2016-06-29T14:47:59.893Z
Answers.LastEditDate: 2016-06-29T14:47:59.893Z
Answers.UserDisplayName: Luke Pearce
Body: <p>If I have a lineItem that needs some validation, for example on an option or note filed, how do I show potential errors in a template and repopulate the form with the posted values?</p>
  
  <p>In addition, I’m working with a template that has multiple products on a page and uses <a href="https://github.com/engram-design/MultiAdd" rel="nofollow">MulitAdd</a> to add items to the cart.</p>
  
  <p>I’m validating lineItem data using <code>onBeforeSaveLineItem</code> and adding the error with something similar to <code>$lineItem-&gt;addError('text','This is required');</code>.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T13:50:02.463Z
Comments.Text: This is a quick guess before testing - but have you tried `$lineItem->addError('options[text]','This is required');`
Comments.UserDisplayName: Luke Pearce
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T14:03:30.800Z
Comments.Text: You can't attach errors to `$lineItem` because it doesn't exist - you stop it from being created if the validation fails. The above will work if the line item exists in the cart and will show the error message with `{{ item.getError( 'options[text]' ) }}`
Comments.UserDisplayName: Luke Pearce
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T14:11:36.793Z
Comments.Text: Very good point, yes. I’m not adding the item though if there’s an error. `$event->performAction = false;` gets set instead.
Comments.UserDisplayName: James
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T14:13:26.620Z
Comments.Text: Trying to find a solution where you attach errors to the cart - would be fine if there was only ever one item on a page, but you state there are multiple? If it were single you could just use `$cart->addError('lineItemOptions[text]','This is required');` Seems messy though.
Comments.UserDisplayName: Luke Pearce
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-06-29T14:18:31.950Z
Comments.Text: Thanks. Yep, multiple items in the add to cart form. I may need to fall back to a general message, which could work but no ideas yet on how to repopulate the form if it fails validation. As you say lineItems don’t exist, and creating a custom model seems messy.
Comments.UserDisplayName: James
Tags: <templating><plugin-craftcommerce>
Title: Show lineItem error(s) in template?
ThreadText: # Show lineItem error(s) in template?
  
  (Asked by: username_0 on 2016-06-29)
  
  If I have a lineItem that needs some validation, for example on an option or note filed, how do I show potential errors in a template and repopulate the form with the posted values?
  
  In addition, I’m working with a template that has multiple products on a page and uses [MulitAdd](https://github.com/engram-design/MultiAdd) to add items to the cart.
  
  I’m validating lineItem data using `onBeforeSaveLineItem` and adding the error with something similar to `$lineItem->addError('text','This is required');`.
  
  **Tags:** templating, plugin-craftcommerce
  
  > **Comments:**
  > **username_1**: This is a quick guess before testing - but have you tried `$lineItem->addError('options[text]','This is required');`  
  > **username_1**: You can't attach errors to `$lineItem` because it doesn't exist - you stop it from being created if the validation fails. The above will work if the line item exists in the cart and will show the error message with `{{ item.getError( 'options[text]' ) }}`  
  > **username_0**: Very good point, yes. I’m not adding the item though if there’s an error. `$event->performAction = false;` gets set instead.  
  > **username_1**: Trying to find a solution where you attach errors to the cart - would be fine if there was only ever one item on a page, but you state there are multiple? If it were single you could just use `$cart->addError('lineItemOptions[text]','This is required');` Seems messy though.  
  > **username_0**: Thanks. Yep, multiple items in the add to cart form. I may need to fall back to a general message, which could work but no ideas yet on how to repopulate the form if it fails validation. As you say lineItems don’t exist, and creating a custom model seems messy.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-06-29. Score: 3 | Accepted answer
  
  To repopulate the form fields you can use the data from the post request using something like:
  
  `value="{{ craft.request.getPost( 'options.text' ) }}"`
  
  I also think I've got a rough solution for handling the errors.
  
  ```
  //Add the cart to your event
  $cart = craft()->commerce_cart->getCart();
  
  //Check for field/fields
  $text = craft()->request->getPost( 'options.text' );
  
  //Check if it's got a value
  if ( $text === '' )
  {
    $event->performAction = false;
  
    //Build the handle we'll attach the error to
    //Use the purchasable id (variant) and then add the field we're checking for
    $lineItemErrorHandle = $item->purchasable->id . '-options[text]';
  
    //Add the error to the cart
    $cart->addError( $lineItemErrorHandle, 'The text is required' );
  }
  
  ```
  
  Then in your template you display it with the following where `product.defaultVariant.id` needs to be the id of the purchasable that attempted to be added to the cart.
  
  ```
  {% set string = product.defaultVariant.id ~ '-options[text]' %}
  {% if cart.getError( string ) %}
    We've got an error its {{ cart.getError( string ) }}
  {% endif %}
  
  ```
  
  I had to build the string outside of the `.getError` method because string interpolation wouldn't work using `'#{product.defaultVariant.id}-options[text]'`
  
  > **Comments:**
  > **username_0**: If the form has multiple items there could be multiple options.text fields though, some valid and some not, would this work?  
  > **username_1**: What does the post request look like for multiple items? The purchasable id makes each error added to the cart unique to the item that through the error. I have only tested this with a single product page though.  
  > **username_0**: We’re using MultiAdd so the post looks like `items[num][options][text]` –  Perhaps we could loop through `items[]` and match the `purchasable->id`?  
  > **username_1**: Looking at the MultiAdd docs it looks like you can use `{{ craft.request.getPost( items[{{ loop.index }}][options][text] ) }}` to populate the fields. In the plugin you would then loop through each item in the `items` post array and do the same as if it were a single item.  
  > **username_0**: Also, having trouble accessing the post data as `'options.text'` actually has a name containing spaces. For example, `option[Item text]`. How do you target that in a template?  
  > **username_1**: `'options.text'` is the syntax within the plugin. Perhaps take a look at the dev tools to see how the post request is formatted for use inside the template (using `craft.request.getPost()`)  
  > **username_1**: Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/41859/discussion-between-username_1-and-username_0).  
  
  ---
  
  