1

我尝试了所有方法,但它蛋糕 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');
4

2 回答 2

0

尝试

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

文档示例中所示。不知道为什么你添加了一个额外的数组。

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

public $validate=array(
    'name' => array(
        'rule'    => 'notEmpty',
        'message' => 'A username is required',
        'required' => true,
        'allowEmpty' => false
    )
);
于 2013-08-09T03:30:51.803 回答
0

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

于 2013-08-12T08:01:50.337 回答