提交带有 rest 的表单时正在跳过验证 api
Validation is skipping when submit the form with rest api
我有这个表格:
class RegistrationFormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
]
])
->add('username')
->add('password')
;
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_user_register';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'allow_extra_fields' => true,
'csrf_protection' => false,
]);
}
}
控制器中我的api:
/**
* @Route("/api/register")
* @Method("POST")
* @param Request $request
*
* @return JsonResponse
*/
public function register(
UserService $userService,
Request $request
)
{
try {
return $userService->register(json_decode($request->getContent(), true));
} catch (\Exception $e) {
return $this->json([
'status' => Response::HTTP_INTERNAL_SERVER_ERROR,
'result' => $e->getMessage()
]);
}
}
还有我的服务功能:
public function register($formData)
{
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);
$form->submit($formData);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($user);
$this->entityManager->flush();
return new JsonResponse([
'status' => Response::HTTP_OK,
'result' => true
]);
}
return new JsonResponse([
'status' => Response::HTTP_BAD_REQUEST,
'result' => FormErrorFormatter::getErrorsFromForm($form)
]);
}
当我试图用
在邮递员中调用 api /api/register
{
"username": "test1",
"email": "test1",
"password": "123456"
}
我得到 200 code
,但通常应该删除错误,因为电子邮件无效,因为我在表单创建中输入字段电子邮件应采用电子邮件格式,即使我输入空字符串也是如此在电子邮件中,我收到 200 code
。所以似乎验证不起作用。
EmailType
,据我所知,没有默认约束。但是,您通过要求 NotBlank
来覆盖约束,这绝对与 Email
约束不同。表单确实将 type="email"
添加到 html, 浏览器 将强制执行(这在技术上是不可靠的,因为用户可以将它变成文本字段) .
解决方案可能是使用 Email
约束并将 required
属性 设置为 true
.
尝试:
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
new Email(),
]
])
并添加到您的实体中:
/**
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
protected $email;
我有这个表格:
class RegistrationFormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
]
])
->add('username')
->add('password')
;
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'app_user_register';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
'allow_extra_fields' => true,
'csrf_protection' => false,
]);
}
}
控制器中我的api:
/**
* @Route("/api/register")
* @Method("POST")
* @param Request $request
*
* @return JsonResponse
*/
public function register(
UserService $userService,
Request $request
)
{
try {
return $userService->register(json_decode($request->getContent(), true));
} catch (\Exception $e) {
return $this->json([
'status' => Response::HTTP_INTERNAL_SERVER_ERROR,
'result' => $e->getMessage()
]);
}
}
还有我的服务功能:
public function register($formData)
{
$user = new User();
$form = $this->formFactory->create(RegistrationFormType::class, $user);
$form->submit($formData);
if ($form->isSubmitted() && $form->isValid()) {
$this->entityManager->persist($user);
$this->entityManager->flush();
return new JsonResponse([
'status' => Response::HTTP_OK,
'result' => true
]);
}
return new JsonResponse([
'status' => Response::HTTP_BAD_REQUEST,
'result' => FormErrorFormatter::getErrorsFromForm($form)
]);
}
当我试图用
在邮递员中调用 api /api/register{
"username": "test1",
"email": "test1",
"password": "123456"
}
我得到 200 code
,但通常应该删除错误,因为电子邮件无效,因为我在表单创建中输入字段电子邮件应采用电子邮件格式,即使我输入空字符串也是如此在电子邮件中,我收到 200 code
。所以似乎验证不起作用。
EmailType
,据我所知,没有默认约束。但是,您通过要求 NotBlank
来覆盖约束,这绝对与 Email
约束不同。表单确实将 type="email"
添加到 html, 浏览器 将强制执行(这在技术上是不可靠的,因为用户可以将它变成文本字段) .
解决方案可能是使用 Email
约束并将 required
属性 设置为 true
.
尝试:
->add('email', EmailType::class, [
'constraints' => [
new NotBlank(),
new Email(),
]
])
并添加到您的实体中:
/**
* @Assert\Email(
* message = "The email '{{ value }}' is not a valid email.",
* checkMX = true
* )
*/
protected $email;