0
votes

I have the user, which can register via form: name, lastname, email, phone, password, username.

Registered user can change: name, lastname, phone

My UserType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text')
        ->add('lastname', 'text')
        ->add('email', 'email')
        ->add('phone', 'text')
        ->add('password', 'text')
        ->add('username', 'text');
}

How should I render different form for registration and update?

1
Simple: just make another Twig view and inject the same FormType but only display form_row() that are needed. You'd need to bind your form with your entity though so data is present in the form when user is changing their profile.D4V1D

1 Answers

1
votes

Only difference is what you pass as a second attribute to createForm method. If you pass empty entity it will be form to add new user. If you pass entity with populated data of your user entity it will be an edit form. So:

$yourNewUserForm = $this->createForm(new UserType(), new UserEntity());

$userEntity = $this->getDoctrine()->getRepository('YourBundle:User')->find(1);
$yourEditUserForm = $this->createForm(new UserType(), $userEntity);

NOTE: Above code assumes you are in class extending Controller and you have your createForm and getDoctrine() methods available but consider if this should be a part of controller or it should be moved somewhere else (which is probably a better idea)