在symfony2 sonata admin bundle中设置默认值

时间:2021-04-29 20:16:20

how can i set default value in sonata admin bundle the data option is missing in configureFormFields method

如何在sonata admin bundle中设置默认值,configureFormFields方法中缺少data选项

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name', null, array('required' => true, 'data' => "my default value"))
    ;
}

how can use data attribute to set default value inside field ???

如何使用data属性在字段内设置默认值???

4 个解决方案

#1


43  

I presume you've probably already solved this by now, but as a reference to anyone else you can override the getNewInstance() method and set the default value on the object:

我认为你现在可能已经解决了这个问题,但作为对其他任何人的引用,你可以覆盖getNewInstance()方法并在对象上设置默认值:

public function getNewInstance()
{
    $instance = parent::getNewInstance();
    $instance->setName('my default value');

    return $instance;
}

#2


6  

you can also assign the default value to the property of the entity directly:

您还可以直接将默认值分配给实体的属性:

class TheEntity
{
    private $name = 'default name';
}

#3


5  

In addition to @RobMasters solution:

除了@RobMaster解决方案:

If you want to set a relation you can get a reference from the entitymanager (instead of the complete object):

如果要设置关系,可以从entitymanager(而不是完整对象)获取引用:

public function getNewInstance()
{
    $instance = parent::getNewInstance();

    if ($this->hasRequest()) {
        $branch = $this->getRequest()->get('branch', null);

        if ($branch !== null) {
            $entityManager = $this->getModelManager()->getEntityManager('MyBundle\Entity\Branch');
            $branchReference = $entityManager->getReference('MyBundle\Entity\Branch', $branch);

            $instance->setBranch($branchReference);
        }
    }
    return $instance;
}

I added the example to my blog: http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/

我在我的博客中添加了这个示例:http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/

#4


0  

For booleans, another option is to set a data value within the first array passed to your add method, inside of configureFormFields

对于布尔值,另一种选择是在configureFormFields内部的第一个传递给add方法的数组中设置一个数据值

So after some memtoring, my code (for a checkbox that I wanted to have checked by default) ended up looking something like this:

所以经过一些记忆之后,我的代码(对于我希望默认选中的复选框)最终看起来像这样:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('visible', null, ['label'=>'Visibility', 'data' => true ])
    ;
}

... which saved a few lines at the top of my file, since I could then get rid of the getNewInstance() definition.

...在我的文件顶部保存了几行,因为我可以摆脱getNewInstance()定义。

#1


43  

I presume you've probably already solved this by now, but as a reference to anyone else you can override the getNewInstance() method and set the default value on the object:

我认为你现在可能已经解决了这个问题,但作为对其他任何人的引用,你可以覆盖getNewInstance()方法并在对象上设置默认值:

public function getNewInstance()
{
    $instance = parent::getNewInstance();
    $instance->setName('my default value');

    return $instance;
}

#2


6  

you can also assign the default value to the property of the entity directly:

您还可以直接将默认值分配给实体的属性:

class TheEntity
{
    private $name = 'default name';
}

#3


5  

In addition to @RobMasters solution:

除了@RobMaster解决方案:

If you want to set a relation you can get a reference from the entitymanager (instead of the complete object):

如果要设置关系,可以从entitymanager(而不是完整对象)获取引用:

public function getNewInstance()
{
    $instance = parent::getNewInstance();

    if ($this->hasRequest()) {
        $branch = $this->getRequest()->get('branch', null);

        if ($branch !== null) {
            $entityManager = $this->getModelManager()->getEntityManager('MyBundle\Entity\Branch');
            $branchReference = $entityManager->getReference('MyBundle\Entity\Branch', $branch);

            $instance->setBranch($branchReference);
        }
    }
    return $instance;
}

I added the example to my blog: http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/

我在我的博客中添加了这个示例:http://blog.webdevilopers.net/populate-resp-set-default-values-on-form-resp-object-or-instance-in-sonataadminbundle/

#4


0  

For booleans, another option is to set a data value within the first array passed to your add method, inside of configureFormFields

对于布尔值,另一种选择是在configureFormFields内部的第一个传递给add方法的数组中设置一个数据值

So after some memtoring, my code (for a checkbox that I wanted to have checked by default) ended up looking something like this:

所以经过一些记忆之后,我的代码(对于我希望默认选中的复选框)最终看起来像这样:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('visible', null, ['label'=>'Visibility', 'data' => true ])
    ;
}

... which saved a few lines at the top of my file, since I could then get rid of the getNewInstance() definition.

...在我的文件顶部保存了几行,因为我可以摆脱getNewInstance()定义。