如何在 Symfony 模板中为单选按钮(性别)和复选框(语言)做 Bootstrap

问题描述 投票:0回答:0

index.twig.html 尝试更正树枝文件中的性别和语言引导程序。

{% extends 'base.html.twig'%}

{% block title %}Hello StudentRegistrationController!{% endblock %}

{% block body %}
   <div class="container"> 
     <div class="row "> 
     <h1 class = "mt-14 mb-6">Student Registration Form</h1>
        <div class="col-md-6">

           {{ form_start(form, {'attr': {'class': 'form'}}) }}
             {{ form_row(form.first_name, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }}
             {{ form_row(form.last_name, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }} 
             {{ form_row(form.email, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }} 
             
             {{ form_row(form.gender, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }}
 
             {{ form_row(form. address, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }} 
             {{ form_row(form.languages, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }}
             {{ form_row(form.country, {'attr': {'class': 'form-control', 'style': 'margin-bottom: 30px;'}}) }}  
             {{ form_row(form. Submit, {'attr': {'class': 'btn btn-primary btn-block mt-3'}}) }}   
           {{ form_end(form) }}    
        </div> 
     </div> 
 </div>

{% endblock %}

表格(StudentRegistrationType.php)

<?php

namespace App\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\RadioType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Intl\Countries;

class StudentRegistrationType extends AbstractType

{ 

    public function buildForm(FormBuilderInterface $builder, array $options): void
    { 
       $countries = Countries::getNames();
       $builder
        ->add('first_name', TextType::class)
        ->add("last_name", TextType::class)
        ->add('email', EmailType::class)
        ->add('gender', ChoiceType::class, [
            'choices' => [
               'Male' => 'male',
               'Female' => 'female',
           ],
           //'choice_value' => true,
           'choice_value' => function ($choice) {
               return $choice;
            },
           'multiple'=>false,
           'expanded'=>true
        ])

         ->add('address', TextareaType::class)
         ->add('languages', ChoiceType::class, [
           'choices' => [
              'English' => 'Eng',
              'Hindi' => 'Hin',
              'Japanese' => 'Jap'
            ],
            'multiple'=>true,
            'expanded'=>true
            ])

             ->add('country', ChoiceType::class, [
               'choices' => $countries,
               'choice_label' => function ($choice, $key, $value) {
                  return ucwords($value);
                },


            ])

            ->add('Submit', SubmitType::class)
            ;

    }


     public function configureOptions(OptionsResolver $resolver): void
     {
         $resolver->setDefaults([
            // Configure your form options here
         ]);
    }

}

请参阅提供的图像。复选框和单选按钮的引导程序未正确完成。参考了许多网站和官方文档。我的问题仍然没有解决。任何建议都表示赞赏。

图片:- gender & languages

谢谢

symfony bootstrap-4 twig symfony1 symfony-forms
© www.soinside.com 2019 - 2024. All rights reserved.