Php 在Symfony中,有条件地要求包含表单的字段

Php 在Symfony中,有条件地要求包含表单的字段,php,forms,validation,symfony,Php,Forms,Validation,Symfony,我有一个表单类型类(我们称之为“ClientType”),它包括另一个表单类型类(“PhoneType”)。PhoneType是ClientType的一部分,如下所示: /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder

我有一个表单类型类(我们称之为“ClientType”),它包括另一个表单类型类(“PhoneType”)。PhoneType是ClientType的一部分,如下所示:

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
            ->add('name')
            ->add('phoneVoice', new PhoneType);
}
我想将其设置为在至少填写一个PhoneType字段时应用PhoneType的必填字段。例如,电话类型可能包括国家代码、号码和分机号码。如果填写了这些字段中的任何一个,我希望PhoneType的必填字段(国家代码和号码)是必填的

我如何设置这个?提前谢谢

编辑-在表单中添加类型类

ClientType

<?php

namespace AppName\ClientBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use AppName\ClientBundle\Form\Type\PhoneType;
use Doctrine\ORM\EntityManager;

class ClientType extends AbstractType {

   /**
     * @var EntityManager
     */
    protected $em;

    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('name')
                ->add('phoneVoice', new PhoneType, ['required' => true]);
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppName\ClientBundle\Entity\Client',
            'cascade_validation' => true,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'client';
    }

}
<?php

namespace AppName\ClientBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class PhoneType extends AbstractType
{

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('countryCode', new PhoneCountryCodeType, [
                    'preferred_choices' => ['1'],
                    'empty_value' => '1'
                ])
                ->add('number', 'number', [
                    'required' => true,
                    'max_length' => 10
                ])
                ->add('extension', 'number')
                ->add('isPrimary');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppName\ClientBundle\Entity\PhoneRecord'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'phone';
    }

}

如我所见,您的PhoneType映射到PhoneRecord实体,因此您可以使用custom

您的案例示例:

//AppName\ClientBundle\Entity\PhoneRecord.php

public function isCreditsLegal()
{
    if ($this->countryCode || $this->number || $this->extension) {
         return $this->countryCode && $this->number;
    }
    return true;
}


#validation.yml

AppName\ClientBundle\Entity\PhoneRecord:
    getters:
        creditsLegal:
            - "True": { message: "Invalid credits" }

如我所见,您的PhoneType映射到PhoneRecord实体,所以您可以使用custom

您的案例示例:

//AppName\ClientBundle\Entity\PhoneRecord.php

public function isCreditsLegal()
{
    if ($this->countryCode || $this->number || $this->extension) {
         return $this->countryCode && $this->number;
    }
    return true;
}


#validation.yml

AppName\ClientBundle\Entity\PhoneRecord:
    getters:
        creditsLegal:
            - "True": { message: "Invalid credits" }

到目前为止你试过什么?请向我们展示您正在使用的代码,以便我们帮助您。添加在表单类型类中,谢谢!到目前为止你试过什么?请向我们展示您正在使用的代码,以便我们帮助您。添加在表单类型类中,谢谢!