/** * answerValidation 登録内容の正当性 * * @param object &$model use model * @param array $data Validation対象データ * @param array $question 登録データに対応する項目 * @param array $allAnswers 入力された登録すべて * @return bool */ public function answerTextValidation(&$model, $data, $question, $allAnswers) { if (!in_array($question['question_type'], $this->_textValidateType)) { return true; } $ret = true; // 数値型登録を望まれている場合 if ($question['question_type_option'] == RegistrationsComponent::TYPE_OPTION_NUMERIC) { if (!Validation::numeric($data['answer_value'])) { $ret = false; $model->validationErrors['answer_value'][] = __d('registrations', 'Number required'); } if ($question['is_range'] == RegistrationsComponent::USES_USE) { $rangeRes = Validation::range($data['answer_value'], intval($question['min']), intval($question['max'])); if (!$rangeRes) { $ret = false; $model->validationErrors['answer_value'][] = sprintf(__d('registrations', 'Please enter the answer between %s and %s.'), $question['min'], $question['max']); } } } else { if ($question['is_range'] == RegistrationsComponent::USES_USE) { if (!Validation::minLength($data['answer_value'], intval($question['min'])) || !Validation::maxLength($data['answer_value'], intval($question['max']))) { $ret = false; $model->validationErrors['answer_value'][] = sprintf(__d('registrations', 'Please enter the answer between %s letters and %s letters.'), $question['min'], $question['max']); } } } return $ret; }
/** * testMinLength method * * @return void */ public function testMinLength() { $this->assertFalse(Validation::minLength('ab', 3)); $this->assertFalse(Validation::minLength('ÆΔΩЖÇ', 10)); $this->assertTrue(Validation::minLength('abc', 3)); $this->assertTrue(Validation::minLength('abcd', 3)); $this->assertTrue(Validation::minLength('ÆΔΩЖÇ', 2)); }
/** * testMinLength method * * @access public * @return void */ function testMinLength() { $this->assertFalse(Validation::minLength('ab', 3)); $this->assertTrue(Validation::minLength('abc', 3)); $this->assertTrue(Validation::minLength('abcd', 3)); }
/** * @testdox maxLength should return false to strings exceeding specified length */ public function testInvalidMinLength() { $value = '4'; $this->assertFalse(Validation::minLength($value, 2)); }
/** * Validates that at least one field is not empty * * @param string $check field to check * @param string $params array of fields that are related * @return boolean true if at least one field has been filled, false otherwise * @author Jose Diaz-Gonzalez * @access public */ function validateDependentFields(&$model, $check, $params = array()) { $fieldKey = array_pop(array_keys($check)); $i = count($params['fields']) + 1; $j = count($params['fields']); foreach ($params['fields'] as $fieldname) { if (empty($model->data[$model->alias][$fieldname])) { $i--; } else { if (!Validation::minLength($model->data[$model->alias][$fieldname], $params['minLength']) or !Validation::maxLength($model->data[$model->alias][$fieldname], $params['maxLength'])) { $j--; } } } if (empty($model->data[$model->alias][$fieldKey])) { $i--; } if (Validation::minLength($model->data[$model->alias][$fieldKey], $params['minLength'])) { $j--; } if ($i === 0 or $j === 0) { return false; } return true; }
/** * checkRange * * @param array $question question * @param string $answer answer value * @return array error message */ public function checkRange($question, $answer) { $errors = array(); // 現在の機能は数値型か文字列型のみ if ($question['question_type_option'] == QuestionnairesComponent::TYPE_OPTION_NUMERIC) { if ($question['is_range'] == QuestionnairesComponent::USES_USE) { if (!Validation::range($answer, intval($question['min']), intval($question['max']))) { $errors[] = sprintf(__d('questionnaires', 'Please enter the answer between %s and %s.', $question['min'], $question['max'])); } } } else { if ($question['is_range'] == QuestionnairesComponent::USES_USE) { if (!Validation::minLength($answer, intval($question['min'])) || !Validation::maxLength($answer, intval($question['max']))) { $errors[] = sprintf(__d('questionnaires', 'Please enter the answer between %s letters and %s letters.', $question['min'], $question['max'])); } } } return $errors; }