2012-04-06 5 views
1

좋아, 솔직하게. symfony가 핵심 파일을 가지고 결정한 의사 결정 중 일부는 마음에 들지 않으므로 그들을 덮어 쓰려고합니다. FormView 부모가있는 경우 예를 들어 코어 심포니 2 구성 요소 덮어 쓰기

Symfony/Component/Form/Extension/Core/Type/FieldType.php

메신저

렌더링됩니다 이름을 변경하려고 그들이 멋진 문자열 형식화하는 작업을 수행하기 때문에 ...

임 그냥 그렇게 $fullName 그것을 만들려고 노력하고 $id은 모두 $form->getName()입니다.

public function buildView(FormView $view, FormInterface $form) 
    { 
     $name = $form->getName(); 

     if ($view->hasParent()) { 
      $parentId = $view->getParent()->get('id'); 
      $parentFullName = $view->getParent()->get('full_name'); 

      // Custom Logic 
      //$id = sprintf('%s_%s', $parentId, $name); 
      //$fullName = sprintf('%s[%s]', $parentFullName, $name); 
      $id = $form->getName(); 
      $fullName = $form->getName(); 
     } else { 
      $id = $name; 
      $fullName = $name; 
     } 

     $types = array(); 
     foreach ($form->getTypes() as $type) { 
      $types[] = $type->getName(); 
     } 

     $view 
      ->set('form', $view) 
      ->set('id', $id) 
      ->set('name', $name) 
      ->set('full_name', $fullName) 
      ->set('errors', $form->getErrors()) 
      ->set('value', $form->getClientData()) 
      ->set('read_only', $form->isReadOnly()) 
      ->set('required', $form->isRequired()) 
      ->set('max_length', $form->getAttribute('max_length')) 
      ->set('pattern', $form->getAttribute('pattern')) 
      ->set('size', null) 
      ->set('label', $form->getAttribute('label')) 
      ->set('multipart', false) 
      ->set('attr', $form->getAttribute('attr')) 
      ->set('types', $types) 
     ; 
    } 

답변

0

도우미 기능을 내장했습니다.

public function fixForm(\Symfony\Component\Form\FormView $form) 
{ 
    foreach($form as &$child) 
    { 
     $name = $child->get('full_name'); 
     $id = $child->get('id'); 
     $matches = array(); 
     preg_match('/^(?<form>.+)\[(?<ele>.+)\]/', $name, $matches); 
     if(isset($matches['ele'])) 
      $child->set('full_name', $matches['ele']); 
     $matches = array(); 
     preg_match('/^(?<first>.+)_(?<second>.+)/', $id, $matches); 
     if(isset($matches['second'])) 
      $child->set('id', $matches['second']); 
    } 
    return $form; 
} 

및 그 작동. 양식을 수정해야 할 때 이것을 호출하십시오. 불행히도 모든 변경 사항을 모든 필드 유형에 적용해야합니다.

+0

왜 downvote? – Ascherer

1

여러분이 시도 할 수있는 것은 자신 만의 양식 유형을 만드는 것입니다.

"TestBundle/Form/Type"과 같은 깨끗한 구조의 Bundle에 자신의 양식 유형을 입력 할 수 있습니다.

이 필드 유형에서는 필요한 사항을 변경할 수 있습니다. 여기

How can I make a custom field type in symfony2?

사용자 정의 필드 형식을 만들기 위해 뜨거운 보여줍니다 helpfull 게시물입니다.

짧은 힌트와 좋은 해결책을 찾을 수 있고 그것이 작동하고 어떻게 해결했는지 알려줄 수 있습니다.

+0

이름을 변경하기 위해 모든 필드 유형을 다시 작성하지 마십시오. – Ascherer

+0

Twig에서이 문제를 해결할 생각이 있습니까? 양식을 사용자 지정하고 서식 파일의 필드를 변경하는 몇 가지 방법이 있습니다. http://symfony.com/doc/current/cookbook/form/form_customization.html – Stony

+0

내가 위에서 말했듯이, 나뭇 가지에서 그 일을하면 모든 form_widget과 그 것을 피하려고 노력하고 있습니다. – Ascherer