2

我想在表中插入记录。为此,我有模型、视图和控制器。我的代码中的一切都运行良好,但我的验证模型代码没有显示任何验证消息。我该怎么办?我在下面给出代码:

我的控制器代码:

    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.'
        ),
        );

     }
4

3 回答 3

0

Use this in your controller:

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

Instead of:

if(empty($this->data) == false)
于 2013-02-14T05:17:27.710 回答
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);

于 2014-09-09T04:58:17.227 回答
0

改变:

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

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

由于您的模型名称Agent不是Agents 请参见此处:模型验证

于 2013-02-14T05:15:45.850 回答