2010-02-17 71 views
1

所以我必须有动作的用户控制器称为“selfView”它看起来像这样: (users_controller.php中CakePHP的表单助手不输出“<form>”标签

function selfView() { 
    $user = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')))); 
    $formms = $this->Form->find('all', array('fields' => array('Form.id', 'Form.name'), 'recursive' => -1)); 
    $this->set(compact('user', 'formms')); 
    $this->render('view'); 
} 

这使得轮廓页面上有一个表单。这是在页面上呈现的第一张表单,它的工作原理正如您所期望的那样。

查看代码片断: (用户/ view.ctp

<div id="passwordChange"> 
     <?php echo $form->create('User', array('action' =>'updatePass'));?> 
      <fieldset> 
      <legend><?php __('Change Password');?></legend> 
      <?php 
       $options = array('type'=>'password', 'div' => array('class' => 'required')); 
       echo $form->input(__('old_password', true), $options); 

       $options = array('div' => array('class' => 'required'), 'error' => array('confirmPassword' => __('Passwords do not match please try again',true), 'noempty' => __('Please provide a password',true))); 
       echo $form->input(__('password', true), $options); 

       $options = array('type'=>'password', 'div' => array('class' => 'required')); 
       echo $form->input(__('confirmation_password', true), $options); 

       echo $form->hidden('User.id', array('value' => $user['User']['id'])); 
      ?> 
      </fieldset> 
     <?php echo $ajax->submit(__('Change Password', true), array('url' => array('controller' => 'users', 'action' => 'updatePass'), 
        'update' => 'storage', 
        'complete' => '$("#UserUpdatePassForm input[type=password]").val(""); ')); 
    $form->end();?> 
        <a href="#">Cancel</a> 
     </div> 

HTML所产生的形式 “用户/ view.ctp”:

<form action="/high-school-app/users/updatePass" method="post" id="UserUpdatePassForm"> 
    <fieldset style="display: none;"> 
    <input type="hidden" value="POST" name="_method"> 
    </fieldset> 
    <fieldset> 
    <legend>Change Password</legend> 
    <div class="required"> 
     <label for="UserOldPassword">Old Password</label> 
     <input type="password" id="UserOldPassword" value="" name="data[User][old_password]"> 
    </div> 
    <div class="required required"> 
     <label for="UserPassword">Password</label> 
     <input type="password" id="UserPassword" value="" name="data[User][password]"> 
    </div><div class="required required"> 
     <label for="UserConfirmationPassword">Confirmation Password</label> 
     <input type="password" id="UserConfirmationPassword" value="" name="data[User][confirmation_password]"> 
    </div> 
    <input type="hidden" id="UserId" value="" name="data[User][id]"> 
    </fieldset> 
    <div class="submit"> 
    <input type="submit" onclick="return false;" id="submit2026598409" value="Change Password" update="storage"> 
    </div> 
    <script type="text/javascript"> 
    //&lt;![CDATA[ 
    jQuery('#submit2026598409').click(function() { jQuery('#submit2026598409').parents('form').ajaxSubmit({beforeSend:function(request) {request.setRequestHeader('X-Update', 'storage');}, complete:function(request, textStatus) {$("#UserUpdatePassForm input[type=password]").val(""); }, success:function(data, textStatus) {jQuery('#storage').html(data);}, async:true, type:'post', url:'/high-school-app/users/updatePass'}); return false;}); 
    //]]&gt; 
    </script> 
    <a href="#">Cancel</a> 
</form> 

然后我打电话给那个也会呈现一个表单的元素。 (用户/ view.ctp

echo $this->element('my_apps' , array('applications' => $applications)); 

然后这是它一切都错了。无论我尝试什么,我都无法获得第二种形式来正确打印。表格的中间部分将打印出来,而不是打开和关闭标签。对他们来说,表格对我来说毫无用处。在这一点上,我真的不想亲手写出来。

这是我的元素,我想用它来输出的形式(元/ my_apps.ctp)代码:

<?php echo $form->create('Applications', array('url' => array('controller' => 'applications', 'action' => 'assign', 'id' => null))); ?> 
     <fieldset> 
     <legend><?php __('Assign Application to User');?></legend> 
     <?php 
      $options = array('--' => __('Select One',true), 'test' => 'test', 'test2' => 'test2'); 
      echo $form->label('Application.form_id', __('Form Name',true).":"); 
      echo $form->select('Application.form_id', $options, $selected = '--'); 

      echo $form->hidden('Application.user_id', array('value' => $user['User']['id'])); 
     ?> 
     </fieldset> 
<?php echo $form->end(__('Assign', true));?> 

这就是“元/ my_apps.ctp”打印出:

<fieldset style="display: none;"> 
    <input type="hidden" value="POST" name="_method"> 
</fieldset> 
<fieldset> 
    <legend>Assign Application to User</legend> 
    <label for="ApplicationFormId">Form Name:</label> 
    <select id="ApplicationFormId" name="data[Application][form_id]"> 
    <option value=""></option> 
    <option selected="selected" value="--">Select One</option> 
    <option value="test">test</option> 
    <option value="test2">test2</option> 
    </select> 
    <input type="hidden" id="ApplicationUserId" value="" name="data[Application][user_id]"> 
</fieldset> 
<div class="submit"> 
    <input type="submit" value="Assign"> 
</div> 

不会打印应该是这样的开始和结束标记:

<form action="/high-school-app/applications/assign" method="post" id="ApplicationAssignForm"> 
..... 
..... 
</form> 

任何想法?我应该尝试去调试的东西会很有帮助。

更新:

我只是试图用手知晓这些代码甚至正在被剥离出来。是否有某种后期处理?可以负责删除我的表单标签吗?

+0

不应该有任何后处理,这取决于你的意思是“手写”。你有没有CTP文件中的HTML?因为肯定不应该被剥离。你会把你的布局文件和你的模板文件的pastebin链接给我吗?我之前有过这个概念,但不记得我是如何解决它的。看到你的布局/视图文件可能会有所帮助。 – 2010-02-17 17:46:49

+0

对不起,从德国发布。我试图在ctp文件中编写html文件,但它仍然删除了“表格”标签。 这里是一个pastebin链接: http://pastebin.com/m5e68275d 如果有什么可以帮助让我知道。 再次为延迟抱歉。 – MrDevin 2010-02-18 08:29:09

+0

所以没有答案我juet做了一个窗体的ajax请求,并将它放在模态窗口中。效果很好。 – MrDevin 2010-03-01 16:26:45

回答

0

我看不出什么错误,但我可以建议,为了您的理智,您可以在第一种形式中为变量$options使用不同的名称,或者更好的是,将数组内联写入。看到$options用于通过选择选项更为常见,就像您在第二种形式中所做的那样。即保存'选项'的$选项。

在您的功能selfView()中,您似乎没有将值分配给applications & formms

我个人的偏好是这样写:

<?php echo $form->submit(__('Assign', true)); 
     echo $form->end();?> 

这样的:

<?php echo $form->end(__('Assign', true)) ?> 

我认为它使代码稍微更具可读性。

+0

感谢您使我的代码更具可读性的提示,我已经做了一些更改以纳入您的建议。 – MrDevin 2010-02-19 09:06:05

4

在你的第一种形式中,你没有回显$ form-> end(),所以表单没有正确关闭。

+0

这应该被标记为正确的答案。如果你不回应结束的$ form-> end(),那么第二个表单根本就不会呈现。 – 2016-12-08 14:33:25