Cakephp FormHelper 不输出“
”标签

发布于 2024-08-21 17:19:59 字数 6175 浏览 10 评论 0原文

所以我有一个用户控制器,其操作名为“selfView”,它看起来像这样: (users_controller.php)

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');
}

这将呈现一个配置文件页面,其中包含一个表单。这是页面上呈现的第一个表单,它的工作效果正如您所期望的那样。

查看代码片段: (users/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 形式为“users/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>

然后我调用 和 元素,该元素也将渲染一个表单。 (users/view.ctp)

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

然后这就是一切出错的地方。无论我如何尝试,我都无法正确打印第二张表格。表格的中间部分将打印出来,只是不打印开始和结束标签。没有它们,表格对我来说毫无用处。此时我真的不想用手写出来。

这是我的元素中尝试用来输出表单的代码(elements/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));?>

这就是“elements/my_apps.ctp”打印的内容out:

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

有什么想法吗?我应该尝试做的一些事情来调试会很有帮助。

更新:

我只是尝试手动输入代码,甚至它也被删除了。是否正在进行某种后期处理?这可以负责删除我的表单标签吗?

So I have a users controller with an action called "selfView" it looks like this:
(users_controller.php)

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');
}

This renders a profile page which has one form on it. This is the first form rendered on the page and it works just fine as you would expect.

View code snippet:
(users/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>

the HTML that is Produced form "users/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">
  //<![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;});
  //]]>
  </script> 
  <a href="#">Cancel</a> 
</form>

Then I call and element that will also render a form.
(users/view.ctp)

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

And then this is where it all goes wrong. No matter what I try I can not get this second form to print out correctly. The middle part of the form will print out,just not the opening and closing tags. With out them the form is useless to me. At this point and I really do not want to write it out by hand.

This is the code in my element that I am trying to use to output the form(elements/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));?>

this is what "elements/my_apps.ctp" prints out:

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

It WILL NOT Print the opening and closing tags which should be something like this:

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

Any ideas? Something that I should try to do to debug would be helpful.

Update:

I just tried witting the code in by hand and even that is being stripped out. Is there some sort of post-processing going? Could that be responsible for removing my form tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青丝拂面 2024-08-28 17:19:59

在您的第一个表单中,您没有回显 $form->end() ,因此表单未正确关闭。

In your first form your are not echoing $form->end() so the form is not properly closing.

余生再见 2024-08-28 17:19:59

我还没有发现任何问题,但我可以建议,为了您的理智,您可以在第一种形式中为变量 $options 使用不同的名称,或者更好的是,内联写入数组。更常见的是使用 $options 来传递选择选项,就像在第二种形式中所做的那样。即保存 $options 为“选项”。

在您的函数 selfView() 中,您似乎没有为 applications & 赋值。 表单

我个人的偏好是这样写:

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

像这样:

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

我认为这使代码更具可读性。

I can't see anything wrong yet, but can I suggest that for your sanity, you either use a different name for the variable $options in the first form, or better still, write the array inline. It is more common to see $options used to pass the selection choices as you have done in the second form. I.e. save $options for 'options'.

In your function selfView() you don't appear to assign values to applications & formms.

My personal preference is to write this:

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

like this:

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

I think it makes the code slightly more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文