set a default value in a form

后端 未结 2 1441
情歌与酒
情歌与酒 2021-01-21 15:22

I would like set a value by default in my form.

I am doing this, but didn\'t work:

$builder->add(\'points\', \'hidden\', array(
            \'data\' =         


        
2条回答
  •  Happy的楠姐
    2021-01-21 15:51

    If you want to set something by default, set it right on the model object:

    $model = new Model;
    $model->setPoints(5000);
    
    $form = $this->createForm('type', $model);
    

    Or better yet, if it makes sense, set it right to the model's property or the constructor:

    class Model 
    {
        private $points = 5000;
    
        // or
        public function __construct()
        {
            $this->points = 5000;
        }
    }
    

提交回复
热议问题