Grails - escape double quotes in params - grails

I have an array of strings that looks like this when I print it on the webpage with ${projectList}:
[foo,bar,foo "bar"]
And I have a form with select:
<g:select class="form-control sbt-color" name='project' value ='${params.project.encodeAsHTML()}' from='${projectList}'/>
If I choose 'foo "bar"' params.project will only get 'foo '. Is there a way of fixing this without changing source data?
Update:
The only workaround I've found so far is to use:
<g:select class="form-control sbt-color" name='project' value ='${params.project}' from='${projectList*.replaceAll("\"","""}'/>
This gives me params.project as I want, but in select list itself double quotes are replaced by " and it looks bad.

After removing the .encodeAsHTML() this will work. Try this
<g:select class="form-control sbt-color" name='project' value ='${params.project}' from='${projectList}'/>

I found an appropriate solution, but still I had to movify source.
I use map instead of an array:
def projectMap = [:]
for (p in projectList){
projectMap[p.replaceAll("\"",""")] = p
}
and use it in select:
<g:select class="form-control sbt-color" name="project" value ="${params.project.encodeAsHTML()}" from="${projectMap.entrySet()}" optionKey="value" optionValue="key"/>
Btw, I need .encodeAsHTML() here to correct display of last chosen option

Related

Evaluate Groovy/Grails Code in variable when printing it in GSP

I have a GSP page containing different input elements related to a specific context.
For example I can display a textfield for usecase A, but not for usecase B (short version, it's very complex actually)
For this I have a domain object, which is normally populated with plain static HTML. But now I need to add dynamic data, like this:
def field = new InputField()
field.code = '<input type="text" name="foo" value="${currentUser.name}" />'
// or: field.code = '<option value="1"><g:message code="someCode"/></option>'
This code is stored in database. It will be rendered later on in GSP:
<g:each in="${InputField.findAllBySomeCondition(...)}">
${it.code}
</g:each>
This will print the input element, but instead of evaluating the dynamic code (${currentUser.name}) it is just printed as plain text.
Unfortunately I can't change the whole process, there are over 3000 different input elements stored already, but none of them are dynamic.
Is there a way to tell Grails to evaluate code within the variable before printing it?
edit: I'm using Grails 2.2.4
That's because you're using ' for the string, not ". When using ' the result is a String, not a GString with variable evaluation.
Try changing to:
field.code = "<input type=\"text\" name=\"foo\" value=\"${currentUser.name}\" />"
or use the triple version """ to avoid escaping:
field.code = """<input type="text" name="foo" value="${currentUser.name}" />"""

Grails : Use value from session in drop down list option

In my controller:
def billingDetails() {
def traineeDetails = session.traineeDetais
println "session data::"+traineeDetails
[traineeNames:traineeDetails.name]
}
This prints: [numberOfTrainees:2, submit_trainee_details:Next: Billing Details �, phone:[999999, 99999], email:[tester1#test.com, tester2#test.com], name:[Jack, Rob], jobTitle:[SE, SE], action:processTraineeDetails, controller:trainingOrder]
now in my GSP i want to have a select tag which will have name (jack, Rob) as options
<g:select name="traineeName"
from="${traineeNames}"
value=""
/>
which is not working fine.. How to make this to work so that i will get the names as options in dropdown list
make use of the optionKey optionValue fields in your g:select
reference:
http://grails.org/doc/2.2.1/ref/Tags/select.html
and do something like
<g:select optionKey="value" optionValue="value"
name="traineeName" from="${traineeNames}" />
I don't know the exact problem but one possible reason is that your session.traineeDetais.name is not a list. Check weather session.traineeDetais.name is a list or not, like
println "Check: ${session.traineeDetais.name instanceof List}"
if it is list then your code should work and if it is string then your drop down contain string characters as values.

Razor syntax isn't recognizing '#' after an underscore

I am trying to do the following:
#foreach(var p in #Model.line_products){
<img class="small_img_#Model.line_products[i].short_name" src="/Content/images/#Model.line_products[i].image_name" />
}
Which isn't working, it renders the text just the way it is, not recognizing the '#'. I found this other post in Stackoverflow, which suggests adding parenthesis in the following way:
#foreach(var p in #Model.line_products){
<img class="small_img_(#Model.line_products[i].short_name)" src="/Content/images/#Model.line_products[i].image_name" />
}
Using this workaround, I get that my id is rendered as small_img_(MODEL ATTRIBUTE). Isn't there a workaround which doesn't require adding specific characters? (such as the parenthesis).
You have more errors than a simple undercore problem here. You cannot use #Model inside your if. You are already in a # block. Simply use #foreach(var p in Model.line_products).
Plus, the way you wrote the parenthesis, they will get rendered. What you want is
small_img_#(Model.line_products[i].short_name)
Put the parenthesis after the # instead of before:
class="small_img_#(Model.line_products[i].short_name)"
I sometimes put a couple of Guids in the id of an element and an underscore separator doesn't work.
There are two ways around this. First use the entity code _ instead and secondly just use a hyphen.
<input id="chk_#classLeader.ClassLeader_#ing.Ingredient.Guid" type="checkbox" class="chk_Done form-check">
<input id="chk-#classLeader.ClassLeader-#ing.Ingredient.Guid" type="checkbox" class="chk_Done form-check">
This is because I want to grab out the Guid's when the check box is clicked with some JQuery like this:
$(".chk_Done").click(function () {
var obj =[];
const itemId = ($(this).attr("id"));
const myArray = itemId.split("_");
var ClassLeaderGuid = myArray[1], IngredientGuid = myArray[2];

grails g:select i18n

I just read a lot and googled but I'm frustrated right now.
I have a Country domain model
class Country{
String countryCode //maybe EN, DE, CH...
}
Now I want a translation inside the . I read in the documentation (and with google) that it is possible with the "id" to select it from the translation message property files. Something like:
country.code.1=America
country.code.2=England
country.code.3=Germany
But this is not what I want. I want to have something like:
country.code.US=America
country.code.EN=England
country.code.DE=Germany
So, I found a possible solution from stackoverflow: translate a HTML select element in Grails
that would mean for me I have to put it like this:
<g:select name="country"
from="${allCountries}"
value="${country}"
optionKey="id"
optionValue="${ {countryCode->g.message(code:'country.code.'+countryCode)} }"/>
But my result is inside the dropdown: "country.code.grails.Country : 1" (and so on for each country)
If I change the last line of the gsp-g:select implementation to:
[...]optionValue="${ {countryCode->g.message(code:'country.code.US')}
as you see hardcoded! And THIS works :-D
Hope you got me and can help me, thank you very much!
There are 3 possibilities:
Instead of sending the id to the controller, use the contryCode instead of id:
<g:select name="contryByCountryCode"
from="${countryCodes}"
valueMessagePrefix="com.yourcompany"/>
Will produce:
<select name="contryByCountryCode" id="contryByCountryCode" >
<option value="US">United States<option>
...
</select>
If you have proper messages configured. In the backend you need to do something like:
def country = Country.findByCountryCode(params.contryByCountryCode)
Do it manually:
<select name="contryByCountryCode" id="contryByCountryCode" >
<g:each in="${countryCodes}" var="country">
<option value="${country.id}">
${message(code:"prefix" + country.countryCode)}
<option>
</g:each>
</select>
Patch g:select to work in case optionValue and messagePrefix is defined ;-)

How can I change the way GRAILS GSP fieldValue formats Integers?

I have a field in my domain object which I define as an Integer...
Integer minPrice
I then access it in a GSP page as follows:
${fieldValue(bean: myBean, field: 'minPrice')}
and what I get in my HTML is...
100,000
which is not an Integer, it's a String. Worse still it's a formatted String in a particular locale.
This is a problem because I have a SELECT control on an HTML FORM which has a (non-ordinal) range of values for minPrice which I want to store in my domain object as integers, and I don't want to store an index to some array of values that I have to repeatedly map back and forth between, I want the value itself.
My select control looks like this...
<g:select name="minPrice"
value="${fieldValue(bean: personInstance, field: 'minPrice')}"
onchange="setDirty()"
noSelection='${['0':'Select a number...']}'
from="${[
['name':'100,000', 'id':100000],
['name':'200,000', 'id':200000],
['name':'300,000', 'id':300000]
]}"
optionKey="id" optionValue="name"
/>
When I get the value from the SELECT field to post back to the server it correctly has an Integer value, which I persist. However the return trip never pre-selects the right row in the drop-down because the value is this comma separated String.
This works fine elsewhere in my code for small numbers where the comma formatting doesn't come into play, and the round-trip in and out of the SELECT is successful. But values >999 don't work.
The docs say "This tag will inspect a bean which has been the subject of data binding and obtain the value of the field either from the originally submitted value contained within the bean's errors object populating during data binding or from the value of a bean's property. Once the value is obtained it will be automatically HTML encoded."
It's that last bit that I want to avoid as it appears to format Integers. So, what little bit of Grails/GSP magic do I need to know so I can get my Integer to be rendered as an integer into my SELECT and pre-select the right row?
EDIT:
I have tried some further things based on the answers below, with pretty disappointing results so far...
If I put the <gformatNumber/> tag in my <g:select/> I get the page code as text in the browser.
<g:select name="minPrice"
value='<g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}" format="#" />'
onchange="setDirty()"
noSelection='${['0':'Select a number...']}'
from="${[
['name':'100,000', 'id':100000],
['name':'200,000', 'id':200000],
['name':'300,000', 'id':300000],
]}"
optionKey="id" optionValue="name"
/>
Using the number format tag from GSP on my Integer value of 100000 like this...
var x = <g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}" format="#" />;
gives 100. Remember that the fieldValue gives back 100,000, so this is not a surprise.
If I use the jsp taglib like this...
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
var y = <fmt:formatNumber value="${fieldValue(bean: personInstance, field: 'minPrice')}" pattern=".00"/>;
I get an error from the page compiler Cannot format given Object as a Number.
I guess I have a wider concern than I can't seem to get an Integer value as a genuine integer into my code if it is greater than 999 because of the default (and unconfigurable) behaviour of the fieldValue directive. However my specific problem of not being able to pre-select an Integer value in a SELECT control is not going away. At the moment I'm at a bit of a loss.
Anyone have any further ideas?
Do you want to show the raw number? like 100000?
You can get the field directly:
${myBean.minPrice}
I think you have at least two possible solutions.
One is to use the JSTL taglib as described in the docs.
Another, cooler way is to use the 'formatNumber' tag included with grails - also in the docs.
For your purpose, the use of that tag might look like this:
<g:formatNumber number="${fieldValue(bean: myBean, field: 'minPrice')}" format="######" />
Use the 'groupingUsed' attribute in combination with your format:
<g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}"
format="#"
groupingUsed="true" />
Better use custom PropertyEditor in order not to bother with formatNumber tag every time you output a value.
Like, declare a bean in resources.groovy:
myOwnCustomEditorRegistrar(CustomEditorRegistrar)
And create your class:
class CustomEditorRegistrar implements PropertyEditorRegistrar {
void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(BigDecimal.class, new MyBigDecimalEditor(BigDecimal.class))
}
}
Change
var x = <g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}" format="#" />;
to
var x = <g:formatNumber number="${personInstance.minPrice}" format="#" />;
I found the best way to handle this was doing what Victor Sergienko (upped btw) hinted at with using a PropertyEditor.
Create an editor for Integer, put in src/groovy:
class IntegerEditor extends PropertyEditorSupport {
void setAsText(String s) {
if (s) value = s as Integer
}
public String getAsText() {
value
}
}
and register it using a PropertyEditorRegistrar (also in src/groovy):
class MyEditorRegistrar implements PropertyEditorRegistrar {
public void registerCustomEditors(PropertyEditorRegistry reg) {
reg.registerCustomEditor(Integer, new IntegerEditor())
}
}
add your registrar into the spring config (grails-app/conf/spring/resource.groovy):
beans = {
customEditorRegistrar(MyEditorRegistrar)
}
From now on any Integers that are bound, receive errors (or not) and then redisplayed with the fieldValue tag should be displayed by Integer's default toString - you can customise this behaviour in the editor by amending the getAsText implementation.
Personally I would create a wrapper for this kind of thing so you can set up an editor just for that type rather than across the board for a frequently used type. Though I realise this would mean a little bit of mapping when persisting to the DB...
I have a solution/work-round... The answer seems to be, "do nothing".
Instead of trying to parse the stringified number back into an integer, I left it as a formatted string for the purposes of the select. This meant I had to change my from values as follows:
<g:select name="minPrice"
value="${fieldValue(bean: personInstance, field: 'minPrice')}"
onchange="setDirty()"
noSelection='${['0':'Select a number...']}'
from="${[
['name':'100,000', 'id':'100,000'],
['name':'200,000', 'id':'200,000'],
['name':'300,000', 'id':'300,000']
]}"
optionKey="id" optionValue="name"
/>
Of course when I post back to the server the value that gets sent is "100,000" as an escaped String. What I realised was that Grails, or Spring, or Hibernate, or something in the stack, would do the coersion of the String back into the right Integer type prior to persistence.
This works just fine for my purposes, however I think it is basically a work-round rather than a solution because of locale issues. If my thousand separator is a "." and my decimal separator is ",", which it is for much of Europe, then my code won't work.
Use like this :
<g:formatNumber number="${fieldValue(bean: personInstance, field: 'minPrice')}"
format="#.##"/>;

Resources