2013-03-19 2 views
0

내 양식은 이지만 '연장'하는 방법을 알 수 없습니다.양식 클래스를 확장 할 수 있습니까?

예를 들어, CustomerType 양식 클래스와 EmailType 양식 클래스가 있습니다. 내가 직접 내 CustomerType

$builder->add('emails', 'collection', array(
    'type'   => new EmailType(), 
    'allow_add' => true, 
    'by_reference' => false 
)); 

EmailType를 추가 할 수 있지만 내 CustomerType 폼 클래스는 고객 정보를 포함하도록, 컨트롤러에서이 작업을 수행하는 것을 선호 것입니다. 때로는 내 사용자가 Customer 세부 정보 만 편집 할 수 있기를 바라며, 다른 사용자는 Customer 세부 정보와 해당 고객과 관련된 객체 인 Email을 모두 편집 할 수 있기를 원하기 때문에 더 모듈화되고 재사용 가능하다고 생각합니다. (예를 들어, 고객의 작업 공정을 볼 때 첫 번째 경우, 새 고객을 만들 때 두 번째 경우).

이것이 가능합니까? 나는 내 컨트롤러에

$form = $this->createForm(new CustomerType(), $customer); 
$form->add('emails', 'collection', ...) 

의 줄을 따라 생각하고 있습니다.

+0

왜 재사용 할 수 있습니까? 동일한 양식이 두 개 있고 이메일 입력란의 이름을 바꾸려면 두 곳에서해야합니다. 당신의 목표는 무엇입니까? – meze

+0

나는 약간의 질문에서 명확히했다. –

답변

0

양식을 만들 때 양식에 모음을 포함할지 여부를 알려주는 옵션 (예 : 'with_email_edition')을 양식에 전달할 수 있습니다. 형태

$form = $this->createForm(new CustomerType(), $customerEntity, array('with_email_edition' => true)); 

:

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
       'with_email_edition' => null, 
      )) 
      ->setAllowedValues(array(
       'with_email_edition' => array(true, false), 
      )); 
} 

를 다음 "buildForm"에서 확인하십시오

는 그냥 setDefaultOptions에 옵션을 추가 컨트롤러에

이 옵션의 값을 입력하고 그 필드를 기반으로 필드를 추가하십시오 :

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    if(array_key_exists("with_email_edition", $options) && $options['with_email_edition'] === true) 
    { 
      //Add a specific field with $builder->add for example 
    } 
} 
관련 문제