У меня есть метод FormType.Symfony2 Условная проверка
<?php
class PostType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('text')
->add('paymentMethod', 'choice', array(
'required' => true,
'choices' => array(
'cach' => 'Cach',
'check'=> 'Check'
)
))
->add('checkId', 'integer', array(
'required' => true
))
->add('submit', 'submit');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'data_class' => 'AppBundle\Entity\Post',
]);
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_post';
}
}
?>
Поле checkId отображает только тогда, когда выбор «проверка» выбран с полем PAYMENTMETHOD.
Вот JavaScript:
PaymentMethod = {
hideCheckId: function(){
$('.check-id').hide();
},
showCheckId: function(){
$('.check-id').fadeIn();
},
whenPaymentMethodChange: function(){
$('#appbundle_post_paymentMethod').on('change', function(){
var method = this.value ;
if(method == 'check'){
PaymentMethod.showCheckId();
}else{
PaymentMethod.hideCheckId();
}
})
}
};
PaymentMethod.hideCheckId();
PaymentMethod.whenPaymentMethodChange();
Я хотел бы поле «checkId» быть обязательно TRUE, только если выбран вариант проверки. Как я могу это сделать?
Использование обратного вызова (http://symfony.com/doc/current/reference/constraints/Callback.html) или выражение (http://symfony.com/doc/current/reference/constraints/Expression .html) validator –