linkyndy
linkyndy

Reputation: 17928

CakePHP form not rendering correctly in element

I am using an element to render a form. The problem is that when I include the element echo $this->element('report', array('id' => $id, 'title' => $title));, the form appears like:

<form id="BugAdminIndexForm" class="form-vertical" accept-charset="utf-8" method="post" action="/admin/stations"></form>
<div style="display:none;">
    <input type="hidden" value="POST" name="_method">
</div>
<input id="BugType" type="hidden" value="database" name="data[Bug][type]">
...

So, the form is closed before all inputs are rendered.

While testing the form separately in the view (without it being included in the element), it renders correctly, with the same code (except the call to the element).

What's the reason for this?

EDIT

Here's the element's code:

<div class="modal fade" id="modal-<?php echo $id; ?>">
<div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h3>Report bug</h3>
</div>
<div class="modal-body">
    <?php echo $this->Form->create('Bug', array('class' => 'form-vertical')); ?>
        <?php
            echo $this->Form->hidden('type', array('value' => 'database'));
            echo $this->Form->hidden('title', array('value' => $title));

            echo $title;

            echo $this->Form->input('bug', array('div' => 'control-group', 'label' => array('text' => 'Bug', 'class' => 'control-label'), 'between' => '<div class="controls">', 'after' => '</div>', 'format' => array('before', 'label', 'between', 'input', 'error', 'after'), 'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline'))));
        ?>  
        <?php echo $this->Js->submit('Send', array(
            'url' => array('superuser' => true, 'controller' => 'bugs', 'action' => 'report'),
            'type' => 'json',
            'success' => '
                if(data === true){
                    $("#modal-'.$id.' .modal-body").html("thanks"); 
                } else if(data === false){
                    $("#modal-'.$id.' .modal-body").html("error");  
                } else {
                    $.each(data, function(field, error){
                        $input = $("#modal-'.$id.' .modal-body #Bug" + field.charAt(0).toUpperCase() + field.slice(1));
                        $input.after("<p class=\"help-block\">" + error + "</span>");
                        $input.closest(".control-group").addClass("error");
                    }); 
                }
            ',
            'div' => false
        )); ?>
    <?php echo $this->Form->end(); ?>
</div>
<div class="modal-footer">
</div>

Upvotes: 1

Views: 1215

Answers (2)

El Abogato
El Abogato

Reputation: 123

I had the same problem but in a view and the comment added by Brett F helped me.

My create was included inside a table. I moved both Form->create and Form->end just before and after the table respectively and everything worked as expected.

The credits for this answers should go to him.

In my case, CakePHP 3.2.7

Upvotes: 0

Chuck Burgess
Chuck Burgess

Reputation: 11575

Change your element to the following to see if it appears correctly:

</div>
<div class="modal-body">
    <?php 
        echo $this->Form->create('Bug');
        echo $this->Form->hidden('type', array('value' => 'database'));
        echo $this->Form->hidden('title', array('value' => $title));
        echo $this->Form->input('bug');
        echo $this->Form->end('Submit'); 
    ?>
</div>
<div class="modal-footer">
</div>

Upvotes: 1

Related Questions