【问题标题】:cake 2.1 data validation not workingcake 2.1 数据验证不起作用
【发布时间】:2013-07-13 01:07:44
【问题描述】:

我尝试了所有方法,但它蛋糕 php 数据验证不起作用。你能告诉我哪里出错了吗? 我添加了所有类型的验证,但表单仍然保存没有验证数据!

我的模特:

class contacts extends AppModel
{
    public $name = 'contact';
    public $useTable='contacts';    
    public $validate=array(
        'name' => array(
            'requierd'=>array(
                'rule'=>array('Not empty'),
                'message' => 'A username is required'
        )
        )
    );

}

我的控制器

class ContactsController extends AppController
{
    public $helpers=array('Html','Form');
    public $components=array('session','Email');

    public function index()
    {
        if($this->request->is('post'))
        {
             $this->Contact->create();
            if($this->Contact->save($this->request->data))
            {

                $this->Session->setFlash('Thank you we will contact you soon');
                $this->redirect(array('action'=>'index'));
            }
            else
            {
                $this->Session->setFlash('Unable to send your message.');
            }

        }

    }
}

我的看法:

echo $this->Form->create('Contact');
echo $this->Form->input('name');
echo $this->Form->input('email');
echo $this->Form->input('subject');
echo $this->Form->input('message',array('rows' => '3'));
//echo $this->Form->checkbox('Send me a coppy');
echo $this->Form->end('Send');

【问题讨论】:

    标签: cakephp-2.1 validation


    【解决方案1】:

    试试

    public $validate=array(
        'name' => array(
            'rule'    => 'notEmpty',
            'message' => 'A username is required'
        )
    );
    

    如在docs example 中所见。不知道你为什么添加了一个额外的数组。

    您可以添加 requiredallowEmpty 索引只是为了更严格

    public $validate=array(
        'name' => array(
            'rule'    => 'notEmpty',
            'message' => 'A username is required',
            'required' => true,
            'allowEmpty' => false
        )
    );
    

    【讨论】:

      【解决方案2】:

      我想通了。他们不工作的原因是因为型号名称!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多