本文整理汇总了PHP中Respect\Validation\Validator类的典型用法代码示例。如果您正苦于以下问题:PHP Validator类的具体用法?PHP Validator怎么用?PHP Validator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Validator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setValidationRules
protected function setValidationRules($validator)
{
$required = new Validator();
$required->notEmpty();
$validator->attribute('name', $required);
$validator->attribute('content', $required);
}
示例2: charsInMemory
/**
* Returns the number of characters in memory after parsing given string.
* @param string $string
* @return int
* @throws ParseException
*/
public static function charsInMemory($string)
{
if (!Validator::stringType()->regex('/".*"/')->validate($string)) {
throw new ParseException('String must be surrounded by double quotes (")');
}
return preg_match_all("/(\\\\\\\\|\\\\\"|\\\\x[[:xdigit:]]{2}|.)/", substr($string, 1, strlen($string) - 2));
}
示例3: __construct
public function __construct(Client $client, $key)
{
$this->client = $client;
$this->key = $key;
$baseUrl = $this->client->getConfig('base_uri');
Validator::notEmpty()->url()->endsWith('/')->setName("URL for NewRelic's Insights API must be valid and have a trailing slash")->assert($baseUrl);
}
示例4: __construct
/**
* Validator constructor.
* @param array $rules Array where each key corresponds to one attribute of the model and the value is a list of
* validation rules supported by Respect\Validation separated by the pipe ("|") character.
*/
public function __construct(array $rules)
{
foreach ($rules as $field => $rule) {
$this->addAttribute($field, $rule);
}
parent::__construct();
}
示例5: preprocessValue
protected function preprocessValue(&$uid)
{
if (!Validator::int($uid)) {
throw new InvalidArgumentException('uid', 'type_invalid');
}
$uid = (int) $uid;
}
示例6: validate
public function validate($data)
{
$validator = V::key('name', V::string()->length(0, 100), true)->key('email', V::email()->length(0, 200), true)->key('password', V::string()->length(0, 100), true);
try {
$validator->assert($data);
switch ($data['userable_type']) {
case 'Designer':
$this->designerCreationValidator->validate($data);
$data['userable_type'] = DesignerModel::class;
break;
case 'Administrator':
$this->adminCreationValidator->validate($data);
$data['userable_type'] = AdministratorModel::class;
break;
case 'Buyer':
$this->buyerCreationValidator->validate($data);
$data['userable_type'] = BuyerModel::class;
break;
default:
break;
}
} catch (AbstractNestedException $e) {
$errors = $e->findMessages(['email', 'length', 'in']);
throw new ValidationException('Could not create user.', $errors);
}
return true;
}
示例7: index
public function index($params)
{
Validator::notEmpty()->setName('name')->check($params['name']);
Validator::notEmpty()->setName('password')->check($params['password']);
$levels = Config::loadData('level');
$this->render('index.php', ['name' => $params['name']]);
}
示例8: __construct
/**
* @param string $category
* @param string|null $subCategory
* @throws InvalidArgumentException
* @throws UserException
*/
public function __construct($category, $subCategory = null)
{
if (!is_string($category)) {
throw new InvalidArgumentException('category', 'type_invalid');
}
if (!mb_check_encoding($category, 'UTF-8')) {
throw new InvalidArgumentException('category', 'encoding_invalid');
}
if (!Validator::length(VJ::TAG_MIN, VJ::TAG_MAX)->validate($category)) {
throw new UserException('Problem.Tag.invalid_length');
}
$keyword = KeywordFilter::isContainGeneric($category);
if ($keyword !== false) {
throw new UserException('Problem.Tag.name_forbid', ['keyword' => $keyword]);
}
if (!is_string($subCategory)) {
throw new InvalidArgumentException('subCategory', 'type_invalid');
}
if (!mb_check_encoding($subCategory, 'UTF-8')) {
throw new InvalidArgumentException('subCategory', 'encoding_invalid');
}
if (!Validator::length(VJ::TAG_MIN, VJ::TAG_MAX)->validate($subCategory)) {
throw new UserException('Problem.Tag.invalid_length');
}
$keyword = KeywordFilter::isContainGeneric($subCategory);
if ($keyword !== false) {
throw new UserException('Problem.Tag.name_forbid', ['keyword' => $keyword]);
}
$this->category = $category;
$this->subCategory = $subCategory;
}
示例9: __construct
public function __construct($webapiKey = null, $userLogin = null, $hashedPassword = null)
{
$this->properties['webapiKey'] = new Property(['validator' => function ($value) {
try {
Validator::string()->noWhitespace()->notEmpty()->length(4)->assert($value);
} catch (NestedValidationExceptionInterface $e) {
throw new ValidationException($e);
}
}]);
if (!is_null($webapiKey)) {
$this->properties['webapiKey']->set($webapiKey)->lock();
}
// --------------------
$this->properties['userLogin'] = new Property(['validator' => function ($value) {
try {
Validator::string()->noWhitespace()->notEmpty()->length(4)->assert($value);
} catch (NestedValidationExceptionInterface $e) {
throw new ValidationException($e);
}
}]);
if (!is_null($userLogin)) {
$this->properties['userLogin']->set($userLogin)->lock();
}
// ----------------------
$this->properties['hashedPassword'] = new Property(['validator' => function ($value) {
try {
Validator::string()->noWhitespace()->notEmpty()->regex('/^([A-Za-z0-9+\\/]{4})*([A-Za-z0-9+\\/]{4}|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{2}==)$/')->assert($value);
} catch (NestedValidationExceptionInterface $e) {
throw new ValidationException($e);
}
}]);
if (!is_null($hashedPassword)) {
$this->properties['hashedPassword']->set($hashedPassword)->lock();
}
}
示例10: validate
public function validate($prop, $label)
{
$value = $this->getValue($prop);
if (!v::string()->cnpj()->validate($value)) {
$this->addException("O preenchimento do campo {$label} está inválido");
}
}
示例11: validate
/**
* @param $cardNumber
* @return array
* @throws NumberValidateException
*/
public static function validate($cardNumber)
{
if (!v::numeric()->noWhitespace()->validate($cardNumber)) {
throw new NumberValidateException();
}
return ['valid' => true, 'type' => 'visa'];
}
示例12: setAlternativeEmail
public function setAlternativeEmail($value)
{
if (!v::email()->validate($value)) {
throw new FieldRequiredException("E-mail alternativo inválido");
}
$this->_email .= "," . $value;
}
示例13: isValid
public function isValid($validation_data)
{
$errors = [];
foreach ($validation_data as $name => $value) {
if (isset($_REQUEST[$name])) {
$rules = explode("|", $value);
foreach ($rules as $rule) {
$exploded = explode(":", $rule);
switch ($exploded[0]) {
case 'min':
$min = $exploded[1];
if (Valid::stringType()->length($min)->Validate($_REQUEST[$name]) == false) {
$errors[] = $name . " must be at least " . $min . " characters long";
}
break;
case 'email':
if (Valid::email()->Validate($_REQUEST[$name]) == false) {
$errors[] = $name . " must be a valid email ";
}
break;
case 'equalTo':
if (Valid::equals($_REQUEST[$name])->Validate($_REQUEST[$exploded[1]]) == false) {
$errors[] = "Values do not match";
}
break;
default:
//do nothing
}
}
} else {
$errors = "No value found";
}
}
return $errors;
}
示例14: _validate
private function _validate($param, $type)
{
$libConfig = $GLOBALS['app']->getConfiguration()->getRawConfiguration('library');
$minNameVal = $libConfig["product"]["minNameLength"];
$maxNameVal = $libConfig["product"]["maxNameLength"];
$minCodeVal = $libConfig["product"]["minCodeLength"];
$maxCodeVal = $libConfig["product"]["maxCodeLength"];
$validateName = v::alnum('-_')->length($minNameVal, $maxNameVal);
$validateCode = v::alnum('-_')->noWhitespace()->length($minCodeVal, $maxCodeVal);
$validateToken = v::alnum('-_');
if (strcmp($type, "name") === 0) {
$isValid = $validateName->validate($param);
if (!$isValid) {
throw new \InvalidArgumentException(\Akzo\Product\ErrorMessages::INVALID_PRODUCT_NAME, \Native5\Core\Http\StatusCodes::NOT_ACCEPTABLE);
}
} else {
if (strcmp($type, "code") === 0) {
$isValid = $validateCode->validate($param);
if (!$isValid) {
throw new \InvalidArgumentException(\Akzo\Product\ErrorMessages::INVALID_PRODUCT_CODE, \Native5\Core\Http\StatusCodes::NOT_ACCEPTABLE);
}
} else {
if (strcmp($type, "token") === 0) {
$isValid = $validateToken->validate($param);
if (!$isValid) {
throw new \InvalidArgumentException(\Akzo\Product\ErrorMessages::INVALID_TOKEN, \Native5\Core\Http\StatusCodes::NOT_ACCEPTABLE);
}
} else {
throw new \InvalidArgumentException('Invalid Validation Type', \Native5\Core\Http\StatusCodes::NOT_ACCEPTABLE);
}
}
}
}
示例15: validate
protected function validate()
{
$data = filter_input(INPUT_POST, $this->model);
$error = false;
foreach ($this->validation_rules as $field => $config) {
$validator = new v();
$validator->addRules($config['rules']);
if ($config['optional']) {
$this->validate[$field] = v::optional($validator);
} else {
$this->validate[$field] = $validator;
}
$error = $this->validate[$field]->validate($data[$field]);
}
return $error;
}