pawsitiwe / sulu-frontend-validation-bundle
用于 Sulu 表单前端验证的包
0.1.1
2024-07-28 15:33 UTC
Requires
- php: ^8.2
- sulu/form-bundle: ^2.0
- symfony/framework-bundle: ^6.0
README
安装
composer require pawsitiwe/sulu-frontend-validation-bundle
设置
服务注册
该扩展需要注册为 symfony 服务。
services: Pawsitiwe\Controller\ValidationController: arguments: $formBuilder: '@sulu_form.builder' tags: ['controller.service_arguments']
包注册
return [ Pawsitiwe\SuluFrontendValidationBundle::class => ['all' => true], ]
路由注册
sulu_frontend_validation: resource: '@SuluFrontendValidationBundle/Resources/config/routes.yaml' prefix: /
使用
路由 /validate-form-field 返回 JSON 格式的表单验证结果
示例响应
当表单有验证错误时,响应格式如下
{ "errors": { "email": [ "This value is not a valid email address." ], "lastName": [ "This value should not be blank." ] } }
错误对象中的每个键代表一个表单字段,值是该字段的错误消息数组。
验证成功
当表单有效时,响应将如下
{ "message": "The form is valid!" }
错误处理
如果请求中没有找到表单数据,则响应将如下
{ "message": "No form data found" }