2012-02-28 55 views
1

我正在使用元素來呈現表單。問題是,當我包括元素echo $this->element('report', array('id' => $id, 'title' => $title));,形式看起來像:CakePHP表單在元素中無法正確顯示

<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]"> 
... 

因此,所有輸入之前呈現的形式被關閉。

在視圖中單獨測試窗體(未包含在元素中)時,它使用相同的代碼(除了調用元素)正確渲染。

什麼是這個原因?

編輯

這裏的元素的代碼:

<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> 

+0

什麼是你的元素'report.ctp'文件的代碼? – 2012-02-28 21:24:20

+0

你將需要發佈元素的代碼 - 它必須是元素中非常簡單的東西。 – Dave 2012-02-28 21:24:48

+0

我已經發布了元素的代碼。我相信這是一件小事,但無法弄清楚...... – linkyndy 2012-02-28 21:27:26

回答

0

你的元素更改爲以下,看它是否正確顯示:

</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> 
+0

我需要AJAX功能,這就是爲什麼我無法使用您提供的代碼。 – linkyndy 2013-03-22 11:42:24

0

我有同樣的問題,但在視圖和評論廣告Brett F的幫助幫助了我。

我的創建包含在一個表內。我分別在表的前面和後面移動了Form-> create和Form-> end,並且所有事情都按預期工作。

這個答案的學分應該發給他。

在我的情況下,CakePHP的3.2.7