我有一个项目,我使用Composer,我导入很多东西...我需要我的索引(项目的根)中的autoload.php和istead Slim,Mongo,Twig工作得非常好.但是,当我打电话给一类尊重/验证时,它不起作用; 如果我只是使用Respect/Validation,则错误是:
Class 'Respect\Validation\Validator' not found in (path of file when i need it).
如果我在这里也要求autoload.php错误是:
**Warning**: require_once(vendor/autoload.php): failed to open stream: No such file or directory in (path of file when i need it)
**Fatal error**: require_once(): Failed opening required 'vendor/autoload.php' (include_path='.;C:\xampp\php\PEAR') in (path of file when i need it)
如何使用Respect Validation获得多个自定义错误消息.
我有一些输入,我想针对多个验证器进行验证.我希望每次验证都有自定义错误消息.
这是我试过的:
try {
Respect\Validation\Validator::create()
->key('foo',
v::length(20)->setName('bar')->setTemplate('Custom length message.')
->alnum()->setName('baz')->setTemplate('Custom alnum message.')
)
->assert([
'foo' => 'Hello, world!',
]);
} catch (Respect\Validation\Exceptions\ValidationException $exception) {
$errors = $exception->findMessages([
'bar',
'baz',
]);
var_dump($errors);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
array (size=2)
'bar' => string '' (length=0)
'baz' => string 'Custom alnum message.' (length=21)
Run Code Online (Sandbox Code Playgroud)
我希望它输出两个自定义错误消息.
Idealy我可以获得1个输入的消息数组,如:
var_dump($exception->findMessages(['foo']));
Run Code Online (Sandbox Code Playgroud)
会给我:
array (size=1)
'foo' =>
array (size=2)
0 => string 'Custom length message.' (length=22)
1 => string 'Custom alnum message.' (length=21)
Run Code Online (Sandbox Code Playgroud)
这个问题看起来像是杂草.
我想在 PHP 中使用尊重/验证库。我知道如何使用它,但目前我在一个德语项目中使用它,当然,我也想要德语的错误消息。
对于语言翻译,文档中有一个部分,但我并没有真正理解它,我还没有找到任何答案。
他们正在谈论应该处理消息翻译的翻译器。作为第二个参数,他们给出了“gettext”,但我不知道这应该是什么以及它应该如何处理翻译。
任何人都可以解释我这是如何工作的吗?
我试图使用ValidationErrorsMiddleware.php类作为中间件,所以我将以下代码添加到我的bootstrap/app.php:
$app->add(new App\Middleware\ValidationErrorsMiddleware($container));
Run Code Online (Sandbox Code Playgroud)
将上面的代码添加到我的app.php后,我收到以下错误:
Fatal error: Uncaught exception 'RuntimeException' with message 'Unexpected data in output buffer. Maybe you have characters before an opening <?php tag?' in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
RuntimeException: Unexpected data in output buffer. Maybe you have characters before an opening <?php tag? in C:\wamp64\www\authentication\vendor\slim\slim\Slim\App.php on line 552
Run Code Online (Sandbox Code Playgroud)
为了以防万一,任何人都需要查看我的类和app.php的代码,我已将它们包括在这里
ValidationErrorsMiddleware.php
<?php
namespace App\Middleware;
class ValidationErrorsMiddleware extends Middleware {
public function __invoke($request, $response, $next) {
var_dump('middleware');
$response = $next($request, $response);
return $response;
}
}
Run Code Online (Sandbox Code Playgroud)
Middleware.php
<?php
namespace App\Middleware;
class …Run Code Online (Sandbox Code Playgroud) 如何为我的验证的特定键获取自定义消息?
例如这个:
try {
Respect\Validation\Validator::create()
->key('foo', v::length(20))
->key('bar', v::max(5))
->assert([
'foo' => 'Hello, world!',
'bar' => 30,
]);
} catch (Respect\Validation\Exceptions\ValidationException $exception) {
$errors = $exception->findMessages([
'key' => 'My custom message',
]);
var_dump($errors, $exception->getFullMessage());
}
Run Code Online (Sandbox Code Playgroud)
返回这个:
array (size=1)
'key' => string 'My custom message' (length=17)
\-These rules must pass for "Array"
|-My custom message
| \-These rules must pass for "Hello, world!"
| \-"Hello, world!" must have a length greater than 20
\-Key bar must be valid on bar
\-These …Run Code Online (Sandbox Code Playgroud) 很棒的Respect Validation 库附带了许多内置验证器,例如 string()、alpha() 等。我想将自定义验证器添加到库中,例如,我希望能够执行以下操作:
Validator::myCustomValidator()->assert( $input );
Run Code Online (Sandbox Code Playgroud)
我刚刚发现它并不是很复杂,但我必须查看库的源代码才能找到答案,所以我在这里发布这个自我回答的问题以供将来参考。
我在我的 php 应用程序“ https://respect-validation.readthedocs.io/en/1.1/rules/Each/ ”中使用来自此来源的尊重内置验证。我通过 ajax 调用将数据传递给 php 应用程序代码,例如
print_r($itemsArray);
Array
(
[0] => Array
(
[itemName] => apple
)
[1] => Array
(
[itemName] => banana
)
[2] => Array
(
[itemName] => cherry
)
)
Run Code Online (Sandbox Code Playgroud)
我已将验证应用于 php 代码端的以下字段。
$fullName = $_REQUEST['fullName'];
$email = $_REQUEST['email'];
$phoneNumber = $_REQUEST['phoneNumber'];
$age = $_REQUEST['age'];
$itemsArray = $_REQUEST['itemsArray'];
try
{
v::key('fullName', v::notEmpty()->setTemplate("Full Name: Required field"))
->key('fullName', v::alpha()->setTemplate("Full Name: Alphabets only"))
....
....
->key('itemsArray', v::arrayVal()->each(v::alpha())->setTemplate("Item must contain Alphabets"))
->assert(['fullName' => $fullName, 'email' => $email, …Run Code Online (Sandbox Code Playgroud) php ×7
validation ×4
composer-php ×1
dependencies ×1
php-5.6 ×1
php-7.0 ×1
slim ×1
slim-3 ×1
translation ×1