有趣的部分是在我查看symfony3调试面板时,我可以看到所有那些错误显示在那里:一些是在表单级别(我可以在我的twig模板中看到的那些6)和一些在表单级别(那些2我不能在表单层面看到)
我的问题是:我如何将电话和性别等级错误从字段级别移动到表单级别(反之亦然),以及如何在表单级别和某些字段级别显示某些错误.
这是我的FormType
<?PHPnamespace AppBundle\Form;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilderInterface;use Symfony\Component\OptionsResolver\OptionsResolver;use Symfony\Component\Form\Extension\Core\Type\CheckBoxType;use Symfony\Component\Form\Extension\Core\Type\TextType;use Symfony\Component\Form\Extension\Core\Type\ChoiceType;use Symfony\Component\Form\Extension\Core\Type\submitType;use Symfony\Component\Form\Extension\Core\Type\EmailType;class SubscriberType extends AbstractType { /** * @param FormBuilderInterface $builder * @param array $options */ public function buildForm(FormBuilderInterface $builder,array $options) { $builder ->add('firstname',null,array( 'label' => false,'required' => true,'attr' => array( 'placeholder' => 'First name','class' => 'form-fIEld-set' ))) ->add('lastname','attr' => array( 'placeholder' => 'Last name','class' => 'form-fIEld-set' ))) ->add('emailaddress',EmailType::class,'attr' => array( 'placeholder' => 'Email Address','pattern' => '.{2,}',//minlength 'class' => 'form-fIEld-set' ))) ->add('phone',TextType::class,'attr' => array( 'placeholder' => 'Mobile Phone',//minlength 'class' => 'form-fIEld-set' ))) ->add('gender',ChoiceType::class,array( 'choices' => array('Male' => 1,'Female' => 2,'Refuce to answer' => 0),'label' => false,'placeholder' => 'Gender','attr' => array( 'class' => 'form-fIEld-set' ))) ->add('agreeterms',CheckBoxType::class,array( 'label' => '','required' => true)) ->add('agreeemails','required' => true)) ->add('agreepartners','required' => true)) ->add('submit',submitType::class,array( 'label' => 'Sign Up','attr' => array( 'class' => 'sub-btn' ))) ; } /** * @param OptionsResolverInterface $resolver */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Subscriber' )); } /** * @return string */ public function getname() { return 'subscriber'; }}
这是我的实体
<?PHPnamespace AppBundle\Entity;use Doctrine\ORM\MapPing as ORM;use Symfony\Component\ValIDator\Constraints as Assert;use DateTime;/** * Subscriber * * @ORM\Entity(repositoryClass="AppBundle\Repository\SubscriberRepository") * @ORM\table(name="MediaBay_Master",uniqueConstraints={@ORM\UniqueConstraint(name="subscriber_pkey",columns={"ID"})}) */class Subscriber{ /** * @var int * * @ORM\Column(name="ID",type="integer") * @ORM\ID * @ORM\GeneratedValue(strategy="auto") */ private $ID; /** * @var string * * @Assert\NotBlank (message="Complete First name fIEld") * @ORM\Column(name="first_name",type="string",length=255,nullable=false) */ private $firstname; /** * @var string * * @Assert\NotBlank (message="Complete Last name fIEld") * @ORM\Column(name="last_name",length=50,nullable=false) */ private $lastname; /** * @var string * * @Assert\NotBlank (message="Complete Email Address fIEld") * @ORM\Column(name="email_address",length=100,nullable=false) * @Assert\Email( * message = "The email '{{ value }}' is not a valID email.",* checkMX = true,* checkHost = true * ) */ private $emailAddress; /** * @var string * * @Assert\NotBlank (message="Complete Mobile Phone fIEld") * @Assert\Length(min=5) (message="Phone lenght must be over 5 characters") * @ORM\Column(name="phone",nullable=false) */ private $phone; /** * @var int * @Assert\GreaterThanorEqual("18") * @ORM\Column(name="age",type="smallint",nullable=true) */ private $age; /** * @var int * * @Assert\NotBlank (message="Complete Gender fIEld") * @ORM\Column(name="gender",nullable=false) */ private $gender; /** * @var int * @ORM\Column(name="education_level_ID",nullable=true) */ private $educationLevelID; /** * @var int * * @ORM\Column(name="resource_ID",type="smallint") */ private $resourceID; /** * @var bool * @Assert\NotBlank (message ="Please select this fIEld") * @ORM\Column(name="agree_terms",type="boolean") */ private $agreeTerms; /** * @var bool * @Assert\NotBlank (message ="please select this fIEld") * @ORM\Column(name="agree_emails",type="boolean") */ private $agreeEmails; /** * @var bool * @Assert\NotBlank (message ="please select this fIEld") * @ORM\Column(name="agree_partners",type="boolean",nullable=true) */ private $agreePartners; /** * @var \DateTime * * @ORM\Column(name="subscription_date",type="datetime") */ private $subscriptionDate; /** * @var string * * @ORM\Column(name="subscription_ip",length=50) */ private $subscriptionIp; /** * @var \DateTime * * @ORM\Column(name="unsubscription_date",type="datetime",nullable=true) */ private $unsubscriptionDate; /** * @var string * * @ORM\Column(name="unsubscription_ip",nullable=true) */ private $unsubscriptionIp; /** * @var string * * @ORM\Column(name="continent_code",length=2,nullable=true) */ private $continentCode; /** * @var string * * @ORM\Column(name="country_name",length=59,nullable=true) */ private $countryname; /** * @var string * * @ORM\Column(name="region",nullable=true) */ private $region; /** * @var string * * @ORM\Column(name="city",nullable=true) */ private $city; /** * @var string * * @ORM\Column(name="postal_code",nullable=true) */ private $postalCode; /** * @var string * * @ORM\Column(name="latitude",nullable=true) */ private $latitude; /** * @var string * * @ORM\Column(name="longitude",nullable=true) */ private $longitude; /** * @var string * * @ORM\Column(name="dma_code",nullable=true) */ private $dmaCode; /** * @var string * * @ORM\Column(name="area_code",nullable=true) */ private $areaCode; /** * @var string * * @ORM\Column(name="hash",length=255) */ private $hash;
}
这些是我已经完成的一些资源,但他们并没有完全回答为什么在表单层面上显示某些错误以及在字段级别上显示某些错误的问题:
form_errors(form) not showing any errors in Symfony Twig
Get global form errors from the FormView in twig template
这是我的twig temaplte:
<div ID="alert"> <div > <div ID="alert-Title" >{{ 'Please solve below errors'|trans }}:</div> <div ID="alert-text" > {% if error %} <ul> <li> {{ 'please check if you wrote correct data'|trans }} </li> </ul> {% endif %} <div > {{ form_errors(form) }} </div> </div> <div >{{ 'Close'|trans }}</div> </div></div>
使用{{form_errors(form)}}时会出现表单级错误(其中6个)
我还尝试单独包含每个错误字段(请参见下文),但这只会带来字段级错误:
<div ID="alert"> <div > <div ID="alert-Title" >{{ 'Please solve below errors'|trans }}:</div> <div ID="alert-text" > {% if error %} <ul> <li> {{ 'please check if you wrote correct data'|trans }} </li> </ul> {% endif %} <div > {{ form_errors(form.firstname) }} {{ form_errors(form.lastname) }} {{ form_errors(form.emailaddress) }} {{ form_errors(form.phone) }} {{ form_errors(form.gender) }} {{ form_errors(form.agreeterms) }} {{ form_errors(form.agreeemails) }} {{ form_errors(form.agreepartners) }} </div> </div> <div >{{ 'Close'|trans }}</div> </div> </div>
我也试过了
任何建议将受到高度赞赏
{{ form_errors(form) }}
将在表单级别显示错误
{{form_errors(form.fIEld) }}
将显示该字段的错误
默认情况下,除非将error_bubbling设置为true,否则错误将保留在它们发生的字段上.例如.
->add('phone',/* * Errors in phone will go to the form,* instead of staying in the fIEld */ 'error_bubbling' => true,//minlength 'class' => 'form-fIEld-set' )))
使用error_bubbling => true,在每个字段上,您只需使用即可
{{ form_errors(form) }}
在您的树枝模板中,显示所有错误.即每个错误都会冒泡到表单,而不是停留在字段级别.
总结以上是内存溢出为你收集整理的错误处理 – Symfony 3表单错误未在twig模板中显示全部内容,希望文章能够帮你解决错误处理 – Symfony 3表单错误未在twig模板中显示所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)