$ this-> request-> is('post')在表单提交时返回FALSE [英] $this->request->is('post') returns FALSE on form submit
本文介绍了$ this-> request-> is('post')在表单提交时返回FALSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
模型
class CompanyCategory extends AppModel
{
public $name = "CompanyCategory";
public $hasMany = array(
"Company"
);
}
控制器
public function admin_edit($id = null){
//debug($this->request);
//exit(0);
if($id == null){
$this->Session->setFlash("ID categorie eronat!", "flash/simpla_error");
$this->redirect("index");
}
if($this->request->is('post')){
if($this->CompanyCategory->save($this->request->data)){
$this->Session->setFlash("Categoria a fost salvata!", "flash/simpla_success");
}
else{
$this->Session->setFlash("Categoria NU a fost salvata!", "flash/simpla_error");
}
}
else{
$this->Session->setFlash("READ!", "flash/simpla_error");
$this->request->data = $this->CompanyCategory->read(null, $id);
}
}
视图
<div class="content-box">
<div class="content-box-header">
<h3>Editeaza categorie firme</h3>
</div>
<div class="content-box-content">
<?php
echo $this->Form->create("CompanyCategory", array(
'inputDefaults' => array(
'error' => array(
'attributes' => array(
'wrap' => 'span',
'class' => 'input-notification error png_bg'
)
)
)
));
?>
<?=$this->Form->input('id', array('type' => 'hidden'))?>
<?=$this->Form->input('title', array('class' => "text-input small-input", 'label' => 'Denumire'))?>
<?=$this->Form->submit('Salveaza', array('class' => "button"))?>
</div>
</div>
我的问题是,提交表单时,控制器为request-> is('false返回false ');
My problem is that when submiting the form, the controller returns false for request->is('false');
如果我在视图中显式设置了create方法内部的表单帮助器,则类型为 post,它将按预期工作。
If I set explicitly in the view to the form helper inside the create method the type as 'post' it works as expected.
当form方法已经发布但未设置它时,这有点令人沮丧。
It is a little bit frustrating while the form method is already post without setting it.
我做错了吗?
推荐答案
使用此控制器
public function admin_edit($id = null) {
$this->layout = 'admin_layout';
$this->CompanyCategory->id = $id;
if (!$this->CompanyCategory->exists()) {
throw new NotFoundException(__('Invalid CompanyCategory model'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->CompanyCategory->save($this->request->data)) {
$this->Session->setFlash(__('The CompanyCategory model has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The CompanyCategory model could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->CompanyCategory->read(null, $id);
}
}
这篇关于$ this-> request-> is('post')在表单提交时返回FALSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文