doufei5315 2015-07-10 13:01
浏览 35

如在Kohana 3.3中添加规则吗?

As in Kohana 3.3 add a rule on the fly? Ie eg before saving the model User, if a certain condition is fulfilled, you need to add a rule city_id model must not be empty, try this:

if (Arr::get($data, 'val') > 0){
                    ORM::factory('User')->rules(array('city_id, array(array('not_empty'), array('digit'))));
                }

            try {
                $this->user->update_user($data);
            } catch (ORM_Validation_Exception $e) {
                $errors = $e->errors('Validation');
           }

Bypasses in any case, i.e. usually it does not work

  • 写回答

1条回答 默认 最新

  • duanguochong0397 2015-07-15 08:09
    关注

    First, you are even trying to use the wrong:

    ORM::factory('User')->rules(array ('city_id, array (array (' not_empty '), array (' digit '))));
    

    As a minimum, there should be as follows:

    ORM::factory('User')->rules(array ('city_id => array (array (' not_empty '), array (' digit '))));
    

    Without intervention in the code does not work, I will explain ...

    In the ORM:

    public function rules()
    {
        return array ();
    }
    

    Who we override in the model.

    Even a record implies a redefinition of what is already in the model.

    ORM::factory('User')->rules(array ('city_id => array (array (' not_empty '), array (' digit '))));
    

    But neither of which you can add the required method Class ORM:

    public function addRules($a2){
        $a1 = $ this->rules();
        return array_merge_recursive ($a1,$a2);
    }
    

    Then we get the following:

    ORM::factory('User')->addRules(array ('city_id => array (array (' not_empty '), array (' digit '))));
    

    Cool, but it does not work, as in:

    /**
         * Initializes validation rules, and labels
         *
         * @return void
         */
        protected function _validation()
        {
            // Build the validation object with its rules
            $this->_validation = Validation::factory($this->_object)
                ->bind(':model', $this)
                ->bind(':original_values', $this->_original_values)
                ->bind(':changed', $this->_changed);
    
            foreach ($this->rules() as $field => $rules)
            {
                $this->_validation->rules($field, $rules);
            }
    
            // Use column names by default for labels
            $columns = array_keys($this->_table_columns);
    
            // Merge user-defined labels
            $labels = array_merge(array_combine($columns, $columns), $this->labels());
    
            foreach ($labels as $field => $label)
            {
                $this->_validation->label($field, $label);
            }
        }
    

    It still causes rules(), and this is that the model ...

    You can continue the thought, but I advise you to take advantage of external validation.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)