php 向 Symfony 2 表单元素添加错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12419551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add error to Symfony 2 form element
提问by Alex Pliutau
I check some validation in my controller. And I want to add error to specific element of my form on failure. My form:
我在控制器中检查了一些验证。我想在失败时向表单的特定元素添加错误。我的表格:
use Symfony\Component\Form\FormError;
// ...
$config = new Config();
$form = $this->createFormBuilder($config)
->add('googleMapKey', 'text', array('label' => 'Google Map key'))
->add('locationRadius', 'text', array('label' => 'Location radius (km)'))
->getForm();
// ...
$form->addError(new FormError('error message'));
addError() method adds error to form, not to element. How can I add an error to locationRadius element?
addError() 方法将错误添加到表单,而不是元素。如何向 locationRadius 元素添加错误?
回答by Mun Mun Das
You can do
你可以做
$form->get('locationRadius')->addError(new FormError('error message'));
As form elements are also of FormInterfacetype.
因为表单元素也是FormInterface类型的。
回答by Jekis
OK guys, I have another way. It is more complex and only for specific cases.
好吧,伙计们,我有另一种方式。它更复杂,仅适用于特定情况。
My case:
我的情况:
I have a form and after submit I post data to the API server. And errors I got from the API server as well.
我有一个表单,提交后我将数据发布到 API 服务器。还有我从 API 服务器得到的错误。
API server error format is:
API 服务器错误格式为:
array(
'message' => 'Invalid postal code',
'propertyPath' => 'businessAdress.postalCode',
)
My goal is to get flexible solution. Lets set the error for the corresponding field.
我的目标是获得灵活的解决方案。让我们为相应的字段设置错误。
$vm = new ViolationMapper();
// Format should be: children[businessAddress].children[postalCode]
$error['propertyPath'] = 'children['. str_replace('.', '].children[', $error['propertyPath']) .']';
// Convert error to violation.
$constraint = new ConstraintViolation(
$error['message'], $error['message'], array(), '', $error['propertyPath'], null
);
$vm->mapViolation($constraint, $form);
That's it!
就是这样!
NOTE!addError()method bypasses error_mappingoption.
笔记!addError()方法绕过error_mapping选项。
My form (Address form embedded in the Company form):
我的表格(公司表格中嵌入的地址表格):
Company
公司
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
class Company extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', 'text',
array(
'label' => 'Company name',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('businessAddress', new Address(),
array(
'label' => 'Business address',
)
)
->add('update', 'submit', array(
'label' => 'Update',
)
)
;
}
public function getName()
{
return null;
}
}
Address
地址
<?php
namespace Acme\DemoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints;
class Address extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('postalCode', 'text',
array(
'label' => 'Postal code',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('town', 'text',
array(
'label' => 'Town',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
->add('country', 'choice',
array(
'label' => 'Country',
'choices' => $this->getCountries(),
'empty_value' => 'Select...',
'constraints' => array(
new Constraints\NotBlank()
),
)
)
;
}
public function getName()
{
return null;
}
}

