skip to Main Content

I’ve just updated to Symfony 6.2 (6.2.6 to be exact) and now, not sure why, unable to render form.
This is what debugger says:

Object of class SymfonyComponentFormFormView could not be converted to string

In Symfony 6.2, according to the documentation, it should be possible to pass only FormInterface into render method in Controller. However, in both cases (meaning even using the createView()) method, it’s unable to render the form itself. Any ideas where the problem might be?

Controller method:

#[Route(path: '/register', name: 'security-register')]
    public function register(Request $request, MailUtil $util): Response
    {
        $form = $this->createForm(RegistrationForm::class);
        $form->handleRequest($request);
        if($form->isSubmitted() && $form->isValid()){
            $mail = new Mail();
            $mail->setRecpient($form->get('email')->getData());
            $mail->setTemplate("TestMail");
            $util->sendMail($mail);
        }

        return $this->render("security/register.html.twig", ['form' => $form->createView()]);
    }

Form class:

class RegistrationForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('email', EmailType::class, [
            'required' => true
            ])
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'invalid_message' => 'register.error.password',
                'required' => true,
                'first_options' => ['label' => 'Password'],
                'second_options' => ['label' => 'Repeat password']
            ])
            ->add('submit', SubmitType::class);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'csrf_protection' => true,
            'csrf_field_name' => '_csrf',
            'csrf_token_id' => 'user_register'
        ]);
    }
}

Twig:

{% block container %}
    <div class="row">
        <div class="col s6 offset-s3">
            <div class="card-panel blue-grey darken-1" data-darkmode="card-panel blue-grey darken-3">
                {% if not app.user %}
                    <h4 class="white-text center-align ">
                        Register
                    </h4>
                    {{ form_start(form) }}
                    {{ form.email }}
                    {{ form_end(form) }}
                    <div class="white-text center-align">
                        You are logged in as {{ app.user.userIdentifier }}<br><br><a class="waves-effect waves-light btn" href="{{ path('app_logout') }}">Logout</a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

Tried to use new, Symfony 6.2 approach as stated in documentation: https://symfony.com/doc/current/forms.html#rendering-forms

And then tried to use the old one with createView() method.

Expected result should be rendered form, however both methods are throwing the same stacktrace.

2

Answers


  1. In twig {{ form.email }} is wrong. This is probably why the error occur.

    Switch to {{ form_row(form.email) }} for example

    Login or Signup to reply.
  2. Or you can use widget and label separately.

    {{ form_label(form.email) }}
    {{ form_widget(form.email) }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search