Keep attribute class consistent in TagLibs - grails

I am using a custom TagLib to format dates. From my view, I am passing a domain field into the TagLib like so:
<g:usDate value="${invoice.invoiceDate}" />
In this case the value goes into the TagLib with a type of Date. However, when I try to use the TagLib to format the contents of a field:
<g:textField name="invoiceDate" value="${g.usDate(value:"${invoice.invoiceDate}")}" />
the value goes into the TagLib as a type of GStringImpl. Is there a different syntax I can be using on the textField that passes the value as a Date so that I can use the same TagLib for both cases?

Remove the inner quotes and ${}:
<g:textField name="invoiceDate" value="${g.usDate(value:invoice.invoiceDate)}" />
This will pass the actual object invoice.invoiceDate to the taglib, rather than creating a GString containing the string representation of the object.
It's a little confusing but you need to remember that ${} means different things in different places. As a tag attribute it says "the content inside the braces is a Groovy expression, evaluate it and pass the resulting value directly to the tag". But once you're inside a Groovy expression it has the normal GString meaning of "evaluate this expression and insert its toString representation into the GString".

Related

SAPUI5 - how to set value to input parameter in binding dynamically

In my XSOData service I have an entity based on calculation view with input parameters. I can to set these parameters as constants in my XML view, i.e.
<List items="{dicts>/AncParams(p_dict_name='GROUPS',p_rec_id=2)/Results}" >
<StandardListItem
title="{dicts>NAME}"
/>
</List>
and it will work fine.
But how I can set parameters p_dict_name and p_rec_id dynamically? I tried to use expression bindings to get values for parameters from another model (something like this: <List items="{= ${dicts>/AncParams(p_dict_name='GROUPS',p_rec_id=${DictUIProps>/parentId})/Results} }" >) but with no luck. As I understand, expression bindings won't work. Is there any other way?
As far as I'm aware you can't do the aggregation binding dynamically through XML. At least not in the versions I have used and I have to admit I haven't re-checked in a while. The string never gets interpreted for inner bindings before it's applied to the model.
The way I do this is through the controller:
<List id="myList" />
and in your controller (onBeforeRendering or onPatternMatched or wherever your model and view are known to the controller):
this.getView().byId('myList').bindItems({
model: 'dicts',
path: `{/AncParams(p_dict_name='${p_dict_name}',p_rec_id=${p_rec_id})/Results}`,
template: new sap.m.StandardListItem({
title: '{dicts>NAME}'
})
});
you can use the getModel('dicts').createKey function to generate the path name which is a little cleaner I suppose.
This is the way to apply dynamic filters as well, In case you ever build those.

Parameter returned as a Groovy String instead of the Object

I have a custom taglib. I am going to pass the result of the taglib into the g:render taglib. One of the parameters is an Asset object. If I call the taglib by itself it works fine. The parameter is in fact an Asset Object.
Example:
templateFinder template="_displayMain" findByFactory="asset" findByObject="${params.asset}"
The correctly renders the params.asset as an Asset in the templateFinder taglib.
However once I add it to the g:render taglib it turns into the toString representation of the Asset object.
Example:
render template="${g.templateFinder(template:'_displayMain', findByFactory:'asset', findByObject:"${params.asset}")}" ..../>
This results in a Class Cast errorwith class 'org.codehaus.groovy.runtime.GStringImpl' to my Asset class.
I am confused as to why this is rendering as an Object in the first example but a Gstring in the second.
Thanks for any help.
You are converting the asset to a string by enclosing it in "${...}". Try this:
<g:render template="${g.templateFinder(template:'_displayMain',
findByFactory:'asset', findByObject: params.asset)}" ..../>

Misbehaving Asp.net MVC helper methods (ie: Html.TextBox() and Html.Hidden())

I've been trying to debug an issue and I pinned pointed it down to this scenario:
When the statement Html.TextBox("ID", "What the heck..") is executed, I expect it to render:
<input id="ID" name="ID" type="text" value="What the heck.." />
But I get a Guid as its TextBox value such as so:
<input id="ID" name="ID" type="text" value="2e369d2c-071d-4733-8382-cc9e77d0b912" />
Why is Asp.net MVC outputting Guids? I'm not overriding asp.net mvc's framework methods. Please refer to the screenshot.
Update:
Here's another screenshot using Html.Hidden() instead of Html.TextBox(). I couldn't use Html.HiddenFor() directly into the Watch window because HiddenFor() uses lambdas.
#Eric Petroelje and #TLS: You two are correct. TextBox() and Hidden() is retrieving ID's value from the POST variables and not from the current Model or the function's value parameter. Though, I've expected different from HiddenFor() and TextBoxFor(). I expected it to get its value from the POST'ed variables only if it cant get it from the current Model. How can I achieve this?
Maybe you have a POST variable named ID that is a GUID? If that's the case, the HTML Helper method will use that POST value. If no POST value is present, it will fall back to the value in the model.
You are correct that Html.TextBox("ID", "What the heck..") is expected to output the attribute values that you give in your first example; however, if you are using the Html.TextboxFor method, then you're using a MVC Helper that dynamically loads the value of the ID property and places that into the value attribute in the HTML. When you use the Html.TextboxFor method, your second example is the expected HTML output if your ID property is a Guid.

Grails hasErrors method with ternary operator?

I'm developing application using Grails framework and I'm having problems with hasErrors when invoked as a method from a gsp view.
I have a form that get's populated by values from a database (default values). Those values are stored in a session object. Users can edit values in form fields and send results back to the database. Before data gets persisted I have a command object that validates data. If there are errors command objects renders view with the same form and errors highlighted.
What I'm trying to do is to have form fields populated by values stored in a session object unless there are errors from command object. In that case field(s) should be populated by the wrong values entered by the user.
Here's the code snippet:
<g:textField name="somename" id="someid" value="${hasErrors(bean: commandobject, field: 'somename') ? fieldValue(bean: commandobject, field: 'somename') : session.somevalue}" />
Problem with above code is that no matter the value entered in the field, whether be right or wrong, field always ends up with the value from the session object. Is there a solution to this or am I doing something wrong in the first place?
When you call hasErrors like that you're invoking the <g:hasErrors/> tag which doesn't return a boolean value - it looks at the condition and conditionally invokes the tag body. Under that description, it makes sense why it's behaving the way it is.
What I'd recommend is to create your own TagLib and use commandobject.errors.hasFieldErrors('somename') [docs] in your condition (to get the boolean value you're looking for).
The hasErrors as a method call in GSP works a bit differently than as a tag <g:hasErrors>. The former is intended to set CSS class in divs or spans etc...
e.g. <div class="prop ${hasErrors(bean:parent, field:'child.name', 'errors')}">
where errors is the CSS class name. So if you don't specify the output string, it seems to return false by default, so to workaround your case, return '1'. So your code should look like:
<g:textField name="somename" id="someid" value="${hasErrors(bean: commandobject, field: 'somename', '1') ? fieldValue(bean: commandobject, field: 'somename') : session.somevalue}" />
This will also work with logical operations in <g:if>

how to set a value to <s:hidden > from the action class without using list and iterator in struts 2?

In action class i am reading value from the database.
Let's say "abc".Now the "abc" value should be populated to jsp page.i.e "abc" value should set to s:hidden field in the jsp page.
Since it is single value , i don't want to use List in the action class.
Is there any other way to do that ?
Why would you want to use a list? Just provide the appropriate getter like:
public Object getAbc(){
return abc;
}
and in your page access it with simple OGNL expression like:
<s:hidden name="filedYouWantToSetThisValueTo" value="%{abc}"/>
Hope I got it right.

Resources