views:

112

answers:

4

Hi everybody,

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.

A: 

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

tim_yates
A: 

tim_yates,

I am already using BeanFields.. That is why there is the tag of inputTemplate

I want to use hasErrors() with the template, passing $beanName to hasErrors() and it figure out thhe beanName (e.g. securityInstance )

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

so that when I type in code

<bean:field beanName="securityInstance" field="username" />

It gets substituted with

<div class="prop ${hasErrors(bean:securityInstance,field:'username','errors')}">Username:
  <span  class="value">
     <input type="text" class="" id="username" name="username" />
  </span>
</div>

This is all what I want, but because I call hasErrors() in ${} in the bean:inputTemplate tag , I cant inside the ${hasErrors()} use another ${} to be like this

${hasErrors(${somestuff})}

so,, anybody, help ?!!

A: 

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.

leebutts
A: 

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.