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>
Related
I'm stumped by this problem. I'm trying to display a class if a certain string is equals to the logged in username. However it doesn't seem to ever evaluate to true.
Here's the code in gsp
<g:if test="${it.from.username == sec.loggedInUserInfo(field: 'username')}">
<div class="direct-chat-msg right">
</g:if>
<g:else>
<div class="direct-chat-msg">
</g:else>
I also tried using this method
<div class="direct-chat-msg ${(it.from.username == sec.loggedInUserInfo(field: 'username')) ? 'right' : ''}">
However nothing I do can get the 'right' class to show up in the div.
Just for good measure, I printed out the values of both classes in my gsp in hidden fields.
<input type="hidden" value="${it.from.username}"/>
<input type="hidden" value="${sec.loggedInUserInfo(field: 'username')}"/>
And the values are exactly the same
<input type="hidden" value="u***#gmail.com">
<input type="hidden" value="u***#gmail.com">
I've tried several combinations of string comparison
.equals(), calling .toString() on both, also trying as String. however nothing seems to be working.
What could the issue be?
I have tried passing the logged in user object in the Model from the controller, and just calling loggedInUser.username and it works. So my question now is, what kind of object is returned by spring security?
The result of sec.loggedInUserInfo(field: 'username')} is an HTML encoded string. Spring security calls encodeAsHTML() before returning 'username' value. Visually they look the same but are they equal? Apparently not!
On my gsp-page I've dropdown list. The code below:
<g:select name="tablePlacesAvailable" from="${tableInfo}"
optionValue="${{it.placesInTableAmount}}"
optionKey="placesInTableAmount" value=""/>
It shows non-unique results. How to fix it?
You can modify your code as per following .
<g:select name="tablePlacesAvailable" from="${tableInfo.unique()}" optionValue="${{it.placesInTableAmount}}" optionKey="placesInTableAmount" value=""/>
I have a layout.gsp where I define some markup for a control (say, banner) that might be displayed on any page (or might not).
<g:set var="showBanner" value="${...}" scope="page|request|flash|session"/>
<!-- Some more logic that may g:set showBanner var -->
<g:layoutBody/>
<g:if test="${[flash|request|???].showBanner}">
<div id="banner">...</div>
</g:if>
The idea is to let the page, rendered by <g:layoutBody>, to decide if it wants the banner on it or not. So, one page may decide to always show banner, as following - page1.gsp:
<g:set var="showBanner" value="${true}" scope="page|request|flash|session"/>
Another page decides to never show banner on it, as following - page2.gsp:
<g:set var="showBanner" value="${false}" scope="page|request|flash|session"/>
Unfortunately, this approach doesn't work for me. I tried all different combinations of scope attribute and still cannot have it overridden in children pages.
Is this a wrong approach in general or I miss some detail?
I found what I was doing wrong. The issue is that variables set inside included page are evaluated before the layout.gsp code runs, not at the moment of <g:layoutBody/> tag insertion (or call).
In other words, dependencies are rendered before the layout, not at the moment layout directive is encountered. This may be intuitive for some, but not for others (not for me).
Another point is that you still need to use request scope to access the same var between pages (which is quite intuitive).
So, the solution becomes:
First in page1.gsp:
<g:set var="showBanner" value="${true}" scope="request"/>
Then in layout.gsp:
<!-- Doesn't matter were you put it, always evaluated first -->
<g:layoutBody/>
<g:if test="request.showBanner == null"> <!-- if not set by children page -->
<g:set var="showBanner" value="${...}" scope="request"/>
<!-- Some more logic that may g:set request.showBanner var -->
</g:if>
<g:if test="${request.showBanner}">
<div id="banner">...</div>
</g:if>
What about you set the variable inside an interceptor? Like one shown here: Accessing the model from a layout view in 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'] }" />
I have a form where you can select something from a selector and click and image this then fires to a controller. The problem is currently the images are submit buttons which i can send params with so i dont know which image has been clicked. my current code it as follows:
<g:form controller="profile" action='postFromAlbum'>
<g:select name="child" from="${names}" value="" noSelection="['':'All Children']"/> <br
<g:each in="${pictures}">
<g:submitButton name="submit" class="image3" type="image" src="${it.urlThumb}" alt="no Picture"></g:submitButton>
</g:each>
</g:form>
does anyone know a better way or know of a way to get around this?
One way is to create an analog of actionSubmit that will support params, like in this question.
Another hacky way is to set an invisible text/radio field and set its value in button's onclick.