开发者

Variable Variable in Groovy

开发者 https://www.devze.com 2022-12-09 18:38 出处:网络
I\'m in a project using Grails, I user beanFields plugin where I\'m changing the bean:inputTemplate into the following

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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消