Grails hasErrors 方法与三元运算符?

发布于 2024-12-12 18:11:11 字数 548 浏览 0 评论 0原文

我正在使用 Grails 框架开发应用程序,当从 gsp 视图作为方法调用时,我遇到了 hasErrors 问题。

我有一个由数据库中的值(默认值)填充的表单。这些值存储在会话对象中。用户可以编辑表单字段中的值并将结果发送回数据库。在数据持久化之前,我有一个验证数据的命令对象。如果存在错误,命令对象将以相同的形式呈现视图并突出显示错误。

我想做的是让表单字段由存储在会话对象中的值填充,除非命令对象出现错误。在这种情况下,字段应该由用户输入的错误值填充。

下面是代码片段:

<g:textField name="somename" id="someid" value="${hasErrors(bean: commandobject, field: 'somename') ? fieldValue(bean: commandobject, field: 'somename') : session.somevalue}" />

上面代码的问题是,无论字段中输入的值是正确还是错误,字段始终以会话对象中的值结束。有解决办法吗?还是我一开始就做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-12-19 18:11:11

当您像这样调用 hasErrors 时,您正在调用 < code>标签不返回布尔值 - 它查看条件并有条件地调用标签主体。根据这个描述,它的行为方式是有道理的。

我建议创建您自己的 TagLib 并使用 commandobject.errors.hasFieldErrors('somename') [docs] 在您的条件中(以获取您正在查找的布尔值 为了)。

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).

靑春怀旧 2024-12-19 18:11:11

hasErrors 作为 GSP 中的方法调用与作为标记 的工作方式略有不同。前者旨在在 div 或 span 等中设置 CSS 类...

e.g. <div class="prop ${hasErrors(bean:parent, field:'child.name', 'errors')}">

其中 errors 是 CSS 类名称。因此,如果您不指定输出字符串,默认情况下似乎会返回 false,因此要解决您的情况,请返回“1”。因此,您的代码应如下所示:

<g:textField name="somename" id="someid" value="${hasErrors(bean: commandobject, field: 'somename', '1') ? fieldValue(bean: commandobject, field: 'somename') : session.somevalue}" />

这也适用于 中的逻辑运算

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文