4

我在网站上有很多表格。它们都是以类似的方式创建的

<?php echo $this->Form->create('SysUser');?>
<fieldset>
<legend><?php echo __('Edit Basic Information'); ?></legend>
<?php
echo $this->Form->input('SysUser.first_name');
echo $this->Form->input('SysUser.family_name',array('label'=>__("Last Name")));
echo $this->Form->input('SysUser.mobile_phone_number');
echo $this->Form->input('SysUser.user_name',array('label'=>__("Screen Name")));
echo $this->Form->input('action', array('type'=>'hidden','value'=>'edit_basic_info'));
echo $this->Form->input('SysUser.id', array('type'=>'hidden','value'=>$user["id"]));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit'));?>

但是一种形式的类型变成了“put”,而不是“post”。创建这些表单时,我从未将类型明确设置为“发布”。我收集 CakePHP 设置要发布的默认值。现在,我创建这个新的特殊表格的方式似乎有些不对劲。奇怪的是,这是工作日前的事!

我不知道怎么了。就这个:

<?php echo $this->Form->create('Member'); ?>
<fieldset>
    <legend><?php echo __('Basic Profile Setup'); ?></legend>
    <?php
    echo $this->Form->input('Member.gender_id');
    $w = array();
    for ($i = 40; $i < 120; $i++) {
        $w[$i] = $i . " kg";
    }
    $h = array();
    for ($i = 120; $i < 230; $i++) {
        $h[$i] = $i . " cm";
    }
    echo $this->Form->input('Member.height', array(
        'options' => $h,
        'empty' => __("choose one")
    ));
    echo $this->Form->input('Member.weight', array(
        'options' => $w,
        'empty' => __("choose one")
    ));
    $options['minYear'] = date('Y') - 78;
    $options['maxYear'] = date('Y') - 18;
    echo $this->Form->input('Member.birthdate', $options);
    echo $this->Form->input('Member.residential_location_id', array('label' => __("City/Location")));
    echo $this->Form->input('Member.occupation_id',array('id'=>'MemberOccupationId'));
    echo $this->Form->input('action', array('type' => 'hidden', 'value' => 'create_member'));
    ?>
</fieldset>
<?php
echo $this->Form->end(array("label" => __('Save')));
4

6 回答 6

18

Request data包含一个 Model.idCakeRequest::method()设置为put. 在 cakephp 中处理此问题的首选方法如下。

if ($this->request->is(array('post', 'put'))) {
    // Code
}

您可以在烘焙控制器中看到这一点,编辑操作。

于 2014-10-21T19:21:09.693 回答
5

不知道为什么会这样,但您可以这样设置表单类型:

<?php echo $this->Form->create('Member', array('type' => 'post')); ?>
于 2013-02-05T18:15:15.780 回答
3

我也有这个问题。在我的情况下,当我遇到验证错误时就会发生这种情况。所以对于第二次运行,脚本认为这是一个 PUT 请求而不是 POST 请求。现在,因为它是一个 PUT,它甚至没有进入我检查它是否是一个 POST 的 if 子句,所以它会返回输入并尝试创建一个 POST 请求。这是永远循环的。

解决方案?检查 NOT GET。

所以你会得到这样的东西:

if (!$this->request->is('get')){
    //Save logic here
}

我在食谱中看到过这样的例子,但我找不到。所以我感觉它已经更新了,但就我而言,你必须使用这种方法。因此,您将涵盖一个 PUT 以及一个 POST 请求。

更新

不建议使用这种方法。它是基于是否id在表单中设置的 PUT/POST。由于我是根据请求的类型设置 id,而不是如果它确实存在,它会一遍又一遍地切换。我正在使用 1 表单来执行addedit操作。它们都使用了edit.ctp刚刚设置的更灵活的那个。

于 2013-02-05T20:03:24.620 回答
0

我遇到了同样的问题,经过 4 小时的搜索,我刚刚解决了它,将模型名称附加到视图中的字段,如下所示:

<?php echo $this->Form->create('User');?>

<?php
    echo $this->Form->input('User.id');
    echo $this->Form->input('User.username', array('readonly' => true));
    echo $this->Form->input('User.email', array('readonly' => true));
    echo $this->Form->input('User.name');
    echo $this->Form->input('User.phone');
    echo $this->Form->input('User.gender');
    echo $this->Form->input('User.locale', array('id' => 'locale_select', 'options' => array('es' => __('Spanish'), 'en' => __('English'))));
    echo $this->Form->input('User.birthday', array('type' => 'date', 'dateFormat' => 'DMY', 'minYear' => date('Y') - 100, 'maxYear' => date('Y')));
?>
<?php echo $this->Form->end(__('Save', true));?>

好吧,我不得不说这段代码是在一个插件中,所以我不知道是否还有其他问题。但是该插件中的其他形式可以完美运行,并且该形式需要具有模型名称。

于 2013-11-06T21:11:49.647 回答
0

我处理这种情况的一种方法是创建自己的检测器来定义 post OR put 的上下文。这beforeFilter()在 AppController 中的方法中:

// add a simple form post detector
    $this->request->addDetector('formPosted', array(
    'env' => 'REQUEST_METHOD',
    'options' => array('post', 'put')
));

然后,当您需要检查表单是否已发布(或“放置”)时,则:

if ($this->request->is('formPosted')) { ... }

由于在 AppController 中添加了检测器,因此可以从任何控制器方法中检查条件。

于 2014-02-20T00:03:19.670 回答
0

来自食谱

如果$this->request->data包含以表单模型命名的数组元素,并且该数组包含模型主键的非空值,则 FormHelper 将为该记录创建一个编辑表单。

可能是这样吗?什么Member是主键?

于 2013-02-05T17:31:48.003 回答