Forms Symfony 3:类属性上带有CONTAint的嵌入表单的错误消息
如果一个类具有另一个类的属性,则可以使用Forms Symfony 3:类属性上带有CONTAint的嵌入表单的错误消息,forms,symfony,validation,Forms,Symfony,Validation,如果一个类具有另一个类的属性,则可以使用Valid()注释级联验证,如中所示 我为该示例构建了一个嵌入式表单,当我将不正确的数据放入Address类的表单字段时,它会正确地级联错误 但是,如果我将类Address的所有表单字段保留为空,则不会显示任何错误。看起来没问题。除了在address属性上指定Valid()之外,我还需要指定NotBlank()或NotNull。以下是完整的示例: // src/AppBundle/Entity/Address.php namespace AppBundle
Valid()
注释级联验证,如中所示
我为该示例构建了一个嵌入式表单,当我将不正确的数据放入Address
类的表单字段时,它会正确地级联错误
但是,如果我将类Address
的所有表单字段保留为空,则不会显示任何错误。看起来没问题。除了在address
属性上指定Valid()
之外,我还需要指定NotBlank()
或NotNull
。以下是完整的示例:
// src/AppBundle/Entity/Address.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Address
{
/**
* @Assert\NotBlank()
*/
protected $street;
/**
* @Assert\NotBlank
* @Assert\Length(max = 5)
*/
protected $zipCode;
}
// src/AppBundle/Entity/Author.php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
/**
* @Assert\NotBlank()
* @Assert\Length(min = 4)
*/
protected $firstName;
/**
* @Assert\NotBlank()
*/
protected $lastName;
/**
* @Assert\NotNull()
* @Assert\Valid()
*/
protected $address;
}
使用此代码,将地址的所有字段保留为空将导致表单提交无效。这就是我想要的
但是:提交后表单上不会显示错误消息。我认为这与以下事实有关:NotNull()
的错误消息与单个表单字段没有关联。如何显示错误
表格类型代码:
// src/AppBundle/Form/Type/AddressType.php
namespace AppBundle\Form\Type;
use AppBundle\Entity\Address;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('street', TextType::class)
->add('zipCode', TextType::class)
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Address::class,
));
}
}
// src/AppBundle/Form/Type/AuthorType.php
namespace AppBundle\Form\Type;
use AppBundle\Form\Type\AddressType;
use AppBundle\Entity\Author;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('address', AddressType::class)
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Author::class,
));
}
}
输出dump($form->getErrors(true,true))代码>:
AuthorController.php on line 33:
FormErrorIterator {#538 ▼
-form: Form {#363 ▶}
-errors: array:1 [▼
0 => FormError {#748 ▼
-message: "This value should not be null."
#messageTemplate: "This value should not be null."
#messageParameters: array:1 [▼
"{{ value }}" => "null"
]
#messagePluralization: null
-cause: ConstraintViolation {#682 ▼
-message: "This value should not be null."
-messageTemplate: "This value should not be null."
-parameters: array:1 [▶]
-plural: null
-root: Form {#363}
-propertyPath: "data.author.address"
-invalidValue: null
-constraint: NotNull {#674 ▶}
-code: "ad32d13f-c3d4-423b-909a-857b961eb720"
-cause: null
}
-origin: Form {#489 ▶}
}
]
}
尝试将错误\u冒泡设置为false
在控制器中,需要创建相关对象的实例。这样,对象就不为null,您可以对其进行字段验证
public function myAction()
{
$formObject = new Author();
$formObject->setAddress(new Address())
$this->createForm(AuthorType::class, $formObject)
请显示您的表单类型。您可能需要在AddressType
中设置add('address',AddressType::class,array('error\u bubbling'=>false)
。如果您使用自定义细枝表单呈现,您需要确保您具有{{form\u errors(address)}
以便呈现错误。您将如何处理转储($form->getErrors(true,true))
?@goto我将dump
的输出附加到我的问题中。这表明验证是有效的。这似乎只是如何显示消息的问题。尝试了(在两个可能的位置:地址类型
和AuthorType
)但没有效果。
public function myAction()
{
$formObject = new Author();
$formObject->setAddress(new Address())
$this->createForm(AuthorType::class, $formObject)