wimdeblauwe / htmx-spring-boot

Spring Boot and Thymeleaf helpers for working with htmx
Apache License 2.0
521 stars 49 forks source link

hx:vals not working with multiple key-values #119

Closed sivaprasadreddy closed 6 months ago

sivaprasadreddy commented 6 months ago

While using hx:vals with one key value as mentioned in the docs it is working fine.

<button hx-post="/add-to-cart" hx:vals="${ {code: product.code} }">Add to Cart</button>

But, if I add more than one key-value pairs, it is throwing error as follows:

<button hx-post="/add-to-cart" hx:vals="${ {code: product.code} , {price: product.price} }">Add to Cart</button>
Caused by: org.attoparser.ParseException: Exception evaluating SpringEL expression: " {code: product.code} , {price: product.price} " (template: "partials/products" - line 35, col 37)
    at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
    at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
    ... 52 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: " {code: product.code} , {price: product.price} " (template: "partials/products" - line 35, col 37)
    at org.thymeleaf.spring6.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:292)
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
....
....
....
Caused by: org.springframework.expression.spel.SpelParseException: Expression [ {code: product.code} , {price: product.price} ] @22: EL1041E: After parsing a valid expression, there is still more data in the expression: 'comma(,)'
    at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:144)
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:63)
    at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:34)
    at org.springframework.expression.common.TemplateAwareExpressionParser.parseExpression(TemplateAwareExpressionParser.java:56)

Just FYI: The following is working fine:

<button hx-post="/add-to-cart" th:hx-vals='|{"code":"${product.code}", "price":"${product.price}" |'>Add to Cart</button>
wimdeblauwe commented 6 months ago

Can you try like this:

hx:vals="${ {code: product.code , price: product.price} }"

This is just a first idea, I'm not sure it will work. If it does not, I will investigate in more detail.

wimdeblauwe commented 6 months ago

I just checked and it should work like I proposed. I created #120 to add this info to the documentation.

sivaprasadreddy commented 6 months ago

@wimdeblauwe It is working fine with hx:vals="${ {code: product.code , price: product.price} }". Thanks.