Yii Framework Forum: Improving Validator - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Improving Validator

#1 User is offline   KillCloud 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 02-November 12
  • Location:São Paulo, Brazil

Posted 06 September 2013 - 08:31 AM

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?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,605
  • Joined: 17-January 09
  • Location:Russia

Posted 06 September 2013 - 09:40 AM

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.

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

3. 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.

4. Depends on what do you want to achieve.
0

#3 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,605
  • Joined: 17-January 09
  • Location:Russia

Posted 06 September 2013 - 09:44 AM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users