如何将数据注入symfony2中的表单

时间:2012-10-25 18:23:38

标签: forms symfony ckeditor

我在我的控制器处打电话:

控制器:

    if($appointment->getAnamnese() == NULL){
        $entity = new Anamnese($appointment);
        $form = $this->createForm(new AnamneseType(),null,array('history' => 'Digite o Historico aqui'));
    }else{ 
        $entity = $appointment->getAnamnese();
        $form = $this->createForm(new AnamneseType(), null, array('history' => $entity->getHistory()));
    }

AnamneseType:

    $builder->add('history', 'ckeditor', array(
        'data' => $options['history'], 'toolbar' => $toolbar));
}


public function getDefaultOptions(array $options)
{
    return array(
            'history' => "Digite o Historico aqui"
    );
}

我希望将这些历史信息注入我的表单,但它不能像我想象的那样工作,只需设置'数据'选项......

我该怎么做?

问题是在插入数据后,我无法将其恢复到表单中。

3 个答案:

答案 0 :(得分:3)

使用setData()功能设置数据:

例如:

$form = $this->createForm(new AnamneseType())->setData($entity);

或者甚至可能:

$form = $this->createForm(new AnamneseType(), $entity);

答案 1 :(得分:1)

你正在以错误的方式看待它。

你必须在这里使用选项。

创建表单:

$form = $this->createForm(new AnamneseType(), null, array('history' => $entity->getHistory()));

您的表单应如下所示:

public function buildForm(FormBuilder $builder, array $options){

    $toolbar = array(
        array(
            'name' => 'document',
            'items' => array('Source','-','DocProps','Preview','Print','-','Templates')
        ),
        array(
            'name' => 'clipboard',
            'items' => array('Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo')
        ),
        array(
            'name' => 'editing',
            'items' => array('Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt')
        ),

        array(
            'name' => 'basicstyles',
            'items' => array('Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat')
        ),
        '/',                
        array(
            'name' => 'paragraph',
            'items' => array('NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl')
        ),
        array(
            'name' => 'links',
            'items' => array('Link','Unlink','Anchor')
        ),
        array(
            'name' => 'insert',
            'items' => array('Image','Table','HorizontalRule','Smiley','SpecialChar','PageBreak')
        ),
        '/',
        array(
            'name' => 'styles',
            'items' => array('Styles','Format','Font','FontSize')
        ),
        array(
            'name' => 'colors',
            'items' => array('TextColor','BGColor')
        ),
        array(
            'name' => 'tools',
            'items' => array('Maximize', 'ShowBlocks','-','About')
        )
    );

    $builder->add('history', 'ckeditor', array( 'data' => $options['history'] , 'toolbar' => $toolbar));
}

...

public function getDefaultOptions(array $options)
{
    return array(
        'history' => "Digite o Historico aqui"
    );
}

答案 2 :(得分:1)

好吧,看起来表单是在第二个条件中生成的,$entity->getHistory()返回null。

编辑您的控制器代码,如下所示

$historyValue = 'Digite o Historico aqui'; // Default history value
if($appointment->getAnamnese()){ 
    $entity = $appointment->getAnamnese();
    // Checks whether the history is not empty (null or equals '' in this case)
    if (!empty($entity->getHistory())) { 
        $historyValue = $entity->getHistory();
    }

}

$form = $this->createForm(new AnamneseType(),null,array('history' => $historyValue));

我强烈建议您阅读官方文档。

Symfony Forms


表单数据应该从控制器传递。

替换

$form = $this->createForm(new AnamneseType($entity->getHistory()));

使用

$form = $this->createForm(new AnamneseType(), array(
    'history' => null === $entity->getHistory()
               ? 'Digite o Historico aqui'
               : $entity->getHistory,
));

从表单类中删除构造函数并替换

if($this->history != NULL){
    $builder->add('history', 'ckeditor', array( 'data' => $this->history , 'toolbar' =>       $toolbar));
}else{
    $builder->add('history', 'ckeditor', array( 'data' => "Digite o Historico aqui" , 'toolbar' => $toolbar));
}

使用

$builder->add('history', 'ckeditor', array('toolbar' => $toolbar));

如果要将数据映射回实体,请选中Forms official documentation

<强> UPD:

要从历史记录字段向模板传递某些值,请编辑其定义,如:

$builder->add('history', 'ckeditor', array(
    'attr' => array(
        'toolbar' => $toolbar,
    ),
));

您可以通过以下方式访问toolbar选项:

{{ form.history.get('attr').toolbar }}

有更好的解决方案:Create Custom Form Type

相关问题