1

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 Symfony\Component\Form\FormView 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
  • 1
    Look closely at the example in the docs. The relatively new AbstractController::render method takes original $form as input and takes care of calling createView on it. It is also instructive to look at the source code for AbstractController to see exactly what is going on and how it differs from the previous version of Symfony. Commented Feb 12, 2023 at 22:32
  • @Cerad Hello. As you can see in the question, there was this sentence: "in both cases (meaning even using the createView())" meaning before this question was posed, I've tried to pass the FormInterface without createView() method. Commented Feb 13, 2023 at 12:39

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank your for your hint. Totally missed that this was there. Thank you :)
Your welcome, it often the most obvious that we all miss haha
0

Or you can use widget and label separately.

{{ form_label(form.email) }}
{{ form_widget(form.email) }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.