Is it possible to put multiple conditions in a th:classappend attribute - thymeleaf

I've been racking my brains on this for a while and I'm pretty sure I'm getting the syntax wrong.
Basically I have a <div> and want it to have the class="hidden" unless two thymeleaf conditions are met.
I want the class 'hidden' unless there's an error present on the input.
This works on it's own:
th:classappend="${#fields.hasErrors('month') == false} ? 'hidden'
I want the class hidden unless the value of the input is not null.
This works on it's own:
th:classappend="${applicationData.month == null} ? 'hidden'"
Is there anyway to concat these two conditions into one th:classappend? I'm struggling with getting that working at the moment. Something like this doesn't seem to work for me
<div id="date-met" class="date-container" th:classappend="${#fields.hasErrors('month') == false} ? 'hidden' || ${applicationData.month == null} ? 'hidden'" >
<input id="month" name="month" type="text" th:value="${applicationData.month}">
</div>
Thanks in advance and apologies if it's a stupid question!

You can use:
( condition_one || condition_two ) ? 'hidden'
So in your case that would be:
th:classappend="${ ( #fields.hasErrors('month') == false || applicationData.month == null ) ? 'hidden' }">
The parentheses may not even be necessary - just added for clarity.

Related

In a Thymeleaf `th:text` tag, how do I show different text on `if int == -1`?

I have this in a Thymeleaf web page ...
<span th:text="${account.balance}">balance</span>
When the account.balance == -1 that means unknown, so I want to display text "Unknown".
How do I do an if account.balance == -1 show 'Unknown' in Thymleaf?
There are a whole lot of ways to do this... I would just suggest reading through the Thymeleaf conditional evaluation documentation.
I'd probably do something like this, myself:
<th:block th:switch="${account.balance}">
<span th:case="-1">Unknown</span>
<span th:case="*" th:text="${account.balance}" />
</th:block>
You could also do some thing like this:
<span th:if="${account.balance == -1}">Unknown</span>
<span th:unless="${account.balance == -1}" th:text="${account.balance}" />
Or you could build a String:
<span th:text="${account.balance == -1 ? 'Unknown' : account.balance}" />
This should work: <span th:if="${account.balance == -1}">Unknown</span>
But this : page will be very much helpful for conditionals in general

Thymeleaf - Always keep the HTML element but conditionally show a value

I need Thymeleaf to always include a label element but only conditionally show a value for it.
If message.type is equal to warning it should show the message.text. Otherwise, the HTML DOM should still contain the label element.
I've tried this but then the label element is missing from the HTML when the message.type is not equal to warning.
<label id="message" th:if="${message.type == 'warning'}"
th:value="${message.text}" th:text="${message.text}"></label>
I'm trying to accomplish something like this:
<label id="message" th:value="${message.type=='warning' ?
message.text: ''}" th:text="${message.type=='warning'?
message.text: ''"></label>
If the message.type is warning, I would expect HTML like this:
<label id="message">My warning message</label>
Otherwise, I would like to have HTML like this:
<label id="message"></label>
Many different ways to accomplish this. You already have one that I would expect to work. (why do you say it doesn't work?) Also, I'm not sure why you are including th:value in your tags (I'm including them to match your question).
<label
id="message"
th:value="${message.type == 'warning'? message.text : ''}"
th:text="${message.type == 'warning'? message.text : ''}"></label>
You could also do something like this:
<label th:if="${message.type == 'warning'}" id="message" th:value="${message.text}" th:text="${message.text}"></label>
<label th:unless="${message.type == 'warning'}" id="message"></label>
or like this (assuming an extra span wouldn't mess up the markup you are wanting):
<label id="message"><span th:if="${message.type == 'warning'}" th:text="${message.text}" /></label>

Test if error validation text match a string using if statement doesn't works

I have a validation in my User model which check an input text if it have a value or not :
validate :picture_name
...
...
...
def picture_name
errors[:picture].push "no_name" if namePicture.blank?
end
html text field:
<input type="text" name="namePicture" />
in my view i want to check if that #user.errors[:picture] contains only 1 error and the error text is "no_name" (no_name is the error added by picture_name validation) :
<% if #user.errors.count.eql? 1 && #user.errors['picture'] == "no_name" %>
<h1> test works </h1>
<% else %>
<h1> test doesn't works </h1>
<% end %>
but i don't know why it always shows me "test doesn't works" even when errors['picture'] contains just (one) "no_name" as error !
there is some error in my if statement ? or what ?
thank you
i found the solution if it interest someone, in my above statement :
if #user.errors.count.eql? 1 && #user.errors['picture'] == "no_name"
i use && operator which is high precedence, this means that my statement look like this :
if #user.errors.count.eql? ( 1 && #user.errors['picture'] == "no_name" )
or more explicitly
if #user.errors.count.eql? true
which is incorrect
so, the solution is :
using parentheses :
if (#user.errors.count.eql? 1) && (#user.errors['picture'] == "no_name")
or using "AND" operator which is lower precedence:
if #user.errors.count.eql? 1 and #user.errors['picture'] == "no_name"
I guess it has to do with setting errors with a symbol (:picture), but testing on a string ('picture') + errors[:picture] contains an array, not a string

Remove HTML tag attribute in slim when attribute should not be displayed

I would like to remove class attribute when class should not be displayed in Slim.
In ERB, I could use:
<input <%= "class='foo'" if false %> />
<input />
How do I do this in Slim?
I found this, but I feel there must be a more idiomatic solution:
| <input "#{'class=\"foo\"' if false}" />
If the value of an attribute is nil, then the entire attribute will be omitted (actually this is happens for nil or false for most cases, but it looks like the class attribute behaves differently for false and true):
input class=('foo' if condition)
This will output
<input class="foo" />
if condition is true, and
<input />
if condition is false.
You can use the splat (*) operator to help define conditional attributes for tags in slim, using hashes containing the attributes to be added.
http://www.rubydoc.info/gems/slim/frames#Splat_attributes__
The splat operator will expand a hash into a set of attributes to be added to the tag. If the hash is empty, no attributes will be added.
For example,
- admin_classes = #User.admin? ? {class: "foo"} : {}
input *admin_classes
if #User.admin? == true, it should render
<input class="foo">
else if #User.admin? == false, it should render
<input>
For attributes like "class" or other attributes that have attribute merging turned on, you can also do something like this:
- admin_classes = #User.admin? ? {class: ["foo","bar"]} : {}
input *admin_classes class="biz"
if #User.admin? == true, it should render
<input class="foo bar biz">
else if #User.admin? == false, it should render
<input class="biz">

ASP.NET MVC: Optionally render element attribute

I know there is some syntax with ? to optionally render attribute.
But I can't find it now that I need it ...
Something like:
< input checked=<%? model.IsActivated % > ...
Thanks.
EDIT: Since it seems that nobody knows... perhaps it was some different view engine.
Thanks for answers any way :)
<input checked="<%= model.IsActivated ? "checked" : string.empty % >"...
But the presense of the "checked" attribute might cause it to be checked, I can't remember. But if that's the case then you'll want
<input <%= model.IsActivated ? "checked=\"checked\"" : string.Empty %> ...
EDIT: btw its called the ternary(? or conditional) operator

Resources