E
E
Evgeny Romashkan2018-07-04 00:46:28
symfony
Evgeny Romashkan, 2018-07-04 00:46:28

Is password_widget in Symfony messing up the whole system?

So. How it all happened:
1. I dealt with form customization in Symfony https://symfony.com/doc/current/form/form_customization .
Came up with examples like this.

{% block text_widget %}
    <div class="form-control">
        {{ block('form_widget_simple') }}
    </div>
{% endblock %}

2. Since Symfony has very good documentation, as always, I could not find what to specify in block('...'); so that I have an input with text to enter a password. But it says that all these templates are stored along the path
FrameworkBundle/Resources/views/Form
Actually, along this path I found the file "password_widget.html.php",
3. Well, without thinking twice, I created this code:
{% block password_widget %}
    <div class="form-control">
        {{ block('password_widget') }}
    </div>
{% endblock %}

4. Tried to reload the page. What came of it:
- The page loads endlessly. Tolerable. We need to look for the error. But first I tried to leave it and see what would load in half a minute.
I am attaching a screenshot to make it clearer.
Screen
5b3bee0cb0c02778561551.png

Specifically: the CPU of the computer has become 100% loaded. When I first tried to load this page, it also somehow managed to fill up all the RAM. I don't usually use swap.
So, a couple of questions:
1. How to make a field for entering a password
2. Is password_widget used somewhere, or did I go where I don't need it?
Symfony 4 and PHP 7 are used. The system is loaded by php-fpm
Upd: such code, for example, works
{% block password_row %}
    <div>
        {{ block('form_label') }}
        <div class="form-control">
            {{ block('password_widget') }}
        </div>
    </div>
{% endblock %}

Form class (taken from the documentation)
UserType.php
<?php

namespace App\Form;

use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('email', EmailType::class)
            ->add('username', TextType::class)
            ->add('plainPassword', RepeatedType::class, array(
                'type' => PasswordType::class,
                'first_options'  => array('label' => 'Password'),
                'second_options' => array('label' => 'Repeat Password'),
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}

View:
register.html.twig
{% extends 'base.html.twig' %}

{% form_theme form _self %}
{% block password_widget %}
    <div class="form-control">
        {{ block('password_widget') }}
    </div>
{% endblock %}
{% block body %}
    <div class="form-control col-md-6">
        {{ form_start(form) }}
        <div>
            {{ form_label(form.username) }}:
            {{ form_errors(form.username) }}
            {{ form_widget(form.username) }}
            {{ form_help(form.username) }}
        </div>
        {{ form_row(form.email) }}
        {{ form_row(form.plainPassword.first) }}
        {{ form_row(form.plainPassword.second) }}

        <button type="submit">Register!</button>
        {{ form_end (form) }}
    </div>
{% endblock %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2018-07-04
@EvgeniiR

Congratulations, now you know what recursion is :)

{% block password_widget %}
    <div class="form-control">
        {{ block('password_widget') }}
    </div>
{% endblock %}

Remove the code
{% form_theme form _self %}
{% block password_widget %}
    <div class="form-control">
        {{ block('password_widget') }}
    </div>
{% endblock %}

and everything should work
If you really want to customize, then
{% block password_widget %}
    <div class="form-control">
        {%- set type = type|default('password') -%}
        {{ block('form_widget_simple') }}
    </div>
{% endblock %}

either, as you wrote, through password_row, or as stated in very good documentation
. Basic implementations are in form_div_layout.html.twig , FrameworkBundle/Resources/views/Formtemplates for php forms are in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question