Groovy 中的变量

发布于 2024-08-08 14:33:34 字数 653 浏览 4 评论 0原文

我在一个使用 Grails 的项目中,

我使用 beanFields 插件,其中我将 bean:inputTemplate 更改为以下内容

   <bean:inputTemplate>
    <div class="prop ${hasErrors(bean:$beanName,field:'$fieldId','errors')}">${label}
      <span  class="value">${field}
      </span>
    </div>
  </bean:inputTemplate>

如您所知,我尝试使用 $beanName 作为 BeanName ..那是因为 beanFields 传递 beanName 和 fieldId以及 inputTemplate 标记的更多其他属性。

但是,问题是我不能这样做。而且我真的很懒,不想花所有时间复制和粘贴相同的字段 div 并维护一个巨大的因此

,如果有人能在这种情况下提供帮助,我将非常感激。

我想在 $ { } 代码块中引用一个变量,就像在 PHP 中一样, $$variable 使用 $variable 的值作为要计算的变量的名称。

希望我说得足够清楚..并感谢您的帮助。

I'm in a project using Grails,

I user beanFields plugin where I'm changing the bean:inputTemplate into the following

   <bean:inputTemplate>
    <div class="prop ${hasErrors(bean:$beanName,field:'$fieldId','errors')}">${label}
      <span  class="value">${field}
      </span>
    </div>
  </bean:inputTemplate>

As you can, I'm trying to use $beanName as the BeanName .. that is because beanFields passes beanName and fieldId and some more other properties to the inputTemplate tag..

But, the problem is that I can't do that.. And I'm really lazy and dont want to spend all the time copy and pasting the same field div and maintaining a huge file for that...

So, I will be really greatful if any could help in that situation.

I want to reference a variable inside the $ { } block of code, as in PHP there is $$variable that uses the value of the $variable as a name of a variable to evaluate.

Hope I was clear enought.. and thank you for helping.

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

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

发布评论

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

评论(3

柠栀 2024-08-15 14:33:35

不是直接回答你的问题,但你见过 bean-fields 插件吗?

http://grails.org/plugin/bean-fields

我认为它做了你的事情正在尝试做,以及更多

Not a direct answer to your question, but have you seen the bean-fields plugin?

http://grails.org/plugin/bean-fields

I think it does what you're trying to do, and more

素年丶 2024-08-15 14:33:35

beanName 前面不需要 $,它应该在范围内。

<div class="prop ${hasErrors(bean:beanName,field:'username','errors')}" >

另外,我认为 beanFields 已经通过错误变量提供了错误消息。

因此,您可以测试错误是否不为空,而不是调用 hasErrors。

You shouldn't need a $ in front of beanName, it should be in scope.

<div class="prop ${hasErrors(bean:beanName,field:'username','errors')}" >

Also, I think beanFields already provides the error messages via the errors variable.

So you could test to see if errors is not null instead of calling hasErrors.

﹂绝世的画 2024-08-15 14:33:35

在调查这个问题之后..我发现是的 beanName 被传递到模板,我不需要在 beanName 前面使用 $...

但是,当我使用 hasErrors(beans:beanName,field:'username' ,'错误')它不起作用。

但是,我可以做到这一点

<bean:inputTemplate>
    <div class="prop">${label}
      <span  class="value">${field}
      </span>
      <g:if test="${errors}"><div class="errors"> ${errors} </div></g:if>
    </div>
  </bean:inputTemplate>

即使它不起作用,它取决于域类上的验证方法
所以写这篇文章

if ( ! (userSecurity.validate() && userProfile.validate() && address.validate() && photo.validate() ) ){
                    flash.message = ' Error registering user '
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }else{
                    UserSecurity.withTransaction { status ->
                            userProfile.photos*.save()
                            address?.save()
                            userProfile?.save()
                            userSecurity.password = userSecurity.password.encodeAsPassword()
                            userSecurity.confirmPassword = userSecurity.confirmPassword.encodeAsPassword()
                            userSecurity?.save()
                    }
                    flash.message = 'No Errors Registering User'
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }

是因为, &&失败并显示第一个 False 结果,并且其他验证方法不会执行。

因此将它们更改为这样

if ( ! (userSecurity.validate() & userProfile.validate() & address.validate() & photo.validate() ) ){
                    flash.message = ' Error registering user '
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }else{            ...              }

每个 bean 都会得到验证,并且所有字段错误都会得到正确呈现。

After investigating the issue.. I found the yeah beanName get passed to the template and I don't need to use $ in front of the beanName...

But, still when I use hasErrors(beans:beanName,field:'username','errors') it does not work.

But, I could do this

<bean:inputTemplate>
    <div class="prop">${label}
      <span  class="value">${field}
      </span>
      <g:if test="${errors}"><div class="errors"> ${errors} </div></g:if>
    </div>
  </bean:inputTemplate>

Even though, it didn't work, it depends on the validate method on domain classes
so writing this

if ( ! (userSecurity.validate() && userProfile.validate() && address.validate() && photo.validate() ) ){
                    flash.message = ' Error registering user '
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }else{
                    UserSecurity.withTransaction { status ->
                            userProfile.photos*.save()
                            address?.save()
                            userProfile?.save()
                            userSecurity.password = userSecurity.password.encodeAsPassword()
                            userSecurity.confirmPassword = userSecurity.confirmPassword.encodeAsPassword()
                            userSecurity?.save()
                    }
                    flash.message = 'No Errors Registering User'
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }

Because, the && fails with the first False result, and the other validate methods don't get executed.

so changing them to this

if ( ! (userSecurity.validate() & userProfile.validate() & address.validate() & photo.validate() ) ){
                    flash.message = ' Error registering user '
                    render(view:'index',model:[security:userSecurity,user:userProfile,address:address,photo:photo])
            }else{            ...              }

Every bean gets validated, and all fields errors get rendered correctly.

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