如果我尝试从Symfony中的$event
到addEventListener
转换数据,-'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: {}"
如果我尝试在本地主机上调试,我会发现$表单对象不是空的。