gpt4 book ai didi

php - Symfony2 表单集合allow_add 和allow_delete 空错误(Silex)

转载 作者:行者123 更新时间:2023-12-02 21:46:13 25 4
gpt4 key购买 nike

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

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

ContextErrorException:可捕获的 fatal error :传递给 Project::addTask() 的参数 1 必须是 Task 的实例,D:\web_workspace\wedding\src\testapp.php 第 82 行中给出的 null

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

我创建了一个非常简单的 Silex 演示,仅包含几个文件。我将在这里强调它,但完整的示例在这里,只需通过 Composer 添加 Silex 即可运行:https://gist.github.com/mattsnowboard/7065865

我有以下模型(只有 Project ,其中包含描述和 Task 集合)

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';
}
}

Controller 向表单添加一些测试数据,并在提交时打印数据:

$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');

View 主要是这个,以及说明书中的 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”实际上并不能解决此问题。

最佳答案

抱歉,我发现,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
))

关于php - Symfony2 表单集合allow_add 和allow_delete 空错误(Silex),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19474872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com