本文整理汇总了PHP中lithium\util\Validator::check方法的典型用法代码示例。如果您正苦于以下问题:PHP Validator::check方法的具体用法?PHP Validator::check怎么用?PHP Validator::check使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lithium\util\Validator
的用法示例。
在下文中一共展示了Validator::check方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testConditionalInRange
public function testConditionalInRange()
{
$values = array('height' => 195, 'gender' => 'M');
$rules = array('height' => array(array('conditionalInRange', 'message' => 'Incorrect value for given condition!', 'upper' => 201, 'lower' => 184, 'conditions' => array(array('gender', '===', 'M'))), array('conditionalInRange', 'message' => 'Incorrect value for given condition!', 'upper' => 186, 'lower' => 167, 'conditions' => array(array('gender', '===', 'W')))));
$validate = Validator::check($values, $rules);
$this->assertTrue(empty($validate));
$values['gender'] = 'W';
$validate = Validator::check($values, $rules);
$this->assertTrue(!empty($validate));
$values['height'] = 171;
$validate = Validator::check($values, $rules);
$this->assertTrue(empty($validate));
$values['height'] = 165;
$validate = Validator::check($values, $rules);
$this->assertTrue(!empty($validate));
}
示例2: testNestedFields
/**
* Tests validating nested fields using dot-separated paths.
*/
public function testNestedFields()
{
$rules = array('id' => array('numeric', 'message' => 'Bad ID'), 'profile.name' => "Can't be empty", 'profile.email' => array('email', 'message' => 'Must be a valid email'));
$data = array('id' => 1, 'profile' => array('email' => 'foo'));
$result = Validator::check($data, $rules);
$expected = array('profile.name' => array("Can't be empty"), 'profile.email' => array('Must be a valid email'));
$this->assertEqual($expected, $result);
$data = array('id' => '.', 'profile' => array('email' => 'foo@bar.com', 'name' => 'Bob'));
$result = Validator::check($data, $rules);
$this->assertEqual(array('id' => array('Bad ID')), $result);
}
示例3: validate
public static function validate($object, $object_hash = array())
{
$errors = null;
if (!in_array(spl_object_hash($object), $object_hash)) {
$object_hash[] = spl_object_hash($object);
}
$reflection = new \ReflectionClass($object);
$classname = $reflection->getName();
$validations = $object->validations;
if (!empty($validations)) {
$unique = $equalWith = array();
foreach ($validations as $field => $rules) {
foreach ($rules as $key => $value) {
if ($value[0] == "unique") {
$unique[] = array($field, "message" => $value['message']);
if (count($validations[$field]) == 1) {
unset($validations[$field]);
} else {
unset($validations[$field][$key]);
}
} else {
if ($value[0] == "equalWith") {
$equalWith[] = array($field, "message" => $value['message'], "with" => $value['with']);
if (count($validations[$field]) == 1) {
unset($validations[$field]);
} else {
unset($validations[$field][$key]);
}
}
}
}
}
$errors = Validator::check(static::convertToArray($object), $validations);
/** Unique checking */
foreach ($unique as $key => $value) {
$result = $classname::getRepository()->findOneBy(array($value[0] => $object->{$value}[0]));
if (!empty($result)) {
$errors[$value[0]][] = $value["message"];
}
}
/** EqualWith checking */
foreach ($equalWith as $key => $value) {
if ($object->{$value}[0] != $object->{$value}['with']) {
$errors[$value[0]][] = $value["message"];
}
}
$reflection = new \ReflectionClass($object);
$properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
try {
foreach ($properties as $property) {
$property->setAccessible(true);
if (ModelAnnotation::match($property, array('ManyToMany', 'OneToMany'))) {
$relation = $property->getValue($object);
foreach ($relation as $item) {
if (!in_array(spl_object_hash($item), $object_hash)) {
if (!ModelValidator::isValid($item, $object_hash)) {
$errors[$property->getName()][] = $item->getErrors();
}
}
}
} elseif (ModelAnnotation::match($property, array('ManyToOne', 'OneToOne'))) {
if ($item = $property->getValue($object)) {
if (!in_array(spl_object_hash($item), $object_hash)) {
if (!ModelValidator::isValid($item, $object_hash)) {
$errors[$property->getName()][] = $item->getErrors();
}
}
}
}
}
} catch (\ReflectionException $e) {
die($e->getTrace() . "-" . $e->getMessage());
continue;
}
}
ModelValidator::$_errors[spl_object_hash($object)] = $errors;
return $errors;
}
示例4: testValidationWithContextData
public function testValidationWithContextData()
{
Validator::add('someModelRule', function ($value, $format, $options) {
return $value == 'Title' && $options['values']['body'] == 'Body';
});
$result = Validator::check(array('title' => 'Title', 'body' => 'Body'), array('title' => array('someModelRule')));
$this->assertIdentical(array(), $result);
$result = Validator::check(array('title' => 'Title', 'body' => 'Not Body'), array('title' => array('someModelRule')));
$this->assertIdentical(array('title' => array(0)), $result);
}
示例5: testCheckMultiplePasses
public function testCheckMultiplePasses()
{
$rules = array('title' => 'please enter a title', 'email' => array(array('notEmpty', 'message' => 'email is empty'), array('email', 'message' => 'email is not valid')));
$data = array('title' => 'new title', 'email' => 'something@test.com');
$result = Validator::check($data, $rules);
$this->assertTrue(empty($result));
$expected = array();
$this->assertEqual($expected, $result);
}
示例6: upload
/**
* Call same method on adapter object
*
* @filter This method can be filtered.
*
* @param string $location
* @param array $file
* @param string $destination
* @param array $options
* @return boolean
*/
public static function upload($location, array $file, $destination = null, array $options = array())
{
$options += array('validates' => array());
if (!($adapter = static::_getAdapter($location))) {
return false;
}
static::$uploadErrors = array();
if (!empty($options['validates']) && is_array($options['validates'])) {
$errors = Validator::check($file, $options['validates']);
if (!empty($errors)) {
static::$uploadErrors = $errors;
return false;
}
}
$params = compact('file', 'destination', 'options');
$callback = function ($self, $params) use($adapter) {
return $adapter->upload($params['file'], $params['destination'], $params['options']);
};
return static::_filter(__FUNCTION__, $params, $callback);
}