How to set default value for form field in Symfony2?

后端 未结 22 1771
暖寄归人
暖寄归人 2020-11-27 13:23

Is there an easy way to set a default value for text form field?

22条回答
  •  时光说笑
    2020-11-27 13:41

    Often, for init default values of form i use fixtures. Of cause this way is not easiest, but very comfortable.

    Example:

    class LoadSurgeonPlanData implements FixtureInterface
    {
        public function load(ObjectManager $manager)
        {
            $surgeonPlan = new SurgeonPlan();
    
            $surgeonPlan->setName('Free trial');
            $surgeonPlan->setPrice(0);
            $surgeonPlan->setDelayWorkHours(0);
            $surgeonPlan->setSlug('free');
    
            $manager->persist($surgeonPlan);
            $manager->flush();        
        }   
    }
    

    Yet, symfony type field have the option data.

    Example

    $builder->add('token', 'hidden', array(
        'data' => 'abcdef',
    ));
    

提交回复
热议问题