Ejemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [[['title', 'question', 'select_min', 'select_max', 'start_time', 'end_time'], 'required'], [['question', 'info'], 'string'], [['select_min', 'select_max', 'organizer_id', 'created_by', 'updated_by'], 'integer'], ['select_max', 'compare', 'compareAttribute' => 'select_min', 'operator' => '>=', 'type' => 'number'], [['start_time', 'end_time'], 'safe'], [['start_time', 'end_time'], 'date', 'format' => 'php:Y-m-d H:i:s'], ['locked', 'default', 'value' => 0], ['start_time', DateTimeCompareValidator::className(), 'compareAttribute' => 'end_time', 'format' => 'Y-m-d H:i:s', 'operator' => '<', 'message' => Yii::t('yii', '"{attribute}" must be less than "{compareAttribute}".')], [['title'], 'string', 'max' => 255]];
 }
 public function testValidateOperatorGreaterOrEqual()
 {
     $compareErrorText = Yii::t('app', '{attribute} must be greater than or equal to "{compareValue}".', ['attribute' => 'Date From', 'compareValue' => '2016-01-02']);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-02']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertEquals($errorText, $compareErrorText);
     $model = new DynamicModel(['date_from' => '2016-01-01', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
     $model = new DynamicModel(['date_from' => '2016-01-02', 'date_to' => '2016-01-01']);
     $model->addRule(['date_from', 'date_to'], 'required');
     $model->addRule(['date_from'], DateTimeCompareValidator::className(), ['compareAttribute' => 'date_to', 'format' => 'Y-m-d', 'operator' => '>=']);
     $model->validate();
     $errorText = $model->getFirstError('date_from');
     $this->assertNull($errorText);
 }