【问题标题】:Cake php Model Validation Not WorkingCake php模型验证不起作用
【发布时间】:2013-02-14 05:06:05
【问题描述】:

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

我的控制器代码:

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

     }

【问题讨论】:

  • create('Agents' 将其替换为 create('Agent'

标签: php mysql cakephp


【解决方案1】:

在你的控制器中使用它:

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

代替:

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

【讨论】:

  • 将此代码放入我的控制器后,不会显示任何页面。错误消息类似于:页面未正确重定向,Firefox 检测到服务器正在重定向对该地址的请求永远不会完成的方式..如果如果删除 $this->redirect('send_money'); 它工作但显示 3 警告,如:1.未定义索引:代理 [APP\Controller\AgentsController.php,第 74 行] 2.array_keys () 期望参数 1 为数组,给定 [CORE\Cake\Model\Model.php,第 2035 行]
  • 第三个警告是传递给 Hash::numeric() 的参数 1 必须是一个数组,给定 null,在 C:\wamp\www\astha\lib\Cake\Model\Model 中调用。 php 在第 2035 行并定义 [CORE\Cake\Utility\Hash.php, line 618] 并且我的 flash 消息会自动显示。我该怎么办?
  • 你有 $uses = array('Agent');作为您的控制器的成员?
【解决方案2】:

改变:

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

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

因为您的型号名称是 Agent 而不是 Agents 见这里:Model Validation

【讨论】:

    【解决方案3】:

    试试这个:

     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);

    【讨论】:

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