set a default value in a form

后端 未结 2 1424
情歌与酒
情歌与酒 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条回答
  • 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-21 16:00

    use 'empty_data' instead of 'data'

    $builder->add('points', 'hidden', array(
            'empty_data' => 5000));
    
    0 讨论(0)
提交回复
热议问题