Show lineItem error(s) in template? 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 to add items to the cart.

I’m validating lineItem data using onBeforeSaveLineItem and adding the error with something similar to $lineItem-&gt;addError('text','This is required');.
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()-&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' );
}


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