CakePHP表单不工作


CakePHP form not working

我已经为此创建了一个控制器、一个模型和一个视图。我想在CakePHP表单。但这是行不通的,直到现在我还不明白为什么会发生这种情况…

我的控制器代码是:

class MlistsController extends AppController {
    public $helpers = array('Html','Form');
    public function create() {
        if ($this->request->is('post')) {
            if ($this->Mlist->save($this->request->data)) {
                $this->Session->setFlash(__('okay..'));
                $this->redirect('action' => 'index');
            }
        }
    }    
}

My Model is:

App::uses('AuthComponent', 'Controller/Component');
class MList extends AppModel {
    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }
    public $validate = array(
        'listname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A listname '
            )
        ),
        'replyto' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'email' => 'email'
            )
        ),
        'fromName' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your name'
            )
        ),
        'subject' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A subject '
            )
        ),
        'reminder' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A reminder '
            )
        ),
        'contactsfile' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'custom message'
            )
        ));
}

和我的视图文件create。ctp是:

<h2>Create new list</h2>
<?php
    $this->Form->create('Mlist');
    echo $this->Form->input('listname',array('label' => 'Your ListName:'));;
    echo $this->Form->input('replyto',array('label' => 'Reply To email:'));
    echo $this->Form->input('fromName',array('label' => 'From Name:'));
    echo $this->Form->input('subject',array('label' => 'mail subject:'));
    echo $this->Form->input('reminder',array('label' => 'Reminder'));
    echo $this->Form->input('contactsfile',array('label' => 'Upload your file','type' => 'file'));
    echo '<br />';
    echo $this->Form->end('submit');

最后,表单的Submit按钮甚至不是绿色的,而是灰色的,当我点击它时不起作用。此外,星号(*)没有显示表单标签,其中字段是必需的…

你能帮我解决这个问题吗?

你在某种程度上绕过了Cake的"约定优于配置"范例。为了使这些约定发挥作用,Cake使用了Inflector类来处理英语单词的复数形式。

所以当你使用合适的命名策略时,它会为你"开箱即用"。否则,你将不得不配置模型、控制器和你正在使用的任何助手/组件/行为。它们都有相应的配置参数,但是使用Cake进行这种静态配置并没有什么大的意义,因为如果您重命名一个DB表,您还必须再次进行配置。我只是反对蛋糕的想法。如果你需要这样做,只需使用其他基于配置的框架。

我相信问题出在你的观点上。您还必须回显表单的开头:

    echo $this->Form->create('Mlist');

命名约定没有问题。Cake没有任何真实英语单词的数据库,只有一些不规则的情况(参见http://api.cakephp.org/2.3/source-class-Inflector.html中的数组),所以像Mlist这样的东西只是在末尾添加"s"来复数化。