如果在表单中选择了某个选项,我目前无法关闭某些验证限制。此选项与模型无关,不使用data_class
选项设置。
我的表单包含两个嵌入的地址表单,与一个发票地址和一个货件地址完全相同。如果选择了替代选项,我想通过验证制作所需的装运地址表。如果没有选择替代选项,则出货地址表格不需要验证,需要单独保留。
CustomerCheckoutForm
class CustomerCheckoutForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', 'hidden')
->add('firstName', 'text')
->add('nameAdditions', 'text')
->add('lastName', 'text')
->add('gender', 'choice', array('expanded' => true, 'choices' => array('m' => 'M', 'f' => 'F')))
->add('birthDate', 'date', array(
'required' => true,
'input' => 'string',
'widget' => 'choice',
'years' => range(DATE_CURRENT_YEAR - 80, DATE_CURRENT_YEAR - 18),
'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day')
))
->add('invoiceAddress', new AddressForm($options['countryMapper']), array(
'label' => false,
'required' => true,
))
->add('alternative_shipment_address', 'choice', array(
'expanded' => true,
'choices' => array(0 => 'Delivery on the current address', 1 => 'Alternative shipping address'),
'mapped' => false,
'required' => true,
'label' => 'Delivery address',
'data' => 0,
))
->add('shipmentAddress', new AddressForm($options['countryMapper']), array(
'label' => false,
'required' => false,
))
->add('Continue', 'submit');
$this->registerListeners($builder);
}
private function registerListeners(FormBuilderInterface $builder)
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event) {
$customer = $event->getData();
$form = $event->getForm();
if (!$customer || $customer->getId() === null) {
$form->add('password', 'password');
}
});
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EHV\SharedBundle\Model\Customer',
'countryMapper' => null,
'validation_groups' => array(
'Default',
'checkout',
),
));
}
public function getName()
{
return 'customerCheckout';
}
}
为AddressForm
class AddressForm extends AbstractType
{
private $countryMapper;
public function __construct(CountryMapper $countryMapper)
{
$this->countryMapper = $countryMapper;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new CountryToIdTransformer($this->countryMapper);
$countries = $this->countryMapper->getAll();
$data[''] = 'Make a choice...';
/** @var Country $country */
foreach ($countries as $country) {
$data[$country->getId()] = $country->getName();
}
$builder
->add('id', 'hidden')
->add('streetName', 'text')
->add('streetNumber', 'text')
->add('streetNumberAddition', 'text')
->add('postalCode', 'text')
->add('city', 'text')
->add(
$builder->create('country', 'choice', array('choices' => $data))
->addModelTransformer($transformer)
);
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'EHV\SharedBundle\Model\Address',
));
}
public function getName()
{
return 'address';
}
}
两个模型都通过函数设置了自己的约束:
public static function loadValidatorMetadata(ClassMetaData $metadata) {}
答案 0 :(得分:1)
Checkout回调约束http://symfony.com/doc/current/reference/constraints/Callback.html
如果需要,您可以阅读地址的值并上升错误。
答案 1 :(得分:0)
我设法使用Form event listener
与Hpatoio回答的Constraint Callback
,详情如下。
<强> CustomerCheckoutForm 强>
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event) {
$form = $event->getForm();
$customer = $event->getData();
if ($form->get('alternative_shipment_address')->getData() === 0) {
$customer->setShipmentAddress(null);
}
});
<强> CustomerConstraint 强>
$callback = function($customer) use ($metadata) {
$metadata->addPropertyConstraint('invoiceAddress', new Valid());
/** @var Customer $customer */
if ($customer->getShipmentAddress() !== null) {
$metadata->addPropertyConstraint('shipmentAddress', new Valid());
}
};
$metadata->addConstraint(new Callback($callback));
致 Hpatoio 以表明正确的方向。