Reputation: 2303
I am using FOSUserBundle to manage my users, and I am trying to override the profile edit form, following this doc guide https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md This is my Form type:
<?php
namespace Tracker\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\ProfileFormType as BaseType;
class ProfileFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$this->buildUserForm($builder, $options);
}
public function getName()
{
return 'tracker_user_profile';
}
/**
* Builds the embedded form representing the user.
*
* @param FormBuilderInterface $builder
* @param array $options
*/
protected function buildUserForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('avatar','file',array( 'required'=>'true'));
}
}
This is my services.yml
add:
tracker_user.profile.form.type:
class: Tracker\UserBundle\Form\Type\ProfileFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: tracker_user_profile }
This is the config.yml
part for setting up FOSUSerBundle
profile:
form:
type: tracker_user_profile
And finally my Controller Action, which i almost copied 1to1 from original FOSUser Controller:
/**
* Edit the user
*/
public function editAction()
{
$user = $this->container->get('security.context')->getToken()->getUser();
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$form = $this->container->get('tracker_user.profile.form.type');
$formHandler = $this->container->get('fos_user.profile.form.handler');
$process = $formHandler->process($user);
if ($process) {
$this->setFlash('fos_user_success', 'profile.flash.updated');
return new RedirectResponse($this->getRedirectionUrl($user));
}
return $this->container->get('templating')->renderResponse(
'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
array('form' => $form->createView())
);
}
When i call the page, i get the error:
Fatal error: Call to undefined method Tracker\UserBundle\Form\Type\ProfileFormType::createView() in /coding/src/Tracker/UserBundle/Controller/ProfileController.php on line 105
Is there anything wrong with the way i set up the service? or my code?
Upvotes: 0
Views: 3406
Reputation: 7745
In your controller, retrieve the Form instance instead of the FormType instance:
$form = $this->container->get('fos_user.profile.form');
Upvotes: 1