模型
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>
我的问题是,在提交表单时,控制器为请求返回 false>is('false');
如果我在视图中显式设置为创建方法内的表单助手,则将类型设置为"post",它将按预期工作。
当表单方法已经在没有设置的情况下发布时,这有点令人沮丧。
我做错了什么吗?
使用此控制器
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->Form->create()
您应该有一个选项可以将表单操作类型设置为发布。
如果您查看 html 中 PHP 代码正在生成表单,它是否创建了 的 action 属性? 我的猜测是它可能默认做 GET。