2017-12-23 40 views
-2

초보자입니다. 어제 Symfony의 tools : doctrine : crud와 같은 도구를 테스트했습니다. 나는 이제 내가 수동으로 훨씬 쉽게 할 수있는 많은 것들을 보았다. 나는 공식 문서 일부 자습서를 읽는 시간을 보내고 있지만, 내 의문에 대한 정확한 답변을 찾을 수없는왜 createForm에 TaskType이 필요합니까?

$editForm = $this->createForm('AppBundle\Form\TaskType', $task); 

: 분석은 내가 찾은 코드를 생성 한 후 케이스입니다. 왜이 부분이 필요합니까 : AppBundle\Form\TaskType? 무엇이 포함되어야합니까? 양식을 작성하는 TaskType 파일로 이동할 수 있습니다.

$builder->add('name')->add('datetime'); 

그러나 분리 된 파일 만 만들면별로 유용하지 않습니다. TaskType 파일 사용을 피하는 방법이 있습니까? 작업 엔티티 편집 양식을이 방법으로 실행하려고 시도했습니다.

$editForm = $this->createForm($task); 

그러나 잘못된 방식으로 진행됩니다. 감사 루카스

편집 # 1 ----- 컨트롤러 작업 엔티티

/** 
* Displays a form to edit an existing task entity. 
* 
* @Route("/{id}/edit", name="task_edit") 
* @Method({"GET", "POST"}) 
*/ 
public function editAction(Request $request, Task $task) 
{ 
    $deleteForm = $this->createDeleteForm($task); 
    $editForm = $this->createForm('AppBundle\Form\TaskType', $task); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $this->getDoctrine()->getManager()->flush(); 

     return $this->redirectToRoute('task_edit', array('id' => $task->getId())); 
    } 

    return $this->render('task/edit.html.twig', array(
     'task' => $task, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

에 대한 editAction 및 TaskType

class TaskType extends AbstractType 
{ 
    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('name')->add('datetime'); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Task' 
     )); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getBlockPrefix() 
    { 
     return 'appbundle_task'; 
    } 


} 
+0

게시 형태와 전체 컨트롤러를 작성의 내용이, 당신보다 동일한 버전을 사용하지 않는 한 내 경우에는 내가 있었다 작업 당 양식 메서드 생성 – albert

+0

코드 추가, 버전 3.3.10 – Lukaszy

+3

Symfony에 오신 것을 환영합니다. 가장 좋은 방법은 문서의 일부 [양식 예] (https://symfony.com/doc/current/forms.html)를 통해 작업하는 것일 수 있습니다. 의사 버전 번호 (오른쪽 위)가 Symfony 버전과 일치하는지 확인하십시오. 양식에 대한 기본적인 이해가 끝나면 진부한 것들이 더 의미가 있습니다. – Cerad

답변

1

이 당신을 호출하는 컨트롤러의 방법입니다.

프레임 워크 컨트롤러는 여러 심포니 서비스의 외관입니다. 그 중 하나가 FormFactory 서비스입니다.

는 양식을 만들려면 다음이 필요합니다

  1. 양식 유형 (필수)
  2. 데이터 (선택 사항)
  3. 가 이
  4. 양식 옵션 (선택 사항)

의 CreateForm()가 구현된다 상위 클래스에 있으므로 모든 종류의 양식 및 구현에 일반적입니다. CRUD를 생성 할 때

심포니 \ 번들 \ FrameworkBundle \ 컨트롤러 \ 컨트롤러

/** 
    * Creates and returns a Form instance from the type of the form. 
    * 
    * @param string|FormTypeInterface $type The built type of the form 
    * @param mixed     $data The initial data for the form 
    * @param array     $options Options for the form 
    * 
    * @return Form 
    */ 
    public function createForm($type, $data = null, array $options = array()) 
    { 
     return $this->container->get('form.factory')->create($type, $data, $options); 
    } 
+0

감사. 이 정의를 어디서 발견 했습니까? 나는 https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php#L304 만 찾았지만 코멘트는 덜 유용하다. – Lukaszy

+0

저는 지금 열려있는 프로젝트의 Symfony 2.7에 있습니다. – albert

+0

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/FormFactoryInterface.php# 양식 팩토리 인터페이스를 확인하십시오. L32 – albert