1

I am wondering how to solve this problem: I have form with 4 fields. I want 4th field to be dependent on user status (logged or unlogged). For logged user I will get ID from session but unlogged user should provide username manually.

I dont know which option should i use. Inherit_data, two form types (two much duplicated code) or validation groups based on the submitted data. Any ideas?

symfonydevpl
  • 567
  • 1
  • 6
  • 20
  • it's not clear what exactly you'd like to be different? Different form types like if logged in than hidden-field with token and when not - dropdown with all available usernames ? – V-Light May 22 '17 at 11:07
  • @V-Light validation will be different for this two cases. For logged in user there is no field "username" - only hidden field "user". But for unlogged field "username" (which is text field) is required. I dont know how to create this two forms using only one form builder. – symfonydevpl May 22 '17 at 11:34

3 Answers3

1

Ok, There are several ways to achive that.

Take a Look at FormEvents. In your case it would be FormEvents::PRE_SET_DATA and then read about dynamic forms

I personly prefer to do following

public function buildForm(FormBuilderInterface $builder, array $options)
{
   //$builder->add( ... )
   //$builder->add( ... )
   //$builder->add( ... )

   //each-event with own method, but of cource it can be a callback like in a tutorial above
   $builder->addEventListener(FormEvents::PRE_SET_DATA, array(this, 'onPreSetData');

}

and in the same class there is a method onPreSetData

public function onPreSetData ( FormEvent $formEvent )
{
    $form = $formEvent->getForm();
    $user = $formEvent->getData();

    //pseudo-code
    if( $user->isLoggedIn() )
    {
        $form->add('user', HiddenType::class, array(

        ));
    }
    else
    {
        $form->add('user', TextType::class, array(
            'label' => 'Username',
            'required' => true,
            'constraints' => array(
                new NotBlank(array('message' => 'please enter username...')),
                // new YourCustomValidator(array('message' => 'not enough minerals...')),
            )
        ));
    }
}
V-Light
  • 3,065
  • 3
  • 25
  • 31
1

I personally think a more elegant solution is to pass the user to the form builder from your controller. It's been covered in this answer here: how to check the user role inside form builder in Symfony2?

OK sure
  • 2,617
  • 16
  • 29
0

you can create form by individual fields like

 {{ form_row(form.username) }}                           
 {{ form_row(form.email) }}                           
 {{ form_row(form.phone) }} 
 {% if(app.user) %}
     //your conditional field
 {% endif%} 

By this way, you have to create submit button as well as csrf token if there

I hope this will quite helpful :)

  • I know about creating individual fields. I can display fields depending on user status, but i cant validate them properly. If logged in user will submit the form, he will see error which says that "username" fields is required, because i want to use only one FormBuilder class – symfonydevpl May 22 '17 at 11:39