thymeleaf - passing a Selection Variable Expression *{foo} to an Expression Utility Object - spring

I have a spring backed form with a model object Foo that contains Bar
<form action="#" th:action="#{/some/action}" th:object="${foo}" method="post">
now, I would like to do something like this:
<input type="text" id="bar.createdAt" name="bar.createdAt" th:value="*{bar.createdAt} ? ${#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}"/>
but the following parsing exeption is thrown:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))" (registration:93)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
root cause
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))" (registration:93)
org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:161)
org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154)
org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:59)
root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 7): Property or field 'createdAt' cannot be found on null
org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:211)
org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:85)
org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:43)
org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:341)
I've already tried:
...#dates.format(*{bar.createdAt}..., ...#dates.format(__*{bar.createdAt}__...but none has worked out.
the same without the form (directly accessing view model object) works fine.
Any idea what would be the right construct?
Thanks in advance!!!

Instead of :
${#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}
Change it to :
*{#dates.format(bar.createdAt, #messages.msg('app.dateformat.datetime'))}
So that the utility object will work on the bar property of the last selected object which is the form backing object, ${foo}, in your case.

I think the clue is in the line:
Property or field 'createdAt' cannot be found on null
Are you sure that bar is not null?

Related

Display an error message notification using Toastr and the Exception class render() method in Laravel

Trying to have a dynamic way to catch thrown exceptions and display the custom error message to the user.(Custom meaning each exception has its own message relating to why the exception was thrown.) Here is what I have been toying with so far but have not had much lack.
In a controller/service throw the exception:
throw AppException::withMessages(['message' => 'Quantity should be less than or equal to Stock Available']);
under App/Exceptions I have the AppException class with the render method:
public function render(){
return response()->view('toastr', ['message'=>$this->message], 500);
}
Under Views I have the toastr view:
<script>
toastr.error("{{$message}}")
</script>
I have included the toastr view in the main layout below all the scripts and style sheets: (I understand there should probably be an if statement to check if the $message is set, so it only pops up when there is data in message.)
#include('toastr')
What I am missing about how the response in the render method of the exception works or how would you use this structure to do a dynamic exception handling pop up displaying Laravel workflow?

Why does primefaces return message for ID that doesnt exist

I'm writing a JSF application with primefaces which uses bean validation.
I have an input validation message being returned on form submission:
<partial-response><changes><update id="pageForm:j_idt88"><![CDATA[<div id="pageForm:j_idt88" class="ui-messages ui-widget form__error" aria-live="polite"><div class="ui-messages-error ui-corner-all"><span class="ui-messages-error-icon"></span><ul><li role="alert" aria-atomic="true"><span class="ui-messages-error-summary">must be true</span></li></ul></div></div>]]></update><update id="j_id1:javax.faces.ViewState:0"><![CDATA[2771262580625457800:-3174803273303516341]]></update><extension ln="primefaces" type="args">{"validationFailed":true}</extension></changes></partial-response>
Yet no element exists within my page with an ID of pageForm:j_idt88.
I noted the IDs are sequential and the highest one in the page only reaches j_idt80
I've put in <ui:debug> to my page and inspected the component tree which corresponds to the HTML. (No element with ID of pageform:j_idt88)
The element I believe it should be updating is nested within the (only) h:form. Output as :
<div id="pageForm:confirm:j_idt75" aria-live="polite" class="form__error ui-message"></div>
Where do I start looking to debug why this is happening?

Error trying to get attribute from element in Cypress

I have this HTML element:
<input id="" type="text" name="last_name" value="Userc7bff2d0-7faf-11e8-9884-8fe4c5df7f77-Updated" class="medium" maxlength="2000" autocomplete="off" tabindex="" data-reactid=".0.2.0.1.0.2.1.0.1.0.0.1:0.1.0.1.2:$/=10">
I want to get it's value property to assert that it has been updated by my test.
I have tried using its():
cy
.get(selector)
.its("value")
.should("contain", "-Updated");
But get the error:
CypressError: Timed out retrying: cy.its() errored because the property: 'value' does not exist on your subject.
I have also tried invoke:
cy
.get(selector)
.invoke("value")
.should("contain", "-Updated");
But get a similar error:
CypressError: Timed out retrying: cy.invoke() errored because the property: 'value' does not exist on your subject.
In both cases, the Cypress console output of the get() command shows the element with its value property successfully:
Yielded: input id="" type="text" name="first_name" value="Fake-Updated"
class="medium" maxlength="2000" autocomplete="off" tabindex="" data-
reactid=".0.2.0.1.0.2.1.0.1.0.0.1:0.1.0.0.2:$/=10"
I'm kind of stumped on this one. Please let me know if you want more info or have an idea what's going on.
invoke() calls a jquery function on the element. To get the value of an input, use the function val():
cy.get('input').invoke('val').should('contain', 'mytext')
This is not the same as getting the value attribute which will not update with user input, it only presets the value when the element renders. To get an attribute, you can use the jquery function attr():
cy.get('input').invoke('attr', 'placeholder').should('contain', 'username')
you can use this
cy.get('a') // yields the element
.should('have.attr', 'href') // yields the "href" attribute
.and('equal', '/home') // checks the "href" value
or
cy.get('a').should('have.attr', 'href', '/home')
for more details check this: https://docs.cypress.io/api/commands/should#Method-and-Value
If above answers doesn't work try this,
cy.get('input[placeholder*="Name"]')
Find the input with a placeholder attribute containing the word "Name".
Now there is a plugin for your need.
https://github.com/Lakitna/cypress-commands/blob/develop/docs/attribute.md
With this, you'll be able to do :
cy.get('input').attribute('placeholder').should('contain', 'username');
Aprt from above suggestions, you can also get the value using prop()
The benefit of using prop() over attr() is that:
The prop() will always give you the current value but the attr can sometimes give you the default value no matter how many times you updated it.
cy
.get(selector)
.invoke("prop","value")
.should("contain", "-Updated");

Syntax Issue- Razor MVC4

I'm trying to display a partial view using a custom helper method. The only problem is, I can't get past this syntax issue. The model set for this cshtml file is an IEnumerable collection of models I've defined as Operation. I'm sure this is an easy fix. Here's an example of what I'm seeing:
I have this block of code:
#using(Html.BeginForm()) {
<div id="editor_rows">
#foreach (var item in Model){
Html.RenderPartial("OperationEditorRow", item);
}
</div>
}
This gives me the following error at runtime:
Unexpected "{" after "#" character. Once inside the body of a code block (#if {}, #{}, etc.) you do not need to use "#{" to switch to code.
But if I remove the # sign in front of the foreach statement, everything is interpreted as plain text. So I tried placing an # in front of Html as follows:
#using(Html.BeginForm()) {
<div id="editor_rows">
#foreach (var item in Model){
#Html.RenderPartial("OperationEditorRow", item);
}
</div>
}
This comes back with a compilation error that says:
Cannot implicitly convert type void to object
If I run the project from here, I get a runtime error that says:
The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
I'm assuming this is related to the previous error. If anybody has a suggestion for me, please help.
Problem solved. I worked on this with a coworker. It turns out the error refferring to the write method pointed to a problem inside my partial view. I was using #{} around a block of code inside of there, which was most likely throwing the other syntax errors also. Thanks for the responses.
Add {}'s around your render call like #{RenderPartial...}

Spring MVC : showing a link in the error message

I have the following snippet of code in the Validator which basically is used to show an error message. In the error message I would like to show a link by sending it as a parameter in the error message.
if (user != null && formData.getUserId()== null) {
errors.rejectValue("email", "email.already.exists",new Object[]{"Link "},null);
return;
}
I am unable to get the desired error message on the JSP page . The error is rendered like a string
and is not converted to HTML code. Please help.
Thank you
Manu
<form:errors> has an escapeHtml attribute:
<form:errors escapeHtml = "false" ... />
Note, however, that this may cause undesired behaviour if other error messages displayed by this tag should be escaped.
The attribute is now htmlEscape. See this for reference.
So, if you use <form:errors htmlEscape="false" ... /> then you can have html in the error.
The error variable should only be a string. In your JSP, enclose that string with whatever HTML markup you would like.

Resources