addEventListener中的对象不会转换为JSON

时间:2019-08-06 08:13:29

标签: php symfony

如果我尝试从Symfony中的$eventaddEventListener转换数据,-'json_encode'返回空对象:{}

/**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->add('login', TextType::class, array('label'=> false, 'attr' => array('class' => 'form-control', 'placeholder' => 'Login', 'data' => '')))
            ->add('password', PasswordType::class, array('label'=> false, 'required' => false, 'attr' => array('class' => 'form-control', 'placeholder' => 'Password')))
            ->add('save', SubmitType::class, array('label' => 'Enter'))
            ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
                $this->logToFile($event, 'PRE_SET_DATA');
            })
    }

    private function logToFile($event, $text){
        if ($event->getData()) {
            $data = $event->getData();
        } else {
            $data = 'none';
        }
        $form = $event->getForm();
        error_log(PHP_EOL.date('Y-m-d h:i:s' , time()).' TestData / buildForm / '.$text.': $data: '.json_encode($data).' / $form: '.json_encode($form), 3, "/var/tmp/log.log");
    }

结果: "PRE_SET_DATA: $user: \"none\" \/ $form: {}"

如果我尝试在本地主机上调试,我会发现$表单对象不是空的。

0 个答案:

没有答案