php 如何在 Symfony2 中设置默认值,以便自动 CRUD 生成的表单不需要这些字段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7409615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:37:58  来源:igfitidea点击:

How to set a default value in Symfony2 so that automatic CRUD generated forms don't require those fields?

phpsymfonycrud

提问by Czechnology

As I've already found out, Doctrine2 "does not support to set the default values in columns through the “DEFAULT” keyword in SQL. ... you can just use your class properties as default values".

正如我已经发现的那样,Doctrine2“不支持通过 SQL 中的“DEFAULT”关键字在列中设置默认值。......你可以使用你的类属性作为默认值“。

class Product
{

// ...

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name = "";

    /**
     * @var string $sale
     *
     * @ORM\Column(name="sale", type="boolean")
     */
    private $sale = false;

But even when I do this, the generated CRUD forms still require me to fill out all forms. In case of boolean attributes this even means I can only set it to true (i.e. 1).

但即使我这样做,生成的 CRUD 表单仍然需要我填写所有表单。在布尔属性的情况下,这甚至意味着我只能将其设置为 true(即1)。

Am I doing something wrong?

难道我做错了什么?

(I know I can turn the validation off but I'd like a solution to the problem instead of just bypassing it)

(我知道我可以关闭验证,但我想要解决问题的方法而不是绕过它)

回答by Julien Ducro

Your boolean value need to have nullable set as true:

您的布尔值需要将 nullable 设置为 true:

/**
 * @var string $sale
 *
 * @ORM\Column(name="sale", type="boolean", nullable=true)
 */
private $sale = false;

回答by Freenando

In Object-oriented programming you should use constructor of the entity to set a default value for an attribute:

在面向对象的编程中,您应该使用实体的构造函数来为属性设置默认值:

public function __construct() {
    $this->sale = false;
}

回答by gilden

I haven't used the CRUD auto-generation tool, but I know that by default, each and every field is required. YOu must explicitly pass 'required' => falseas an option for your fields.

我没有使用过 CRUD 自动生成工具,但我知道默认情况下,每个字段都是必需的。您必须明确地将其'required' => false作为选项传递给您的字段。

This can be done in the form classes

这可以在表单类中完成

namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

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

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

The same can be achived in a Form class generated inside your controller

可以在控制器内部生成的 Form 类中实现相同的功能

namespace Acme\DemoBundle\Controller;

use Acme\DemoBundle\Entity\Foo;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function newAction(Request $request)
    {
        // ...    

        $form = $this->createFormBuilder($foo)
            ->add('field', 'text', array('required' => false)
            ->getForm();

        // ...

        return $this->render('AcmeDemoBundle:Default:new.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}

回答by Yassir Ennazk

You can also use the 'data' parameter like in :

您还可以使用“数据”参数,如:

->add('date', 'date', array(
                    'widget' => 'single_text',
                    'format' => 'dd/MM/yyyy',
                    'attr' => array('class' => 'datepicker'),
                    'data' => new \DateTime()
                ))

Here I have set a class to make a jQuery UI datepicker of the field using JavaScript. I also set the widget to a single_text so I won't get three select fields. And then I set the default data to the current DateTime()

在这里,我设置了一个类来使用 JavaScript 制作该字段的 jQuery UI 日期选择器。我还将小部件设置为 single_text 所以我不会得到三个选择字段。然后我将默认数据设置为当前 DateTime()

回答by Louwki

Or In annotations Use:

或在注释中使用:

options={"default":"foo"}

and not:

并不是:

options={"default"="foo"}

For instance:

例如:

/**
 * @ORM\Column(name="foo", type="smallint", options={"default":0})
 */
private $foo;