Php 无法从Symfony Form 4.2获取错误消息
我以独立方式使用Symfony Form 4.2。我有一个表单元素,在HTML文件中只有几个字段。为了在PHP中进行验证,我使用Symfony form 4.2在PHP中复制了该表单,如下所示:Php 无法从Symfony Form 4.2获取错误消息,php,forms,symfony,Php,Forms,Symfony,我以独立方式使用Symfony Form 4.2。我有一个表单元素,在HTML文件中只有几个字段。为了在PHP中进行验证,我使用Symfony form 4.2在PHP中复制了该表单,如下所示: $formFactory = Forms::createFormFactoryBuilder() ->addExtension(new ValidatorExtension($validator)) ->getFormFactory(); $form = $
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$form = $formFactory->createBuilder()
->add('pk_firstname', TextType::class, [
'constraints' => [new NotBlank(),
new Length([
'min' => 2,
'max' => 50,
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters']
)],
])
->add('pk_lastname', TextType::class, [
'constraints' => [new NotBlank(),
new Length([
'min' => 2,
'max' => 50,
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters']
)],
])
->add('pk_emailaddress', EmailType::class, [
'constraints' => [new NotBlank(), new Email(
[
'message' => 'The email "{{ value }}" is not a valid email.',
'mode' => 'strict',
]
)],
])
->add('pk_phonenumber', TelType::class, [
'constraints' => [],
])
->add('pk_message', TextareaType::class, [
'constraints' => [new NotBlank()],
])
->getForm();
$request = Request::createFromGlobals();
$submitData = array("pk_firstname" => $request->request->get('pk_firstname'),
"pk_lastname" => $request->request->get('pk_lastname'),
"pk_emailaddress" => $request->request->get('pk_emailaddress'),
"pk_phonenumber" => $request->request->get('pk_phonenumber'),
"pk_message" => $request->request->get('pk_message'));
$form->submit($submitData);
在前端提交表单时,我使用JavaScript防止默认行为,并使用提交的数据向PHP文件发出AJAX请求,其中使用Symfony表单验证提交数据的正确性
然后,我手动提交如下表格:
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$form = $formFactory->createBuilder()
->add('pk_firstname', TextType::class, [
'constraints' => [new NotBlank(),
new Length([
'min' => 2,
'max' => 50,
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters']
)],
])
->add('pk_lastname', TextType::class, [
'constraints' => [new NotBlank(),
new Length([
'min' => 2,
'max' => 50,
'minMessage' => 'Your first name must be at least {{ limit }} characters long',
'maxMessage' => 'Your first name cannot be longer than {{ limit }} characters']
)],
])
->add('pk_emailaddress', EmailType::class, [
'constraints' => [new NotBlank(), new Email(
[
'message' => 'The email "{{ value }}" is not a valid email.',
'mode' => 'strict',
]
)],
])
->add('pk_phonenumber', TelType::class, [
'constraints' => [],
])
->add('pk_message', TextareaType::class, [
'constraints' => [new NotBlank()],
])
->getForm();
$request = Request::createFromGlobals();
$submitData = array("pk_firstname" => $request->request->get('pk_firstname'),
"pk_lastname" => $request->request->get('pk_lastname'),
"pk_emailaddress" => $request->request->get('pk_emailaddress'),
"pk_phonenumber" => $request->request->get('pk_phonenumber'),
"pk_message" => $request->request->get('pk_message'));
$form->submit($submitData);
这里的问题是,在提交无效数据时,isValid()
methodon$form
对象返回表单无效,但getErrors()
方法返回空数组
这里有什么我做错的吗
我想获取违反约束和约束的字段,以便将这些错误消息传递给JavaScript,以便在前端显示。如果我理解正确,您只想序列化表单对象,对吗 因此,您可以执行以下操作:
public function getErrorMessages(FormInterface $form)
{
$errors = array();
foreach ($form->getErrors(true, true) as $error) {
$propertyPath = str_replace('data.', '', $error->getCause()->getPropertyPath());
$errors[$propertyPath] = $error->getMessage();
}
return $errors;
}
您必须将表单对象传递给此方法,例如,$serializedErrors=getErrorMessages($form)