How do I add args to custom i18n messages in grails? - grails

I have an object which I declared as "unique:true" and then I created a custom message in message.properties in grails. It appears correctly, but I don't know how to pass arguments to it. Is it even possible?
net.mypackage.MyObject.myField.unique = The Object {0} already belongs to the Object Group {1}.
I am aware that I can pass args via "g:message" but in this case I am rendering my errors via this snippet:
<g:hasErrors bean="${object}">
<ul class="errors no-margin" role="alert">
<g:eachError bean="${object}" var="error">
<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
</g:eachError>
</ul>
</g:hasErrors>
I can probably hack it like say do an if-else to determine if the error was a "unique constraint" error then pass the values, but other than that I don't have any idea. Most of the custom message examples I see on the internet are related to custom validators. I tried.. But I dont want to resort to that just yet... Any opinions on how to do this?

From the Grails documentation (http://grails.org/doc/latest/guide/i18n.html):
<g:message code="my.localized.content" args="${ ['Juan', 'lunes'] }" />

Related

adding tooltip to zendesk home_page.hbs

I want to add tooltips for some "Categories" box in Zendesk, so when the user hang over one box then I can show a tooltip with more information about the "Category".
Problem is that I'm trying to use IF statement like this:
{{#if href == '/hc/en-us/categories/360001117812-Cow-Traffic' }}
{{#each categories}}
{{#if ../has_multiple_categories}}
<li class="blocks-item">
<a href='{{url}}' class="blocks-item-link">
{{#if href == 'url_to_compare' }}
<a class="tooltiptext">tooltip_text</a>
{{/if}}
<span class="blocks-item-title">{{name}}</span>
<span class="blocks-item-description">{{excerpt description}}</span>
As this IF is inside of a {{#if}} of the categories I want to select in this example only one link using the href to see if the mouse is over or not the box.
I already tried to add a helper in the script.js file but looks like it's not working, I remember that there was a way to use this IF and IS to be able to solve it.
Thanks!
I get a lot of help on this, at the end I found that the better way to solve this is using a {{#is}} statement, So it's possible to use {{#is name "Category Name"}} {{/is}}
And this option will do a comparation between the name of the Category and the name you are looking for, if the value it's equal then you can do something inside, if not then you can use an {{else}} between and do another thing over there.
{{#is name "Category Name"}}
<div class="tooltip1">{{name}}<span class="tooltiptext">{{excerpt description}}</span></div>
{{else}}
<span class="blocks-item-title">{{name}}</span>
{{/is}}
I used the "Excerpt description" to add the value of the tooltip inside Zendesk, and no need to add it manually in the home_page.hbs code.
I added 3 different tooltips and working fine now. Thank you all!

thymeleaf Property or field cannot be found on null

if ${object} is null, then its items visited by *{item} would cause:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'item' cannot be found on null
So how to solve this and I want to have the div structure remained, like in the following example, when currentUser is null, div containing city and name remains.
<div class="form" th:object="${currentUser}">
<div class="form-group form-group-sm">
<label>city:</label>
<span id="details_city" th:text="*{address == null ? '' : address.city}">Hongkong</span>
</div>
<div class="form-group form-group-sm">
<label>name:</label>
<span id="details_username" th:text="*{name}">Jane</span>
</div>
</div>
I've seen such a situation in a project (but not a spring project). The approach was that all users, even anonymous (not authenticated), were represented by a User (e.g. or CurrentUser) instance. This instance was mostly empty for anonymous users. The idea was to get rid of null checks. There was a dedicated method in the User class that answered if the represented user is authenticated or not. IMO the approach worked decently in the context of the technology stack that was used.
If feasible in for your project you may also try the idea. You may create an empty currentUser instance for anonymous users. Then currentUser will never be null. But if you use spring security + thymeleaf security dialect you may use thymeleaf to determine if the user is anonymous if this is necessary. E.g. a sample logout link visible for logged in users only may be rendered like this:
<li sec:authorize="isAuthenticated()">
<a href="#" class="log-out-link">
<i class="fa fa-sign-out"></i> Log out
</a>
</li>
A #ControllerAdvice and #ModelAttribute may push the user representation in all relevant views. Alternatively you may provide a separate, empty user in the view and use something like th:object="${#objects.nullSafe(currentUser,emptyUser)}"
If this is too much then do not use the th:object and add some extra checks.
In my opinion, you've got two options:
Pass an object instance anyway (with null values for the fields) or
Duplicate your div structure, one with th:if="${object != null}" and one with th:if="${object == null}

Thymeleaf th tag in blc tag

my problem is following:
in this code
<header>
<h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1>
</header>
<blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" />
<ul th:if="${not #lists.isEmpty(menuItems)}">
<li th:each="menuItem : ${menuItems}">
<a th:href="#{${menuItem.url}}" th:class="${menuItemStat.first}? 'home'">
<span th:utext="${menuItem.label}"></span>
</a>
</li>
</ul>
the line <h1 th:utext="${BLC_PAGE.pageFields[menu_name]}"></h1> gets the name of actual menu. I need the menuName in this line <blc:menu resultVar="menuItems" menuName="${BLC_PAGE.pageFields[menu_name]}" /> to do the same thing, but "${BLC_PAGE.pageFields[menu_name]}" is not appropriate, as it is from thymeleaf tags library. Any idea how to get this this value in blc:menu tag?
In order to resolve the Thymeleaf expression ${BLC_PAGE.pageFields[menu_name]}, you will need to wrap it like so: th:attr="menuName=${BLC_PAGE.pageFields[menu_name]}". Because menuName is not a standard HTML attribute, you won't be able to simply add the Thymeleaf namespace to menuName (i.e., th:menuName), and, without the th: namespace, Thymeleaf doesn't know that it needs to resolve any expressions. The Thymeleaf attr attribute allows you to use non-standard HTML attributes with Thymeleaf. For more information on setting attribute values using Thymeleaf you can review their documentation.
Here are links to the Broadleaf and Thymeleaf documentation on Processors and Dialects:
Thymeleaf: http://www.thymeleaf.org/doc/tutorials/2.1/extendingthymeleaf.html#dialects-and-processors

Only display first error in gsp

I'm performing inline validation on fields as the user tabs between them.
A problem occurs when there is more than one error against a field i.e both errors are shown.
I only want to show one error (The first one for arguments sake).
Is there are different tag to deal with this?
<jqvalui:renderError for="title">
<g:eachError bean="${objInstance}" field="title"><g:message error="${it}" /></g:eachError>
</jqvalui:renderError>
Thanks
So essentially you just have to use the errors themselves instead of using the tags provided for you.
<g:hasErrors bean="${objInstance}" field="title">
<g:message error="${objInstance.errors.getFieldErrors("title")[0]}" />
</g:hasErrors>
I know it's like a hack but if no exact solutions...
Consider adding a flag or a counter and set/test it inside the loop:
<g:set var="isErrorShown" value=""/>
<g:eachError bean="${objInstance}" field="title">
<g:if test="${!isErrorShown}">
<g:message error="${it}"/>
<g:set var="isErrorShown" value="TRUE"/>
</g:if>
</g:eachError>

How to disable development stage messages?

I like to know if it is posible disable the error messages that appear at the bottom of the page when I have some JSF syntax error:
<ul title="Project Stage[Development]: Unhandled Messages"
id="javax_faces_developmentstage_messages">
<li style="Color: orange">
The button/link/text component needs to have a Form in its ancestry. Please
add <h:form>.
</li>
</ul>
And you can't say "add the form tag".
Remove the <context-param> with name of javax.faces.PROJECT_STAGE from your web.xml, or set its value to Production instead of Development.
See also:
The form component needs to have a UIForm in its ancestry. Suggestion: enclose the necessary components within <h:form>

Resources