Improving Validator

Hi everyone,

I’m doing a specific validator to check my date and time, but i have some doubts about a good way to make a validator. Well, here is my current validator:


array('beginning_date, beginning_time, terminus_date, terminus_time', 'consistentDateAndTime')




    public function consistentDateAndTime()

    {

        // Change this for data validation

        if(!empty($this->beginning_date) && !empty($this->terminus_date))

        {

            $beginning_date = DateTimeFormatter::convertDate($this->beginning_date, 'br', 'us');

            $terminus_date = DateTimeFormatter::convertDate($this->terminus_date, 'br', 'us');

        

            if(strtotime($beginning_date) >= strtotime(date('Y-m-d')))

            {

                if(strtotime($beginning_date) > strtotime($terminus_date)) 

                    $this->addError('beginning_date', Yii::t('message', 'Beginning date cannot be greater than Terminus date'));


                else if(strtotime($this->beginning_date) == strtotime($this->terminus_date))

                {

                    if(strtotime($this->beginning_time) == strtotime($this->terminus_time))

                    {

                        $this->addError('beginning_time', Yii::t('message', 'Beginning time cannot be equal to Terminus time'));

                    }


                    else if(strtotime($this->beginning_time) > strtotime($this->terminus_time))

                        $this->addError('beginning_time', Yii::t('message', 'Beginning time cannot be greater than Terminus time'));

                } 

            }


            else

            {

                $this->addError('beginning_date', Yii::t('message', 

                        'Cannot insert an unavailability in the past'));

            }

        }

    }



1ª - What is the best way to get the attribute values​​?

2ª - I pretend call the data validation rule inside this validator (for check data format), how i can do that?

3ª - Considering I have four attributes, it’s possible get the four labels to put in error messages?

4ª - THE LAST ONE! I am using four attributes to compare them, but with ajax validation this give me four error messages (the same). What’s the best solution here? Split into multiple rules or put all in one?

  1. Function you’re defining takes 2 arguments: $attribute, $params. Full signuate would be



public function consistentDateAndTime($attribute, $params)



In the above $attribute is the attribute name so you can get value like $this->[$attribute]. $params is an array of validator parameters, not useful in your case.

  1. Each validator class has validateValue method that can be used for that purpose. You have to create and configure validator manually in this case.

  2. Yes. You can call $this->addError($attribute, “Message.”) and since validator will be called 4 times (as defined in attributes list of the rules() method), you’ll get 4 messages.

  3. Depends on what do you want to achieve.

I’ve updated Model::rules phpdoc to reflect this info. Thanks for asking a good question.