title: Spring Thymeleaf - Conditional Expressions in th:field
body: <p><a href="http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#conditional-expressions" rel="nofollow noreferrer">Conditional Expression (docs):</a></p>
  
  <pre><code>&lt;tr th:class="${row.even}? 'even' : 'odd'"&gt; 
  </code></pre>
  
  <hr>
  
  <p>I want to use conditional expression within a th:field. But every time I try it I get the following error:</p>
  
  <blockquote>
    <p>*only variable expressions ${...} or selection expressions <em>{...} are allowed in Spring field bindings</em></p>
  </blockquote>
  
  <hr>
  
  <p>For example:</p>
  
  <pre><code>// This works fine.
  &lt;input type="text" th:value="${object.covered} ? 'yes' : 'no'" /&gt;
  
  // This on the other hand, generates the error mentioned earlier.
  // Which does make sense, cause it would otherwise generate invalid attributes.
  &lt;input type="text" th:field="${object.covered} ? 'yes' : 'no'" /&gt;
  
  // Combining the two does not work.
  &lt;input type="text" th:field="${object.covered}" th:value="${object.covered} ? 'yes' : 'no'" /&gt;
  </code></pre>
  
  <hr>
  
  <p>Basically, what I want, is to create a th:field where its value is determined by a conditional expression.</p>
  
  <p>More specific, In my implementation I want to populate a input field with a number (Java long) from my model. And if that number is zero or lesss, I want to use a placeholder instead.</p>
  
  <pre><code>// Ultimateley, what I want to achieve is something like this.
  &lt;input type="text" th:field="${person.age}" 
         th:value="${person.age} le 0 ? null : ${person.age}" 
         placeholder="age" /&gt;
  </code></pre>
  
  <p>How to use th:field, and determine its value with a conditional expression with Spring Thymeleaf?</p>
  
  <p>(Thymeleaf 2.1.5, and Spring Boot 1.4.2)</p>