Grails GSP null safety check trouble - grails

I am trying to populate a text field on my GSP as such:
<label>Phone(aaa-bbb-cccc):</label>&nbsp<g:textField name="phone" style ="border-radius: 5px"
value="${recordToEdit.telephones = [] ? null : recordToEdit.telephones.first()}"></g:textField><br>
but it still tell me I can't access first() on an empty list. telephones is a List of Strings each being a phone number.

as #gross-jonas pointed out, the recordToEdit.telephones = [] ? .. : .. is terribly wrong already, unless it's a typo
the check you are trying to make should look like:
value="${recordToEdit.telephones ? recordToEdit.telephones.first() : ''}"
or
value="${recordToEdit.telephones?.getAt( 0 ) ?: ''}"

You can just use the Null Safe Operator (?.) as
${recordToEdit.telephones?.first()}
for null checks, which is not sufficient.
UPDATE
for empty list checks and null checks,
${ recordToEdit.telephones ? recordToEdit.telephones[0] : '' }
will be good.

Dude, didn't you just mean == instead of = ?
It looks like you are overwriting your telephones which get issued successfully instead of comparing it.

Related

issue with arguments to messages.properties, all numbers except zero work correctly

In my Grails 2.4.4 app I am using messages.properties for internationalization, with the following value:
my.app.thing.allowance(s)={0} Allowance(s)
and it is being using in a gsp like so:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.size()}"/>
the message is correctly displayed for any values greater than 0, for example if item.allowances.size() == 4 then the message displayed is 4 Allowances(s)
the issue is that if item.allowances.size() == 0 then the message displayed is {0} Allowance(s)
I have tried to write the args in several different ways, for example:
<g:message code="my.app.thing.allowance(s)" args="${item.allowances.isEmpty() ? 0.intValue() : item.allowances.size()}"/>
I have debugged things and I am sure that item.allowances.size() == 0 but for some reason it can not handle a value of 0 properly. What is the correct way to pass an argument with an int value of 0 to messages.properties?
In g.message arguments are always passed as an List.
From: http://docs.grails.org/3.0.17/ref/Tags/message.html
args (optional) - A list of argument values to apply to the message when code is used.
Try this code instead:
<g:message code="my.app.thing.allowance(s)" args="[item.allowances.size()]"/>
The Bharat's answer is correct, but I want to add why it happened so:
You have passed args=0
And here it is code from message tag lib:
List args = []
if (attrs.args) {
args = attrs.encodeAs ? attrs.args as List : encodeArgsIfRequired(attrs.args)
}
In groovy 0 is false, that's why you didn't have filled in message in case of ZERO

Append strings if not nil

In the view of a rail's app, I want to achieve:
If user's height isn't nil: return the height value with string "cm"
Otherwise: return string "N/A".
I'm wondering if there's a way to do this. The code below:
user.try(:profile_name).try(:push("as Alias")) || "N/A"
isn't working. The part try(:push("cm")) gives me an error. I thought of using the << operator to append strings by using , but I think there should be a neater way to complete this. Is there anyone who can give me a hint?
--another similar example which I want to accomplish but not working:
user.try(:height).try(:to_s).try(:push("cm")) || "N/A"
How about:
(user && user.profile.present?) ? "#{user.profile} as Alias" : 'N/A'

Why value is viewing with [] in grails view

I am calling an action by remoteFunction for showing some value in some field.The value is viewing but with in []. I have no idea why it is behaving like this. Can anyone please help me on this please ? I am using grails 2.1.0. here are my attempts below :
my remoteFunction >>
<g:remoteFunction action="setValueForDetails" params="'procurementMasterId='+procurementMasterId" update="changedValue"/>
my action in controller >>
def setValueForDetails(){
def otmIFQDetailsByProcurementMaster
if(params.procurementMasterId != null && params.procurementMasterId != "" && params.procurementMasterId != "null"){
otmIFQDetailsByProcurementMaster = commonService.getOtmIFQDetailsValueByProcurementMaster(Long.parseLong(params.procurementMasterId))
}
render (template: 'ifqDetails', model: [otmIFQDetailsByProcurementMaster: otmIFQDetailsByProcurementMaster])
}
my field where I want to set the value in template >>
<g:textField id="PROCUREMENT_TYPE" name="PROCUREMENT_TYPE.id" readonly="" value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE}" class="form-control" />
I guess the 'PROCUREMENT_TYPE" is an Array of enums due to spelling, and displaying. So if You want to 'print' value without square brackets, You should change value to (if You want only first result):
value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE[0]}"
or if You want to should more than one element from list:
value="${otmIFQDetailsByProcurementMaster?.PROCUREMENT_TYPE.toString().replace('[', '').replace(']', '')}"
or simply iterate through the elements of PROCUREMENT_TYPE and show as many textfield as many PROCUREMENT_TYPE values You have.

JSF 2.0 EL handeling nulls for resource keys

Just a quick question for you guys.
I have a resource key that is stored as a string in a managed bean and I'd like to get it to resolve to the value in a particular mapped Resource Bundle.
Here is what I started with:
<h:outputText value="#{msgs[bean.someVal]}"/>
I immediatly noticed that when someVal was null I would get the following exception:
javax.el.PropertyNotFoundException: /webpage.xhtml at line 118 and column 188 value="#{msgs[bean.someVal]}": Property '' not found on type java.util.PropertyResourceBundle
So I tried to set up a ternary like this:
<h:outputText value="#{bean.someVal == null ? '' : msgs[bean.someVal]}"/>
But I got the same error only quoting the new value.
I'm running JSF2.0 (Apache) on Tomcat6.
Anyone got any ideas? I'm pretty stumped on this one..
Let me know if you need more info, I hope this is enough to go on.. I'm thinking this is just something dumb that I'm doing ;)
Property '' not found
You've there an empty string. An empty string is not the same as null.
Use the empty check instead. It will check if the value is not null and if it is not an empty string.
<h:outputText value="#{empty bean.someVal ? '' : msgs[bean.someVal]}" />
An alternative, by the way, is to provide a custom ResourceBundle implementation on #{msgs} which doesn't throw the exception, but instead returns null or an empty String on handleGetObject() method.

Request parameter if struts2 if tag

I have another probably basic problem. happy if u can help.
there is a request parameter 'action'. if I write :
<label><s:property value="%{#parameters.action}"/></label>
the value appears (it is 1)
So itry to test now :
<s:if test="%{#parameters.action == '1'}">YES 1</s:if><s:else>NOT 1</s:else>
NOT 1 appears.
I have tries all the syntaxes I found on the net for the test. Nothing changes, NOT 1 still displays
Thank you
This is because:
the value of %{#parameters.action} is an array, not a single value, and
the value will be type-converted to a number (not sure why; need to look in to that)
The correct expression would be:
<s:if test="%{#parameters.action[0] == 1}">YES 1</s:if><s:else>NOT 1</s:else>
The correct expression would be:
<s:if test="#parameters.action[0] == 1">YES 1</s:if><s:else>NOT 1</s:else>
The request parameters is a map of [Strinf, String[]], So you have to access it like above

Resources