The question title is enough to explain excepting the attempts that i try :
Attempt1 :
<g:link controller="staff" action="all" <g:if test="${actionName=='all' }">class="active"</g:if> >Overview</g:link>
Error Message
Error 500
Class :org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message Grails tag [g:link] was not closed
Attempt2 :
<g:link controller="staff" action="all" class="<g:if test="${actionName=='all' }">active</g:if>" >Overview</g:link>
Error Message
Error 500
Class org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException
Message Attribute value must be quoted (controller="staff" action="all" class="
How to do a branching to decide if an element has css class X or not ?
X(in this example)= active
I think It will be sufficient if you will use ternary operator e.g:
<g:link controller="staff" action="all" class="${actionName == 'all' ? 'active' : ''}">
Overview
</g:link>
Related
The code in controller is as follows:
for(o in options){
if(o){
if(!o.isInteger()){
don.errors.reject("The value was not integer")
render(view: "editdonation", model: [id:id, donation:don])
return
}
don.addToDenominations(o.toInteger())
}
}
I intentionally pass in invalid input so that the don.errors.reject() code is executed.
The editdonation.gsp has the following code to print the error in the donation object.
<ul class="inline-errors" role="alert">
<g:hasErrors bean="${donation}">
<div class="errors">
ok it has errors
</div>
</g:hasErrors>
</ul>
Why is the view not displaying the error? I appreciate any help!
The Errors.reject() method expects an error code rather than an error description.
Creating the error code
To create your own error code:
Edit the file grails-app/i18n/message.properties
Add a new line with your error code. Ex: option.donation.notinteger=The option value {0} was not an Integer
Save the file grails-app/i18n/message.properties
Using the error code
To use the error code, call Errors.reject() like this:
don.errors.reject('option.donation.notinteger', [o] as Object[], 'The value was not integer')
The code above passes the option to reject so that it can insert the value into the error message. And as required, it provides a fallback error message.
I have the next constraint in my domain object usuario/Usuario.groovy:
phone blank: false, matches: "[0-9 -+()]{3,15}"
And I have in i18n/messages_es.properties:
usuario.telefono.matches=Se admiten números, espacios y los caracteres: -+(). Se admiten entre 3 y 15 caracteres en total.
This is the error message if the phone doesn't match the pattern:
The translation of the message would be something like: "Please, adjust the requested format"
I have not any similar error message in any of the i18n/messages... files. It looks a client side error message as it appears instantly.
In create gsp there is the next code (I think the error message could come from here):
<g:hasErrors bean="${usuarioInstance}">
<bootstrap:alert class="alert-error">
<ul>
<g:eachError bean="${usuarioInstance}" 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>
</bootstrap:alert>
</g:hasErrors>
How could I change this error message by a custom one?
The first thing you have to understand: this message is html form validation error and doesn't connected to your application messages. So the fix will be in html. Try to add oninvalid attribut to input tag:
<input type="text" name="phone" pattern="[0-9 -+()]{3,15}" required="" value="" id="phone" oninvalid="setCustomValidity('Please, type valid phone number')">
You can move 'Please, type valid phone number' to i18n file
Edit: if you use fields plugin add oninvalid attr with input- prefix.
<f:field bean="${usuarioInstance}" property="phone" input-oninvalid="setCustomValidity('Please, type valid phone number')"/>
I think you are missing 'invalid' as part of the message key.
Use the following error code for matches:
className.propertyName.matches.invalid = 'errorMessage'..
Still if it doesn't work ..try the following approach.
Add def messageSource in controller /service.
user.errors?.allErrors?.each{
println messageSource.getMessage(it, null)
};
Go through the following link which explains this better
http://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html
I am trying to follow the ajax - driven select tutorial here: http://grails.org/AJAX-Driven+SELECTs+in+GSP however, I get the following error:
URI
/ajaxSelects/
Class
java.lang.NullPointerException
Message
Cannot invoke method list() on null object
I followed the tutorial exactly. The problem seems to be from the following code, where grails does not like Country.list():
<g:select
optionKey="id" optionValue="name"
name="country.nameid="country.name" from="${Country.list()}"
onchange="${remoteFunction(
controller:'country',
action:'ajaxGetCities',
params:'\'id=\' + escape(this.value)',
onComplete:'updateCity(e)')}"
></g:select>
Any ideas as to why this code is not working?
You either need to do a page import:
<%# page import="com.yourpackage.Country" %>
or use the full path for the list
from="${com.yourpackage.Country.list()}"
You also have mistyped here
name="country.nameid="country.name"
Should be
name="country.name" id="country.name"
Since we've updated to grails 2.0.1 (from 2.0.0) all of our beans shown via bean fields are incorrectly displayed as the first property of that "withBean" field. In the example I've posted below, all of [firstName, lastName, dateOfBirth, contactNumber] are shown as just 'firstName' (we know it's not just the messages which are wrong because otherwise the 3rd property (dateOfBirth) would be a date picker, not just a text field).
Any potential workarounds or suggestions?
(plugins.bean-fields=1.0-RC3)
I encountered the same problem, and have a work-around.
I has customised beanfield templates extracted into a gsp template called /shared/_beanfieldConfig.gsp , which I then included by rendering before calling any beans tags. e.g.
<g:render template="/shared/beanFieldConfig" />
<bean:withBean beanName='command'>
<bean:input property='username' />
This worked nicely in 1.3.7, and meant I could share beanFieldConfig between views.
When upgrading to 2.0.3, I enountered the same issue as the original question. I've found I can work around this by inlining the content of my _beanFieldConfig in each view.
Yuk, but at least it means I don't need rewrite all my views to use the replacement plugin (yet).
(edit)
Interestingly, although beanField config in a render'd template doesn't work, sticking it in a taglib DOES.
so, while previously I had in beanFieldConfig
<bean:inputTemplate>
<div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
<g:if test="${errors}">
${errors}
</g:if>
</div>
</bean:inputTemplate>
If I define the equivalent tag -
def beanFieldConfig = {
bean.inputTemplate { m ->
m.with {
""" <div class='input ${errors ? 'errors' : '' }'>
${label}
${field}
${errors ?: ''}
</div>"""}
}
}
and then in my gsp replace <g:render template="/shared/beanFieldConfig" /> with <g:beanFieldConfig/>, it works.
I'm using the pageProperty function to drive some of my menus that are in my layout. I need to apply specific classes to links depending on which meta.nav pageProperty returns. Right now, it looks like this...
<g:if test="${pageProperty(name:'meta.nav') == 'support'}">
<g:link class="selected" ...>support</g:link>
</g:if>
<g:else>
<g:link ...>support</g:link>
</g:else>
I'd like to clean this up, however, this does not work
<g:link class="${pageProperty(name:'meta.nav') == 'support' ? selected : null}" ...>support</g:if>
I've tried several different variations of paranthesis and none seem to get what I need. For example:
${(pageProperty(name:'meta.nav') == 'support') ? selected : null}
${(pageProperty(name:'meta.nav') == 'support' ? selected : null)}
Just can't seem to get it to act right. Any help is appreciated.
As a wild stab in the dark, how about:
${ pageProperty(name:'meta.nav').equals( 'support' ) ? 'selected' : null }
Not as groovy, but might be less confusing to the parser (it looks like something somewhere is getting confused and dumping == support out where it shouldn't)
I would try making the true condition a String:
${(pageProperty(name:'meta.nav') == 'support') ? 'selected' : null}
It may be trying to access a variable named selected within the GSP script, which would be undefined.
Hope that helps.