0

I have a form for creating a task, and when creating it, user is asked to select which employees will be assigned to it. There may be just one employee or even up to 10. I allow user to dynamically create those input fields on the go, but the array that i get after the form submition looks like this:

array(
    'Event' => array(
        'project_id' => '62',
        'user_id' => '23',
        'user_id2' => '24',
        'user_id4' => '28',
        'user_id8' => '30',
        'hours' => '6',
        'minutes' => '0',
        'assignment' => '',
        'material' => 'safsaf',
        'date' => '2013-10-12',
    )
)

The problem is I do not know how to iterate over the user_ids. Is it possible to save the IDs as a list? Or is there any other solution?

3
  • What does your form look like, especially the inputs that define the employees? And have you set up a proper HABTM relationship between events and employees? Commented Oct 16, 2013 at 18:07
  • You need to use HABTM Commented Oct 16, 2013 at 19:00
  • Well, not really, that is the only situation where I need to use it. I create new input fields by jQuery if users clicks on "add more". Commented Oct 17, 2013 at 8:55

1 Answer 1

1

Use CakePHP's find('list') to retrieve the $users in an key=>value array, then set the multiple attribute of the input to true:

echo $this->Form->select('Model.field', $users, array('multiple' => true));

$attributes['multiple'] If ‘multiple’ has been set to true for an input that outputs a select, the select will allow multiple selections:

Sign up to request clarification or add additional context in comments.

2 Comments

Worked a charm. It is just that I do not really like how the list looks in the final output. Users needs to know that CTRL buttons needs to be pressed which may not be obvious in some cases.
Change to 'multiple'=>'checkbox' instead of 'multiple'=>true (this is all in the CakePHP Book online.

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.