我最近将CakePHP项目更新为2.4.5。
现在,有些forms
将输入隐藏 = PUT 。但是,method
是POST。
我不知道为什么会这样。
这是表格:
<?php echo $this->Form->create('User', array('url' => array('action' => 'new_password', $this->request->data['User']['forget_password'], 'admin' => false), 'autocomplete' => 'off')) ?>
<?php echo $this->Form->hidden('User.id') ?>
<fieldset>
<label class="block clearfix">
<span class="block input-icon input-icon-right">
<?php echo $this->Form->password('User.password', array('label' => false, 'div' => false, 'class' => 'form-control', 'placeholder' => 'Digite a nova senha')) ?>
<i class="icon-user"></i>
</span>
</label>
<label class="block clearfix">
<span class="block input-icon input-icon-right">
<?php echo $this->Form->password('User.password_confirmation', array('label' => false, 'div' => false, 'class' => 'form-control', 'placeholder' => 'Digite novamente a nova senha')) ?>
<i class="icon-user"></i>
</span>
</label>
<div class="space"></div>
<div class="clearfix">
<?php echo $this->Form->button('<i class="icon-key"></i> '. __('Enviar'), array('class' => 'width-35 pull-right btn btn-sm btn-success', 'escape' => false)) ?>
</div>
<div class="space-4"></div>
</fieldset>
<?php echo $this->Form->end() ?>
而且,行动:
/**
* new_password method
*
* @access public
* @param String $forget_password
* @return void
* @since 1.0
* @version 1.0
* @author Patrick Maciel
*/
public function new_password($forget_password)
{
$user = $this->User->findByForgetPassword($forget_password);
if ($user == false) {
$this->Session->setFlash(__('Link inválido'), 'flash/frontend/error');
$this->redirect('/');
}
$this->layout = 'login';
if ($this->request->is('post')) {
$this->User->set = $this->request->data;
if ($this->User->validates(array('fieldList' => array('id', 'forget_password', 'password', 'password_confirmation')))) {
// ...
} else {
// ...
}
}
$user['User']['password'] = null;
$this->request->data = $user;
}
$this->request->is('put')
而不是POST将起作用,但是,我不想这样做。我想要一个POST,而不是一个PUT。input hidden form
中设置PUT,POST或DELETE。 Obs。:我不强迫方法,使用'type' => 'POST'
,因为之前不需要这样做。
抱歉我的英文。
答案 0 :(得分:4)
正如我可以从文档中推断出的那样,当您向视图提供数据并根据此数据创建表单时,Cake假定您要编辑表单,因此它将其作为“put”请求。
PUT
方法来自REST服务,此方法与编辑内容相关联,而不是用于插入新内容的POST
方法。因此,当看到传递给视图的数据时,它会解释为编辑它。
因此,如果您希望通过邮寄方式接收此表单,则有两种选择:通过传递选项'type' => 'post'
来更改表单方法,或者通过if($this->request->is('put'))
更改控制器
查看文档以获取更多参考: http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
答案 1 :(得分:2)
就像帕特里克所说,当Request data
包含Model.id CakeRequest::method()
设置为put
时。在cakephp中处理此问题的首选方法如下。
if ($this->request->is(array('post', 'put')) {
// Code
}
您可以在baked控制器中看到此信息,编辑操作。