蛋糕php模型验证不起作用

时间:2013-02-14 05:06:06

标签: php mysql cakephp

我想在表格中插入记录。为此我有模型,视图和控制器。我的代码中的所有内容都完美地工作但我的验证模型代码没有显示任何验证消息。我该怎么办?我在下面给出代码:

我的控制器代码:

    public function send_money()
        {

         $this->layout='agent';
         $this->Agent->create();
         $this->Agent->set($this->data);

           if(empty($this->data) == false)
            {
                //$this->Agent->saveAll($this->data['Agent'], array('validate' => 'only')); //This code Id New
                $this->Agent->saveAll($this->data['Agent']);
                $this->Session->setFlash('Information Added Successfully.');
                $this->redirect('send_money');

            }
            else
            {
                $this->set('errors', $this->Agent->invalidFields());    
            }

        }

And My Model Code is :



    App::uses('AppModel', 'Model');
    /**
     * Admin Login Model
     *
     */
     class Agent extends AppModel
     {
        public $name='Agent';
        public $usetables='agents';

        public $validate = array(

         'contact' =>array(
          'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
          'allowEmpty' => false,
          'message'    => 'Please Enter Contact No.'
        ),
         'name' =>array(
          'rule' => 'notEmpty', // or: array('ruleName', 'param1', 'param2' ...)
          'allowEmpty' => false,
          'message'    => 'Please Enter Name.'
        ),

         'email_add' =>array(
          'rule' => 'email', // or: array('ruleName', 'param1', 'param2' ...)
          'allowEmpty' => false,
          'message'    => 'Please Enter Valid Email.'
        ),
        );

     }

3 个答案:

答案 0 :(得分:0)

变化:

$this->Form->create('Agents',

$this->Form->create('Agent',

由于您的型号名称为Agent而非Agents 见这里:Model Validation

答案 1 :(得分:0)

在你的控制器中使用它:

if($this->Agent->validates($this->data)) {

而不是:

if(empty($this->data) == false)

答案 2 :(得分:0)

试试这个:

 public function send_money()
    {

     $this->layout='agent';
     $this->Agent->create();
     $this->Agent->set($this->data);

       if($this->Agent->saveAll($this->data['Agent'])) {
            $this->Session->setFlash('Information Added Successfully.');
            $this->redirect('send_money');
        }
        else {
            $this->set('errors', $this->Agent->invalidFields());    
        }

    }

注意:要记录错误验证,请使用此debug($this->Agent->validationErrors);