我在应用程序上编辑配置文件时遇到问题,因为图像字段正在返回此错误:
类型为“string或Symfony \ Component \ Form \ FormTypeInterface”,“MainBundle \ Entity \ Image”的预期参数
这是我的RegistrationEditFormType类:
class RegistrationEditFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('lastName', 'text')
->add('firstName', 'text')
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('phone', 'text')
->add('amount', 'money')
->add('image', new \MainBundle\Entity\Image(), array(
'required' => false,
'data_class' => null
)
)
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'UserBundle\Entity\User'
));
}
public function getName()
{
return 'lldcontribution_userbundle_registration_edit';
}
}
以前我试过:
->add('image', ImageType::class, array(
'required' => false,
'data_class' => null
)
)
并收到此错误:
“MainBundle \ Entity \ Image”类型的预期参数,“array”给出
其他信息:
class ImageType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', 'file', array('data_class' => null))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'MainBundle\Entity\Image'
));
}
/**
* @return string
*/
public function getName()
{
return 'lldcontribution_mainbundle_image';
}
}
使用以下形式的控制器的操作:
/**
* Edit the user
*/
public function editAction($id)
{
//die('ok');
//$user = $this->getUser();
//$id = $request->request->get('id');
$user = $this
->getDoctrine()
->getManager()
->getRepository('UserBundle:User')
->find($id)
;
$request = $this->getRequest();
if (!is_object($user) || !($user instanceof UserInterface)) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
$dispatcher = $this->get('event_dispatcher');
$event = new GetResponseUserEvent($user, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
//$formFactory = $this->get('fos_user.profile.form.factory');
//$form = $formFactory->createForm();
$form = $this->createForm(new RegistrationEditFormType(), $user);
//$form->setData($user);
$form->handleRequest($request);
if ($form->isValid()) {
/** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
$userManager = $this->get('fos_user.user_manager');
$amount = $request->request->get('amount');
$user->setEmail($request->request->get('email'));
$user->setFirstName($request->request->get('firstName'));
$user->setLastName($request->request->get('lastName'));
$user->setPhone($request->request->get('phone'));
$user->setAmount($request->request->get('amount'));
$user->setUpdatedAt(new \DateTime());
//var_dump($user);die();
// Updating the user
$userManager->updateUser($user);
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
//$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('lld_admin');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
$request->getSession()->getFlashBag()->add('info-edit', 'Your account has been updated!');
return $this->render('UserBundle:Profile:edit.html.twig', array(
'form' => $form->createView(),
'user' => $user
));
}
我正在使用Symfony2.8和PHP 5.6。我被困住了,不知道出了什么问题,特别是因为我习惯使用我的Image类而没有任何问题。我错过了什么?有什么建议吗?