本文整理汇总了PHP中Illuminate\Validation\Factory::extend方法的典型用法代码示例。如果您正苦于以下问题:PHP Factory::extend方法的具体用法?PHP Factory::extend怎么用?PHP Factory::extend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Validation\Factory
的用法示例。
在下文中一共展示了Factory::extend方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setUp
public function setUp()
{
parent::setUp();
$this->translator = new Translator(new \Illuminate\Translation\FileLoader(new \Illuminate\Filesystem\Filesystem(), __DIR__ . '/../sample-lang'), 'en');
$this->translator->setLocale('en');
$this->factory = new Factory($this->translator);
$validator = new JalaliValidator();
$this->factory->extend('jalali', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateJalali($attribute, $value, $parameter);
});
$this->factory->extend('jalali_after', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateAfter($attribute, $value, $parameter);
});
$this->factory->extend('jalali_before', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateBefore($attribute, $value, $parameter);
});
$this->factory->replacer('jalali', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceJalali($message, $attribute, $rule, $parameter);
});
$this->factory->replacer('jalali_after', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceAfterOrBefore($message, $attribute, $rule, $parameter);
});
$this->factory->replacer('jalali_before', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceAfterOrBefore($message, $attribute, $rule, $parameter);
});
}
示例2: extendValidator
/**
* Extend validator.
*
* @param string $name
* @param string $class
* @param \Closure|null $replacer
*/
private function extendValidator($name, $class, Closure $replacer = null)
{
$this->validator->extend($name, $class . '@validate' . studly_case($name));
if (!is_null($replacer)) {
$this->validator->replacer($name, $replacer);
}
}
示例3: __construct
/**
* Create a new FormRequest instance.
*
* @param \Illuminate\Validation\Factory $factory
* @return void
*/
public function __construct(ValidatonFactory $factory)
{
$factory->extend('valid_token', function ($attribute, $value, $parameters, $validator) {
$secret = Crypt::decrypt($this->user->google2fa_secret);
return Google2FA::verifyKey($secret, $value);
}, 'Not a valid token');
$factory->extend('used_token', function ($attribute, $value, $parameters, $validator) {
$key = $this->user->id . ':' . $value;
return !Cache::has($key);
}, 'Cannot resue token');
}
示例4: __construct
public function __construct(Factory $factory)
{
//Custom Validator Rut
$factory->extend('old_password', function ($attribute, $value, $parameters) {
$id = $this->id;
$usuario = Usuario::find($id);
return Hash::check($value, $usuario->password);
}, 'La Constraseña actual no corresponde');
$factory->extend('rut_valid', function ($attribute, $value, $parameters) {
return Rut::check($value);
}, 'El campo Rut no tiene un formato válido');
}
示例5: __construct
/**
* Overwrite the parent constructor to define a new validator.
*
* @param Factory $factory
* @return void
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function __construct(Factory $factory)
{
$factory->extend('channel', function ($attribute, $value, $parameters) {
$first_character = substr($value, 0, 1);
return ($first_character === '#' || $first_character === '@') && strlen($value) > 1;
});
}
示例6: __construct
/**
* Overwrite the parent constructor to define a new validator.
*
* @param Factory $factory
* @return void
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function __construct(Factory $factory)
{
$factory->extend('repository', function ($attribute, $value, $parameters) {
if (preg_match('/^(ssh|git|https?):\\/\\//', $value)) {
// Plain old git repo
return true;
}
if (preg_match('/^(.*)@(.*):(.*)\\/(.*)\\.git/', $value)) {
// Gitlab
return true;
}
/*
TODO: improve these regexs, using the following stolen from PHPCI (sorry Dan!)
'ssh': /git\@github\.com\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'git': /git\:\/\/github.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/github\.com\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
*/
if (preg_match('/^[a-zA-Z0-9_\\-]+\\/[a-zA-Z0-9_\\-\\.]+$/', $value)) {
// Github
return true;
}
/*
'ssh': /git\@bitbucket\.org\:([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'http': /https\:\/\/[a-zA-Z0-9_\-]+\@bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)\.git/,
'anon': /https\:\/\/bitbucket.org\/([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)(\.git)?/
*/
if (preg_match('/^[a-zA-Z0-9_\\-]+\\/[a-zA-Z0-9_\\-\\.]+$/', $value)) {
// Bitbucket
return true;
}
return false;
});
}
示例7: __construct
public function __construct(Factory $factory)
{
//Custom Validator Rut
$factory->extend('rut_valid', function ($attribute, $value, $parameters) {
return Rut::check($value);
}, 'El campo Rut no tiene un formato válido');
}
示例8: boot
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(Validator $validator)
{
$rules = new Rules();
collect(get_class_methods($rules))->each(function ($method) use($validator) {
$name = snake_case(preg_replace('/^validate/i', null, $method));
$validator->extend($name, sprintf('%s@%s', Rules::class, $method));
});
}
示例9: extending
/**
* add new rule to validation factory
*
* @return void
*/
private function extending()
{
$this->factory->extend('has_parent', function ($attribute, $value) {
if ($attribute == 'object' && $value->getDepth() > 1 && $value->getParent() === null) {
return false;
}
return true;
});
}
示例10: initializeValidator
function initializeValidator()
{
$validator = new Validator($this->translator, new Container());
$validator->extend('phone', function ($attribute, $value, $parameters, $validator) {
preg_match('/\\d+/', $value, $matches);
if (strlen($matches[0]) == 10) {
return true;
}
});
return $validator;
}
示例11: registerValidators
/**
* Register field's custom validators.
*
* @param Factory $factory
* @param FormBuilder $builder
* @param FieldType $fieldType
*/
protected function registerValidators(Factory $factory, FormBuilder $builder, FieldType $fieldType)
{
foreach ($fieldType->getValidators() as $rule => $validator) {
$handler = array_get($validator, 'handler');
if (is_string($handler) && !str_contains($handler, '@')) {
$handler .= '@handle';
}
$factory->extend($rule, function ($attribute, $value, $parameters, Validator $validator) use($handler, $builder) {
return $this->container->call($handler, compact('attribute', 'value', 'parameters', 'builder', 'validator'));
}, array_get($validator, 'message'));
}
}
示例12: registerJDateTimeRules
/**
* @param JalaliValidator $validator
*/
private function registerJDateTimeRules(JalaliValidator $validator)
{
$this->factory->extend('jdatetime', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateJDateTime($attribute, $value, $parameter);
});
$this->factory->extend('jdatetime_after', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateJDateTimeAfter($attribute, $value, $parameter);
});
$this->factory->extend('jdatetime_before', function ($attribute, $value, $parameter) use($validator) {
return $validator->validateJDateTimeBefore($attribute, $value, $parameter);
});
$this->factory->replacer('jdatetime', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceJalali($message, $attribute, $rule, $parameter);
});
$this->factory->replacer('jdatetime_after', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceAfterOrBefore($message, $attribute, $rule, $parameter);
});
$this->factory->replacer('jdatetime_before', function ($message, $attribute, $rule, $parameter) use($validator) {
return $validator->replaceAfterOrBefore($message, $attribute, $rule, $parameter);
});
}
示例13: __construct
/**
* Overwrite the parent constructor to define a new validator.
*
* @param Factory $factory
* @return void
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function __construct(Factory $factory)
{
$factory->extend('host', function ($attribute, $value, $parameters) {
if (filter_var($value, FILTER_VALIDATE_IP)) {
return true;
}
if (filter_var(gethostbyname($value), FILTER_VALIDATE_IP)) {
return true;
}
return false;
});
}
示例14: validate
/**
* @param array $input
* @param array $rules
* @return array
*/
public function validate(array $input, array $rules)
{
$validatorFactory = new IlluminateValidationFactory(new SymfonyTranslator('en'));
// enabling rule default, which will be used not to validate, but to set default value for optional parameter
$validatorFactory->extend('default', function ($attribute, $value, $parameters) {
return count($parameters) == 1 && !empty($parameters[0]);
});
// create replacers for custom messages
//$validatorFactory->replacer('default', function ($message, $attribute, $rule, $parameters) {
// $replacement = !empty($parameters[0]) ? $replacement[0] : 'MISSING';
// return str_replace(':defaultValue', $replacement, $message);
//});
$validator = $validatorFactory->make($input, $rules, $this->validationMessages);
$validationErrors = $validator->errors()->getMessages();
reset($validationErrors);
return $validationErrors;
}
示例15: testMakeMethodCreatesValidValidator
public function testMakeMethodCreatesValidValidator()
{
$translator = m::mock('Symfony\\Component\\Translation\\TranslatorInterface');
$factory = new Factory($translator);
$validator = $factory->make(array('foo' => 'bar'), array('baz' => 'boom'));
$this->assertEquals($translator, $validator->getTranslator());
$this->assertEquals(array('foo' => 'bar'), $validator->getData());
$this->assertEquals(array('baz' => array('boom')), $validator->getRules());
$presence = m::mock('Illuminate\\Validation\\PresenceVerifierInterface');
$factory->extend('foo', function () {
});
$factory->setPresenceVerifier($presence);
$validator = $factory->make(array(), array());
$this->assertEquals(array('foo' => function () {
}), $validator->getExtensions());
$this->assertEquals($presence, $validator->getPresenceVerifier());
}