2012-10-23 32 views
0

奧姆/ YAML

Subject: 
    type: entity 
    repositoryClass: My\SampleBundle\Repository\SubjectRepository 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     body: 
      type: text 
    oneToMany: 
     choices: 
      targetEntity: Choice 
      mappedBy: subject 
      cascade: ["persist", "remove"] 
Choice: 
    type: entity 
    repositoryClass: My\SampleBundle\Repository\ChoiceRepository 
    id: 
     id: 
      type: integer 
      generator: { strategy: AUTO } 
    fields: 
     choice: 
      type: text 
    manyToOne: 
     subject: 
      targetEntity: Subject 
      inversedBy: choices 
      joinColumn: 
       name: subject_id 
       referencedColumnName: id 

查看

<form action="{{ path('path_create or path_edit', {'id': id}) }}" method="post" {{ form_enctype(form) }}> 

    // ... 

    <ul id="choice-list" data-prototype="{{ form_widget(form.choices.vars.prototype.choice) | e }}"> 
     {% for choice in form.choices %} 
      <li> 
       <div class="errors"> 
        {{ form_errors(choice.choice) }} 
       </div> 
       {{ form_widget(choice.choice) }} 
      </li> 
     {% endfor %} 
    </ul> 

    <!-- ## The field can be added by JavaScript. --> 
    <span class="add-another-choice">Add another choice</span> 
    {{ form_rest(form) }} 
    <input type="submit" class="_button" /> 
</form> 

表/類型

class SubjectType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('body', 'textarea'); 
     $builder->add('choices', 'collection', array(
      'type' => new ChoiceType(), 
      'options' => array(
       'required' => false 
      ), 
      'allow_add' => true, 
      'by_reference' => false, 
      'allow_delete' => true, 
      'prototype' => true 
     )); 
    } 

    // ... 
} 

class ChoiceType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('choice', 'textarea'); 
    } 

    // ... 
} 

驗證

Subject: 
    properties: 
     body: 
      - NotBlank: ~ 
      - MinLength: 8 
     choices: 
      - Valid: ~ 
Choice: 
    properties: 
     choice: 
      - NotBlank: ~ 

控制器

public function createAction(Request $request, $id) 
{ 
    $em = $this->get('doctrine')->getEntityManager(); 

    $subject = new Subject(); 
    $subject->getChoices()->add(new Choice()); 

    $form = $this->createForm(new SubjectType(), $subject); 

    if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request);   //<= error on this point. Why? 
               // if there is the empty field. 
     if ($form->isValid()) { 
      return // ... 
     } 
    } 

    return $this->render('MySampleBundle:Subject:create.html.twig', array(
     'form' => $form->createView(), 
    )); 
} 

我嘗試Symfony2的集合形式。
但是,當字段增加的Javascript,它會失敗,如果有空字段。
當字段增加時,它在執行驗證之前就會變成錯誤。 (Catchable致命錯誤:))

我該如何設法實現這一目標?


編輯:

如果該值被設置,兩個或更多個登記將是成功的。
如果有一個項目沒有設置值,兩個或更多的註冊將會出錯。
bindRequest函數失敗。
如果只有空值的部分添加實體,則它將成功。
但是,它似乎是不正確的方式。

回答

1

從選擇表單字段中刪除required = false或將模式選擇字段添加nullable = true

+0

Jeez!我忽視了一個人想要完成的事情。 對不起,我問了一個基本問題。我正在妄圖嘗試。 我很欣賞你的考慮。謝謝你的建議。 – JIGEN

相關問題