I'm adding a custom error
trip.errors.rejectValue('driver','Driver already on a trip at this time.')
When I output the error in a view....
<g:hasErrors bean="${trip}" field="driver">
<g:renderErrors bean="${trip}" field="driver" as="list" />
</g:hasErrors>
....a fully qualified field path gets appended to it.
"Driver already on a trip at this time. org.heidelberg.Trip.driver"
How can I get rid of the org.heidleberg.Trip.driver part that is applied?
Your method calling is incorrect.
Try this -
trip.errors.rejectValue('driver',null,'Driver already on a trip at this time.')
it should work fine.
The correct method signature of rejectValue method is
void rejectValue(String field, String errorCode, String defaultMessage
Cheers!!
Related
I am attempting to use a converter to modify a string in a jsrender template, but I can't seem to get the converter to work on a tag.
Using the example on JsRender API documentation Using converters for example, I have:
<script>
$.views.converters("upper", function(val) {
return val.toUpperCase();
});
</script>
Then in my HTML I have {{upper:Name}} which throws an error in the console: TypeError: val is undefined, and the template does not render at all.
However, if I apply the converter directly to a string like {{upper:"This should be uppercase"}} it outputs the string in uppercase as expected.
The {{:Name}} tag works fine by itself, so why isn't the converter working with it?
In case it is relevant, this is an ASP.NET-MVC project and the JSON data rendered by the template is coming from a $.post('#Url.Action(..,..)')... response. It's working perfectly until I attempt to apply the converter to the tag. Are converters not usable in this scenario?
It looks like your Name property is undefined in some case.
If you have a Name that is undefined, then {{:Name}} will render as the empty string, "" - but {{upper:Name}} will throw an error since undefined.toUpperCase() will fail.
You can investigate by breaking on thrown errors, (or by putting a break point or debugger statement in the converter) and seeing where your undefined Name is coming from.
You can also prevent the error getting thrown - and instead get error information rendered out - by any of the following techniques
write {{upper:Name onerror=true}} or {{upper:Name onerror='bad'}} (see https://www.jsviews.com/#onerror)
write
$.views.converters("upper", function(val) {
return val === undefined ? 'undefined' : val.toUpperCase();
});
write $.views.settings.debugMode(true);
and see what output you get, to investigate further about where your undefined Name is occuring.
I've stored a string in the database. When I save and retrieve the string and the result I'm getting is as following:
This is my new object
Testing multiple lines
-- Test 1
-- Test 2
-- Test 3
That is what I get from a println command when I call the save and index methods.
But when I show it on screen. It's being shown like:
This is my object Testing multiple lines -- Test 1 -- Test 2 -- Test 3
Already tried to show it like the following:
${adviceInstance.advice?.encodeAsHTML()}
But still the same thing.
Do I need to replace \n to or something like that? Is there any easier way to show it properly?
Common problems have a variety of solutions
1> could be you that you replace \n with <br>
so either in your controller/service or if you like in gsp:
${adviceInstance.advice?.replace('\n','<br>')}
2> display the content in a read-only textarea
<g:textArea name="something" readonly="true">
${adviceInstance.advice}
</g:textArea>
3> Use the <pre> tag
<pre>
${adviceInstance.advice}
</pre>
4> Use css white-space http://www.w3schools.com/cssref/pr_text_white-space.asp:
<div class="space">
</div>
//css code:
.space {
white-space:pre
}
Also make a note if you have a strict configuration for the storage of such fields that when you submit it via a form, there are additional elements I didn't delve into what it actually was, it may have actually be the return carriages or \r, anyhow explained in comments below. About the good rule to set a setter that trims the element each time it is received. i.e.:
Class Advice {
String advice
static constraints = {
advice(nullable:false, minSize:1, maxSize:255)
}
/*
* In this scenario with a a maxSize value, ensure you
* set your own setter to trim any hidden \r
* that may be posted back as part of the form request
* by end user. Trust me I got to know the hard way.
*/
void setAdvice(String adv) {
advice=adv.trim()
}
}
${raw(adviceInstance.advice?.encodeAsHTML().replace("\n", "<br>"))}
This is how i solve the problem.
Firstly make sure the string contains \n to denote line break.
For example :
String test = "This is first line. \n This is second line";
Then in gsp page use:
${raw(test?.replace("\n", "<br>"))}
The output will be as:
This is first line.
This is second line.
I'm trying to create a link with a dynamic link like:
<g:link action="${nextDashboardUriMap.nextAction}" params="${["$nextDashboardUriMap.queryStringId": "$entityId" ]}">
${entityName}
</g:link>
where nextDashboardUriMap.queryStringId contains xyz and entityId contains 12.
I was expecting url of the link to be http://website.com/controller/action?xyz=18 but <g:link/> consistently gives me http://website.com/controller/action?xyz.
I have tried replacing entityId with a string literal.
You don't need to use GStrings here, you can simply say
<g:link action="${nextDashboardUriMap.nextAction}"
params="[(nextDashboardUriMap.queryStringId):entityId]">
It seems that it may be this bug: GRAILS-9774 - the value is lost if key in params map is of type GString. Converting the key to String should resolve your problem:
<g:link action="${nextDashboardUriMap.nextAction}"
params="${[("$nextDashboardUriMap.queryStringId".toString()): "$entityId" ]}">
(...)
how about
<%
Map paramsMap = [:]
paramsMap[nextDashboardUriMap.queryStringId] = entityId
%>
<g:link action="${nextDashboardUriMap.nextAction}" params="${paramsMap}" >${entityName}
</g:link>
Hitting my wall here...
I've got the following data where a Primary Employee may have multiple dependents. I need to create a function that will match the Employee's SSN (ab:SSN) against the Dependent_SSN and determine if one of them is a 'Spouse'. If so, then we'll return the Dependent_SSN of the 'Spouse'.
If not, we'll move on and return the next non-'Spouse' Dependent_SSN.
I'm trying to create a function as I think I'll need this more than once. The code snippet resides inside of an existing template that is doing other looping functionality.
I've tried this but Oxygen returns an error:
<xsl:function name="ab:PQB">
<xsl:param name="EE_SSN">
</xsl:param>
<xsl:for-each select="/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]ab:dependents/ab:Dependent_SSN">
</xsl:for-each>
The Error returned is :
"Engine name: Saxon-PE 9.3.0.5
Severity: fatal
Description: Unexpected token name "wd:dependents" beyond end of expression"
I know I need to test the higher level SSN against looping through the dependents, but like I said "I'm against my wall" :)
Data is here:
<ab:Report_Entry>
<ab:SSN>888881006</ab:SSN>
<ab:Last_Name>Smith</ab:Last_Name>
<ab:First_Name>Kimberly</ab:First_Name>
<ab:dependents>
<ab:Dependent_SSN>888881009</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Spouse">
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881004</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Child">
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881003</ab:Dependent_SSN>
<ab:Relation ab:Descriptor="Child">
<ab:ID ab:type="Related_Person_Relationship_ID">Child</ab:ID>
</ab:Relation>
</ab:dependents>
<ab:dependents>
<ab:Dependent_SSN>888881001</ab:Dependent_SSN>
<ab:Dependent_ID>1032D-4</ab:Dependent_ID>
<ab:Relation ab:Descriptor="Child">
<ab:ID ab:type="Related_Person_Relationship_ID">Child</ab:ID>
</ab:Relation>
</ab:dependents>
</ab:Report_Entry>
Thank you to any advice!
You might want to define the type of the input parameter and the type of the function result and then you should write a function body returning a value of that type. Currently your description sounds rather procedural, that is not going to work with XSLT/XPath.
As for the error, I think in the path /ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]ab:dependents/ab:Dependent_SSN you need one more slash /ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:Report_Data/ab:Report_Entry[ab:Employee_ID=$EE_SSN]/ab:dependents/ab:Dependent_SSN to have a syntactically correct path. That should avoid the syntax error you get but is not likely to return the result you want.
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.