【问题标题】:CDateValidator set null when emptyCDateValidator 为空时设置为空
【发布时间】:2013-10-14 03:22:00
【问题描述】:

我正在为一个相当简单的问题尝试不同的解决方案而发疯

我的应用程序字面上存储了数百个日期,而我客户的源数据有一个坏习惯,即日期格式不同。

例如,我的一个模型的规则如下(为了完成,所有都列出了这个特定模型)

    public function rules() {
        return array(
            array('function, junior, ces,agreement_expected,start_date', 'required'),
            array('start_budget', 'numerical'),
            array('visa_received,training_days', 'numerical', 'integerOnly' => true),
            array('function, junior, ces,currency', 'length', 'max' => 10),
            array('agreement_received, status, stop_date_original, stop_date_extended', 'safe'),
            array('agreement_received,  visa_received, stop_date_original, stop_date_extended', 'default', 'setOnEmpty' => true, 'value' => null),
            array('agreement_received,agreement_expected,start_date,stop_date_original,stop_date_extended', 'date', 'format' => 'Y-m-d', 'allowEmpty' => true),
            array('id, Sname,PFces, PFdomain,PDkeyword, PFstatus, PRname,PRcountry,PRscore,PRcomment,PRngo,TRname,TRpic, TFfunction, TFx, TRdateofbirth,TRedufields,TRcoach,TRlocation,TRtask,TRcontract,TRproject,TRcontact,TFdateofbirth,TFedufields,TFcoach,TFlocation,TFtask,TFcontract,TFproject,TFcontact, 
date1,date2,idate, ddomains,dkeywords,country,agreement, function,ngo, status,group, junior, junior_lastname, junior_firstname,search_all,search_interrupt, ces, agreement_expected, agreement_received, visa_received, start_date, stop_date_original, stop_date_extended,currency, start_budget, training_days', 'safe', 'on' => 'search'),
        );
    }

我想要达到的目标是通过以下规则来描述的

        array('agreement_received,  visa_received, stop_date_original, stop_date_extended', 'default', 'setOnEmpty' => true, 'value' => null),
        array('agreement_received,agreement_expected,start_date,stop_date_original,stop_date_extended', 'date', 'format' => 'Y-m-d', 'allowEmpty' => true),

当我有一个表单时,我在 stop_date_extended 上提交了一个空值,它不会设置为 NULL,而是一个空字符串..

我做错了什么? 当然必须有一个简单的解决方法,因为不使用日期验证器效果很好。

【问题讨论】:

    标签: php validation datetime yii


    【解决方案1】:

    Yii Default Validator 类处理空值如下:

    /framework/validators/CDefaultValueValidator.php

    protected function validateAttribute($object,$attribute)
    {
        if(!$this->setOnEmpty)
            $object->$attribute=$this->value;
        else
        {
            $value=$object->$attribute;
            if($value===null || $value==='')
                $object->$attribute=$this->value;
        }
    }
    

    鉴于上面的函数,您唯一的问题可能是通过表单发布的值 [即 $value] 可能不是空字符串,可能是 it might have spaces。可能您可以在将值分配给模型属性之前尝试 trim() 值。或者扩展默认值验证器,例如

    class CMyDefaultValueValidator extends CDefaultValueValidator
    {       
        protected function validateAttribute($object,$attribute)
        {
            if(!$this->setOnEmpty)
                $object->$attribute=$this->value;
            else
            {
                $value=trim($object->$attribute);
                if($value===null || $value==='')
                    $object->$attribute=$this->value;
            }
        }
    }
    

    只需注意这一行:$value=trim($object->$attribute);

    虽然,我还没有测试过上面的代码,但我不知道这是否可以做得更好,

    【讨论】:

      【解决方案2】:

      如果你想为空字符串插入 null,你可以在你的模型中做,验证后:

      public function afterValidate()
      {
          if(empty(trim($this->yourField))) // put whatever logic that you need
              $this->yourField = null;
      
          return parent::afterValidation();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-06
        • 2018-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        • 1970-01-01
        • 1970-01-01
        • 2019-03-29
        相关资源
        最近更新 更多