E
E
entermix2018-03-30 22:53:34
symfony
entermix, 2018-03-30 22:53:34

Why are the form messages not showing up?

There is a simple form:

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;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;

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'),
            ))
            ->add('save', SubmitType::class);
    }

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

Sample:
{% block body %}
    <div class="row">
        <div class="col-md-12">
            <div class="card mb-4">
                <div class="card-header">
                    <a href="{{ url('security_register') }}">
                        <h1 class="card-title h3">{% trans %}Регистрация{% endtrans %}</h1>
                    </a>
                </div>
                <div class="card-body">
                    {{ form_start(registerForm) }}

                    {{ form_errors(registerForm) }}

                    <div class="form-group">
                        {{ form_label(registerForm.username, 'Логин', {'attr': {'class': 'foo'}}) }}
                        {{ form_errors(registerForm.username, {'attr': {'class': 'text-danger'}}) }}
                        {{ form_widget(registerForm.username, {'label': false, 'attr': {
                            'class': 'form-control',
                            'id': 'registerForminputUsername',
                            'placeholder': 'Введите логин'
                        }}) }}
                    </div>

                    <div class="form-group">
                        {{ form_label(registerForm.email, 'E-Mail', {'attr': {'class': 'foo'}}) }}
                        {{ form_errors(registerForm.email, {'attr': {'class': 'text-danger'}}) }}
                        {{ form_widget(registerForm.email, {'label': false, 'attr': {
                            'class': 'form-control',
                            'id': 'registerForminputEmail',
                            'placeholder': 'Введите логин или адрес электронной почты'
                        }}) }}
                    </div>

                    <div class="form-group">
                        {{ form_label(registerForm.plainPassword.first, 'Пароль', {'attr': {'class': 'foo'}}) }}
                        {{ form_errors(registerForm.plainPassword, {'attr': {'class': 'text-danger'}}) }}
                        {{ form_widget(registerForm.plainPassword.first, {'label': false, 'attr': {
                            'class': 'form-control',
                            'id': 'registerForminputPlainPasswordFirst',
                            'placeholder': 'Введите пароль'
                        }}) }}
                    </div>

                    <div class="form-group">
                        {{ form_label(registerForm.plainPassword.second, 'Пароль повторно', {'attr': {'class': 'foo'}}) }}
                        {{ form_errors(registerForm.plainPassword, {'attr': {'class': 'text-danger'}}) }}
                        {{ form_widget(registerForm.plainPassword.second, {'label': false, 'attr': {
                            'class': 'form-control',
                            'id': 'registerForminputPlainPasswordSecond',
                            'placeholder': 'Введите пароль повторно'
                        }}) }}
                    </div>

                    <div class="form-group">
                        {{ form_widget(registerForm.save, {'label': 'Зарегистрироваться', 'attr': {
                            'class': 'btn btn-primary',
                            'id': 'registerFormSaveSubmit',
                            'placeholder': 'Введите пароль повторно'
                        }}) }}
                    </div>

                    {{ form_end(registerForm) }}
                </div>
            </div>
        </div>
    </div>
{% endblock %}

I see errors in the profiler, but they are not displayed on the page. CHADNT?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2018-03-30
@entermix

{{ form_errors(registerForm.plainPassword .first , {'attr': {'class': 'text-danger'}}) }}
{{ form_errors(registerForm.plainPassword .second , {'attr': {'class': 'text-danger'}}) }}
or override https://symfony.com/doc/current/reference/forms/ty... but then the error will only be rendered on the first input

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question