zoran119
zoran119

Reputation: 11327

command object data binding

I'm trying to use command objects in the following way:

class BeneficiaryCommand {
    List<Beneficiary> tempBeneficiaries = org.apache.commons.collections.list.LazyList.decorate(new ArrayList(), new org.apache.commons.collections.functors.InstantiateFactory(Beneficiary.class))

    static constraints = {
        tempBeneficiaries(validator: {
            it.each{println it.beneficiary} // it.beneficiary is null, but I expect it to have name and address from the form
        })
    }
}

class Beneficiary {
    Contractor beneficiary // Contractor has name and address properties, both non-blank and non-nullable
    Boolean save
}

The GSP looks like:

<tr class="prop">
    <td valign="top" class="name">
      <label for="beneficiaries"><g:message code="contract.beneficiaries.label" default="New Beneficiary Name" /></label>
    </td>
    <td valign="top" class="value ${hasErrors(bean: i, field: 'name', 'errors')}">
        <g:textField name="tempBeneficiaries[0].name" />
    </td>
</tr>

<tr class="prop">
    <td valign="top" class="name">
      <label for="beneficiaries"><g:message code="contract.beneficiaries.label" default="New Beneficiary Address" /></label>
    </td>
    <td valign="top" class="value ${hasErrors(bean: i, field: 'address', 'errors')}">
        <g:textField name="tempBeneficiaries[0].address" />
    </td>
</tr>

<tr class="prop">
    <td valign="top" class="name">
        </td>
    <td valign="top" class="value ${hasErrors(bean: beneficiaryCommand, field: 'save', 'errors')}">
        <div id="newBeneficiaryFlagDisplay"><g:checkBox name="tempBeneficiaries[0].save" /> Create new Beneficiary</div>
    </td>
</tr>

The save property of the command object (represented by the checkbox) is bounded to the command object, but the name and address are not. I have also tried renaming the text field names to tempBeneficiaries.beneficiary[0].name to no avail.

Any ideas on how to capture name and address into the command object?

Upvotes: 1

Views: 921

Answers (1)

schmolly159
schmolly159

Reputation: 3881

From the hierarchy of your command object the name of the field should be tempBeneficiaries[0].beneficiary.name and tempBeneficiaries[0].beneficiary.address

This is the LazyList syntax I've used before to do something similar, but I don't know if your syntax is an issue at all:

List<Beneficiary> copies = ListUtils.lazyList([], FactoryUtils.instantiateFactory(Beneficiary))

Also, you might try

Contractor beneficiary = new Contractor()

in your Beneficiary class so that an object is available at object creation time to be populated.

Upvotes: 4

Related Questions