2

我在遵循 Symfony 食谱以添加/删除表单集合时遇到了问题。请参阅:http ://symfony.com/doc/current/cookbook/form/form_collections.html

现在,由于某种原因,如果我动态添加表单行但不填写任何字段,我会收到以下错误:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Project::addTask() must be an instance of Task, null given in D:\web_workspace\wedding\src\testapp.php line 82

我希望人们能够在表单中有空白行,而这些空白行会被忽略。例如,如果您单击“添加任务”几次,但没有填写最后一行,则表单仍应提交,并且应忽略最后一行。

我创建了一个非常简单的 Silex 演示,它只适合几个文件。我将在这里突出显示,但完整的示例在这里,只需通过 composer 添加 Silex 即可运行:https ://gist.github.com/mattsnowboard/7065865

我有以下模型(只是Project其中有描述和Tasks 集合)

class Task
{
    protected $name;

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
}

class Project
{
    protected $description;

    protected $tasks;

    public function __construct()
    {
        $this->tasks = array();
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }

    public function getTasks()
    {
        return $this->tasks;
    }
    public function addTask(Task $task)
    {
        if (!is_null($task) && !in_array($task, $this->tasks)) {
            $this->tasks[] = $task;
        }
        return $this;
    }
    public function removeTask(Task $task)
    {
        if (!is_null($task)) {
            $this->tasks = array_diff($this->tasks, array($task));
        }
        return $this;
    }
}

我的表格如下

class TaskType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text', array(
            'required' => false
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '\Task',
        ));
    }

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

class ProjectType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('description', 'text', array())
            ->add('tasks', 'collection', array(
                'type' => new TaskType(),
                'allow_add'    => true,
                'by_reference' => false,
                'allow_delete' => true,
                'required' => false
            ))
            ->add('submit', 'submit')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '\Project',
        ));
    }

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

控制器将一些测试数据添加到表单中,并在提交时打印数据:

$app->match('/', function (Request $request) use ($app) {
    $project = new Project();

    // dummy code
    $task1 = new Task();
    $task1->setName('A Task');
    $project->addTask($task1);
    // end dummy code

    $form = $app['form.factory']->create(new ProjectType(), $project);

    $form->handleRequest($request);

    if ($form->isValid()) {
        $data = $form->getData();
        $debug = print_r($data, true);
        echo $debug;
    }

    return $app['twig']->render('test.html.twig', array(
        'form' => $form->createView(),
    ));
})
->method('GET|POST');

视图主要是这个,以及食谱中的 javascript 示例

{{ form_start(form) }}

    {{ form_row(form.description) }}

    <h3>Tags</h3>
    <ul class="tasks" data-prototype="{{ form_widget(form.tasks.vars.prototype)|e }}">
        {# iterate over each existing tag and render its only field: name #}
        {% for task in form.tasks %}
            <li>{{ form_row(task.name) }}</li>
        {% endfor %}
    </ul>

    {{ form_widget(form.submit) }}
{{ form_end(form) }}

我只是错过了什么吗?这是一个错误吗?我觉得这个非常接近食谱的例子应该很容易工作......

更新:删除"allow_deleted"实际上并不能解决此问题。

4

1 回答 1

5

对不起,我想通了,collection类型不应该有'required' => false

    $builder->add('description', 'text', array())
        ->add('tasks', 'collection', array(
            'type' => new TaskType(),
            'allow_add'    => true,
            'by_reference' => false,
            'allow_delete' => true,
            'required' => false
        ))

应该

    $builder->add('description', 'text', array())
        ->add('tasks', 'collection', array(
            'type' => new TaskType(),
            'allow_add'    => true,
            'by_reference' => false,
            'allow_delete' => true
        ))
于 2013-10-20T07:42:45.337 回答