0

Currently, i have succeeded in creating the checkbox. The array which i have setup is as below:

$emailName = $this->User->find('list', array(
    'fields' => array('User.username', 'User.email')
    ));

The output is as follows:

array
  'admin' => string '[email protected]' (length=11)
  'test' => string '[email protected]' (length=14)
  'Floo' => string '[email protected]' (length=16)

I'm trying to make the checkbox shows the username instead of the user email in view.ctp.

I have tried using the following code in view.ctp

<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
    'type' => 'select',
    'multiple' => 'checkbox',
    'options' => $emailName['email']
    )); ?>

However, it seems that this doesn't work. Any ideas?

2
  • 2
    First, you're not following cake conversions. I'm assuming that "Address_list" is a made-up field. To make this work, simply remove the [ and ] from the field name and that should solve your problem. Commented Feb 15, 2012 at 5:59
  • Nope, tried removing it but doesn't work. Commented Feb 15, 2012 at 6:44

1 Answer 1

3

You are not formatting your form field for the checkbox list correctly. Try changing it to this:

echo $this->Form->input('Address_list', array(
    'multiple' => 'checkbox',
    'options' => $emailName, 
));

However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:

<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin" 
   name="data[Address_list][]"/><label for="AddressListAdmin">[email protected]</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
   name="data[Address_list][]"/><label for="AddressListTest">[email protected]</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo" 
   name="data[Address_list][]"/><label for="AddressListFloo">[email protected]</label></div>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. this works.. To achieve what i want, i re-code the find method. so that email comes first and the name comes 2nd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.