0

I have a foreach that show many forms with the same action ending with diferente id's. But, the tag <form> just appears in the first form. All others, the fields appears, but don't the <form> I tried to put the id for the form different in the loop. But doesn't work.

The code:

    <?php echo $this->Form->create(null, array(
                'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
                )); ?>
                <div class="control-group">
                <label class="control-label">Alçada:</label>
                <div class="controls">
                <?php echo $this->Form->input ('alcada', array('type' => 'select', 'label' => FALSE, 'options' => array(
                                                                                 'Até 10.000' =>  'Até 10.000',
                                                                                 'Até 50.000' => 'Até 50.000',
                                                                                 'Acima de 100.000' => 'Acima de 100.000',
                                                                                 'Acima de  500.000' => 'Até 500.000',),

                                                                                 'empty' => 'Selecione')); ?>
                </div>
                </div>

                <div class="control-group">
                <label class="control-label">Validade:</label>
                <div class="controls">
                <?php echo $this->Form->input('validade', array('label' => FALSE, 'type' => 'text')); ?>
                </div>
                </div>
                <?php echo $this->Form->submit('Ok', array('class' =>'btn btn-success pull-left', 'div' => false)); ?>
                </div>

The field "Alçada" and "Validade" appears correctly. But the tag <form> just appears in the first element.

Igor Martins
  • 2,015
  • 7
  • 36
  • 57

1 Answers1

0

You are not ending the form.

echo $this->Form->create(null, array(
            'id' => 'your-form-'.$i, //that $i is the index of the foreach, for example
            'url' => array('controller' => 'menus', 'action' => 'aprovar', $procuracao['Attorney']['id']), 'id' => $procuracao['Attorney']['id']
            ));
//all inputs and other stuff
echo $this->Form->end(array('label'=>'Ok', 'class' =>'btn btn-success pull-left', 'div' => false));

all that inside the foreach you're using.

Here is the reference of that function in the docs. But basically, it does this

Closes an HTML form, cleans up values set by FormHelper::create(), and writes hidden input fields where appropriate

Nunser
  • 4,512
  • 8
  • 25
  • 37
  • Hi!!! Doesn't work. The form on the first element its fine!! But in the rest doesn't appear the `
    `
    – Igor Martins Aug 13 '13 at 17:12
  • Probably a clash of names or ids. FormHelper will give the same id to those forms, so you will need to prevent that passing a variable id for that. I will update the answer, but have this in mind: how will you know which form was submitted if all will have the same model? every form submit will be set in `$this->request->data['Model']` in the controller, so the foreach will be useless, no? Also, forms in the same view will lead to the [multiple form validation problem`(http://stackoverflow.com/questions/16669857/multiple-form-with-same-model-name-on-single-page-cakephp/17495130#17495130) – Nunser Aug 13 '13 at 17:22
  • i tried to change the id and the name of every form but doesn't work also – Igor Martins Aug 13 '13 at 17:35