2016-07-08 1 views
0

다음 설정을 사용합니다. - 하루 엔티티 - 자체 필드가있는 요일 양식symfony : 나뭇 가지 서식 파일에 사용할 자식 양식의 사용자 지정 데이터 특성을 설정하십시오.

일 양식에 6 일 속성을 기반으로 계산 된 '합계'값을 추가하고 싶습니다. 이미 성공적으로 클래스를 만들었습니다. 따라서 주요 질문은 각 날 양식에 계산 된 데이터를 연결하는 방법입니다.

필자는 필요한 코드 만 아래에 표시하므로 뭔가를 놓친 경우 표시되지 않습니다.

DayEntity가 기본값이며 필드 묶음이 있습니다.

DayType 양식 : 나뭇 가지에서

class DayType extends AbstractType 
    { 
     public function buildForm(FormBuilderInterface $builder, array $options) 
     { 
      $builder 
       ->add('field'); 
     // etc, bunch of fields 

     // Here I want to add a calculated field. How to do it? 
     // The result is retrieved from my service, with a function: calculateTotal($day); 
     } 
    } 

I have already a service which does the necessary computation: 

public function calculateTotal(Day $day) 
{ 
    // Some magic here ;-) 

    return $returnDateInterval; 
} 

내가 좋아하는 뭔가를 좋아는 :

{{ dayform.vars.data.calculatedTotal|date('H:i') }} 
// or 
{{ dayform.calculatedTotal|date('H:i') }} 
+0

서비스를 formType에 옵션으로 전달할 수 있습니까? –

+0

의견을 보내 주셔서 감사합니다. 답변을 추가했습니다. – DelphiLynx

답변

0

나는 이미 .... 위대한 심포니 워드 프로세서 답을 발견! Defining your forms as services

내가 내 service.yml을 수정했다 않았다 그래서 : 당신이 볼 수 있듯이

app.subscriber.calculatetotal: 
    class: AppBundle\Form\EventListener\CalculateTotalFieldSubscriber 
    arguments: ["@app.manager.day"] 


app.form.day: 
    class: AppBundle\Form\Type\DayType 
    arguments: ["@app.subscriber.calculatetotal"] 
    tags: 
     - { name: form.type } 

나는 또한 나의 비 매핑 '총'필드를 변경 PRE_SUBMIT 및 POST_SET_DATA에서 수신 대기 양식의 EventListener를 추가했다. DayType.php 형태로

:

private $calculateTotalFieldSubscriber; 

public function __construct(CalculateTotalFieldSubscriber $calculateTotalFieldSubscriber) 
{ 
    $this->calculateTotalFieldSubscriber = $calculateTotalFieldSubscriber; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
    // I do not save this field to the database, so mapped is set to false 
    ->add('total', TimeType::class, array(
     'mapped' => false 
    )) 
    $builder->addEventSubscriber($this->calculateTotalFieldSubscriber); 
} 

calculateTotalFieldSubscriber은 두 사건을 듣고, 기본값입니다.