示例#1
0
 public function testValidateValueWithValue()
 {
     $val = new RequiredValidator(['requiredValue' => 55]);
     $this->assertTrue($val->validate(55));
     $this->assertTrue($val->validate("55"));
     $this->assertTrue($val->validate("0x37"));
     $this->assertFalse($val->validate("should fail"));
     $this->assertTrue($val->validate(true));
     $val->strict = true;
     $this->assertTrue($val->validate(55));
     $this->assertFalse($val->validate("55"));
     $this->assertFalse($val->validate("0x37"));
     $this->assertFalse($val->validate("should fail"));
     $this->assertFalse($val->validate(true));
 }
 /**
  * (non-PHPdoc)
  * @see \yii\validators\Validator::validateAttributes()
  */
 public function validateAttributes($model, $attributes = null)
 {
     //prepare attributes
     if (is_array($attributes)) {
         $attributes = array_intersect($this->attributes, $attributes);
     } else {
         $attributes = $this->attributes;
     }
     //validate all attributes with the required validator
     $reqVal = new RequiredValidator();
     $numOk = 0;
     $labelList = [];
     foreach ($attributes as $attr) {
         $labelList[] = $model->getAttributeLabel($attr);
         if ($reqVal->validate($model->{$attr})) {
             $numOk++;
         }
     }
     //check if ok or add error message
     if ($numOk < $this->numRequired) {
         //prepare message
         $msg = Yii::t('yii', $this->message, ['num' => $this->numRequired, 'num2' => $this->numRequired, 'attributes' => implode(', ', $labelList), 'errorAttribute' => $this->errorAttribute]);
         //decide where to add the message
         if ($this->errorAttribute !== null) {
             $this->addError($model, $this->errorAttribute, $msg);
         } else {
             foreach ($this->attributes as $attr) {
                 $this->addError($model, $attr, $msg);
             }
         }
     }
 }
示例#3
0
 protected function validateValue($value)
 {
     $badStructure = 'Неправильная структура данных.';
     try {
         $decoded = Json::decode($value);
     } catch (InvalidParamException $e) {
         return [$badStructure];
     }
     if (!array_key_exists(0, $decoded) || !array_key_exists(1, $decoded)) {
         return [$badStructure];
     }
     $state = $decoded[0];
     $value = $decoded[1];
     if (!in_array($state, [ComboWidget::STATE_LIST, ComboWidget::STATE_TEXT])) {
         return [$badStructure];
     }
     if ($this->required === true) {
         $validator = new RequiredValidator();
         $error = null;
         if (!$validator->validate($value, $error)) {
             return [$error];
         }
     }
     if ($value === null || $value === '') {
         $this->_targetValue = null;
         return null;
     }
     if ($state === ComboWidget::STATE_LIST) {
         $this->_targetValue = ['id' => (int) $value];
         return null;
     }
     if ($state === ComboWidget::STATE_TEXT) {
         $value = StringHelper::squeezeLine($value);
         $validator = new NazvanieValidator();
         $error = null;
         if (!$validator->validate($value, $error)) {
             return [$error];
         }
         $this->_targetValue = ['nazvanie' => $value];
         return null;
     }
     return [$badStructure];
 }
 public function validateSchedule($attribute)
 {
     $requiredValidator = new RequiredValidator();
     foreach ($this->{$attribute} as $index => $row) {
         $error = null;
         $requiredValidator->validate($row['priority'], $error);
         if (!empty($error)) {
             $key = $attribute . '[' . $index . '][priority]';
             $this->addError($key, $error);
         }
     }
 }
 public function validateSchedule($attribute)
 {
     $requiredValidator = new RequiredValidator();
     foreach ($this->{$attribute} as $index => $row) {
         $error = null;
         foreach (['user_id', 'priority'] as $name) {
             $error = null;
             $value = isset($row[$name]) ? $row[$name] : null;
             $requiredValidator->validate($value, $error);
             if (!empty($error)) {
                 $key = $attribute . '[' . $index . '][' . $name . ']';
                 $this->addError($key, $error);
             }
         }
     }
 }
示例#6
0
 /**
  * The required validation works in 3 steps:
  * 1. Get all required fields that are not in the submission (data and files). If there are, at least one,
  *    the validation fail.
  * 2. Get all required files. If a required file if not in the global $_FILES, the validation fail.
  * 3. Get all required fields that pass the last steps, and validates them with a RequiredValidator.
  */
 public function requiredFieldsValidation()
 {
     // Messages
     $requiredMessage = Yii::t('app', 'the input value is required.');
     // Compares requiredLabels keys against data and files keys, and returns the difference
     // All requiredLabel that are not in the submission data and file uploads
     $requiredFields = array_diff_key($this->requiredLabels, $this->data, $_FILES);
     // If exist a requiredField, add a required error
     // Useful to validate if a least one checkbox of a group is checked
     if (count($requiredFields) > 0) {
         foreach ($requiredFields as $name => $label) {
             $this->addError($name, $label, '', $requiredMessage);
         }
     }
     // Check all required File Inputs with $_FILES
     foreach ($this->requiredFileLabels as $name => $label) {
         if (!is_array($_FILES) || !isset($_FILES[$name]) || !isset($_FILES[$name]['name']) || empty($_FILES[$name]['name'])) {
             // If no file was upload
             $this->addError($name, $label, '', $requiredMessage);
         }
     }
     // Get all submission data, that were not in the last validations
     $data = array_diff_key($this->data, $requiredFields, $this->requiredFileLabels);
     // Filter required data
     $requiredData = array_intersect_key($data, $this->requiredLabels);
     // Check each fields item with requiredValidator
     $requiredValidator = new RequiredValidator();
     // If a field does'nt pass the validator, add a blank error
     if (count($requiredData) > 0) {
         foreach ($requiredData as $name => $value) {
             if (!$requiredValidator->validate($value, $error)) {
                 $this->addError($name, $this->requiredLabels[$name], '', $error);
             }
         }
     }
 }
 private function internalValidateAnswers($questionIndex, $answers)
 {
     $requiredValidator = new RequiredValidator();
     $error = null;
     foreach ($answers as $answerIndex => $answer) {
         $error = null;
         $value = ArrayHelper::getValue($answer, 'answer', null);
         $requiredValidator->validate($value, $error);
         if (!empty($error)) {
             $key = sprintf('questions[%d][answers][%d][answer]', $questionIndex, $answerIndex);
             $this->addError($key, $error);
         }
     }
 }