2014-09-19 1 views
0

OptionsResolverInterface을 사용하여 양식에 일부 추가 매개 변수를 전달합니다. 이 폼의 코드입니다buildForm에 대한 선택적 매개 변수 설정

class OrdersType extends AbstractType { 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     if ($options['curr_action'] !== NULL) 
     { 
      $builder 
        ->add('status', 'choice', array(
         'choices' => array("Pendiente", "Leido"), 
         'required' => TRUE, 
         'label' => FALSE, 
         'mapped' => FALSE 
        )) 
        ->add('invoice_no', 'text', array(
         'required' => TRUE, 
         'label' => FALSE, 
         'trim' => TRUE 
        )) 
        ->add('shipment_no', 'text', array(
         'required' => TRUE, 
         'label' => FALSE, 
         'trim' => TRUE 
      )); 
     } 

     if ($options['register_type'] == "natural") 
     { 
      $builder->add('person', new NaturalPersonType(), array('label' => FALSE)); 
     } 
     elseif ($options['register_type'] == "legal") 
     { 
      $builder->add('person', new LegalPersonType(), array('label' => FALSE)); 
     } 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setRequired(array(
      'register_type' 
     )); 

     $resolver->setOptional(array(
      'curr_action' 
     )); 

     $resolver->setDefaults(array(
      'data_class' => 'Tanane\FrontendBundle\Entity\Orders', 
      'render_fieldset' => FALSE, 
      'show_legend' => FALSE, 
      'intention' => 'orders_form' 
     )); 
    } 

    public function getName() 
    { 
     return 'orders'; 
    } 

} 

그리고 이것은 내가 컨트롤러에있는 양식 구축 '하는 방법입니다

$order = new Orders(); 
$orderForm = $this->createForm(new OrdersType(), $order, array('action' => $this->generateUrl('save_order'), 'register_type' => $type)); 

을하지만이 오류 받고 있어요 :

Notice: Undefined index: curr_action in /var/www/html/tanane/src/Tanane/FrontendBundle/Form/Type/OrdersType.php line 95

왜 ? 은이 코드 집합으로 $options 형태로 선택 사항입니까?

$resolver->setOptional(array(
    'curr_action' 
)); 
+0

이 함수가 도움이된다면 변수를 전달하는 구문을 사용합니다.이 함수가 도움이된다면 __construct ($ options = array()) { $ this-> options = $ options; }''form form에'$ orderForm = $ this-> createForm (새 OrdersType ($ options) ....')을 작성하십시오. –

+0

$ orderForm = $ this-> createForm (새 OrdersType(), $ order, array ('action'=> $ this-> generateUorder ('save_order'), 'register_type'=> $ type)); 여기에서 curr_action을 변경하거나 OrdersType을 변경하십시오. 그리고 당신은 @NawfalSerrar 생성자 제안을 따를 수 있습니다. – herr

답변

1

정확히. PHP는 알려지지 않은 배열 키에 접근 할 때 NOTICE을 발생시킵니다.

** 1) 교체 :

제대로 이것을 처리하기 위해, 당신은이 개 솔루션이 if ($options['curr_action'] !== NULL)

if (array_key_exists('curr_action', $options) && $options['curr_action'] !== NULL)

약간 복잡하지만 작동과를 ...

2) 또 다른 해결책은 기본값을 정의하는 것입니다.

$resolver->setDefaults(array(
     'data_class' => 'Tanane\FrontendBundle\Entity\Orders', 
     'render_fieldset' => FALSE, 
     'show_legend' => FALSE, 
     'intention' => 'orders_form', 
     'curr_action' => NULL // <--- THIS 
    ));