我最近将我的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将工作,但是,我不想这样做。我要的是职位,而不是推荐信。input hidden form中放置、发布或删除。Obs.:我不强制方法,使用,因为以前不需要.。
对不起我的英语。
发布于 2014-01-20 17:06:22
正如我可以从文档中推断的那样,当您向视图提供数据并基于这些数据创建表单时,Cake假设您希望编辑该表单,因此它使它成为一个"put“请求。
PUT方法来自REST服务,该方法与编辑内容相关联,而不是用于插入新内容的POST方法。因此,Cake当看到数据被传递到视图时,它解释为编辑它。
因此,如果您希望通过post接收此表单,则有两个选项:通过传递选项'type' => 'post'更改表单方法,或通过if($this->request->is('put'))更改控制器上的操作。
查看文档以获得更多参考:http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create
发布于 2014-10-21 19:18:31
就像Patrick说的,当Request data包含一个Model.id,CakeRequest::method()被设置为put。在cakephp中处理此问题的首选方法如下所示。
if ($this->request->is(array('post', 'put')) {
// Code
}您可以在烘烤的控制器中看到这一点,编辑操作。
https://stackoverflow.com/questions/21238682
复制相似问题