gpt4 book ai didi

error-handling - Symfony 3表单错误未显示在 Twig 模板中

转载 作者:行者123 更新时间:2023-12-04 06:47:08 25 4
gpt4 key购买 nike

我正在调试我在symfony3中创建的表单,其中用 Twig 作为模板引擎。由于某种原因,如果我使用{{form_errors(form)}}(应该返回所有形式的错误),我只会得到部分结果(消息框中未显示某些错误)。

当我查看symfony3调试面板时,有趣的部分进来了,我可以看到在那里显示的所有错误:有些在表单级别(我在 Twig 模板中可以看到的那些6)和一些在表单级别(我不能看到的2)在表单级别上查看)enter image description here

我的问题是:如何将电话和性别级别的错误从字段级别转移到表单级别(反之亦然),以及如何在表单级别显示一些错误,而在字段级别显示一些错误。

这是我的FormType

    <?php
namespace 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', null, array(
'label' => false,
'required' => true,
'attr' => array(
'placeholder' => 'Last Name',
'class' => 'form-field-set'
)))
->add('emailaddress', EmailType::class, array(
'label' => false,
'required' => true,
'attr' => array(
'placeholder' => 'Email Address',
'pattern' => '.{2,}', //minlength
'class' => 'form-field-set'
)))
->add('phone', TextType::class, array(
'label' => false,
'required' => true,
'attr' => array(
'placeholder' => 'Mobile Phone',
'pattern' => '.{2,}', //minlength
'class' => 'form-field-set'
)))
->add('gender', ChoiceType::class, array(
'choices' => array('Male' => 1, 'Female' => 2, 'Refuce to answer' => 0),
'label' => false,
'required' => true,
'placeholder' => 'Gender',
'attr' => array(
'class' => 'form-field-set'
)))
->add('agreeterms', CheckboxType::class, array(
'label' => '',
'required' => true))
->add('agreeemails', CheckboxType::class, array(
'label' => '',
'required' => true))
->add('agreepartners', CheckboxType::class, array(
'label' => '',
'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';
}
}

这是我的实体
<?php

namespace 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", type="string", length=50, nullable=false)
*/
private $lastName;

/**
* @var string
*
* @Assert\NotBlank (message="Complete Email Address field")
* @ORM\Column(name="email_address", type="string", 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", type="string", length=50, 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", type="smallint", nullable=false)
*/
private $gender;

/**
* @var int
* @ORM\Column(name="education_level_id", type="smallint", 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", type="string", length=50)
*/
private $subscriptionIp;

/**
* @var \DateTime
*
* @ORM\Column(name="unsubscription_date", type="datetime", nullable=true)
*/
private $unsubscriptionDate;

/**
* @var string
*
* @ORM\Column(name="unsubscription_ip", type="string", length=50, nullable=true)
*/
private $unsubscriptionIp;

/**
* @var string
*
* @ORM\Column(name="continent_code", type="string", length=2, nullable=true)
*/
private $continentCode;

/**
* @var string
*
* @ORM\Column(name="country_name", type="string", length=59, nullable=true)
*/
private $countryName;

/**
* @var string
*
* @ORM\Column(name="region", type="string", length=255, nullable=true)
*/
private $region;

/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255, nullable=true)
*/
private $city;

/**
* @var string
*
* @ORM\Column(name="postal_code", type="string", length=50, nullable=true)
*/
private $postalCode;

/**
* @var string
*
* @ORM\Column(name="latitude", type="string", length=50, nullable=true)
*/
private $latitude;

/**
* @var string
*
* @ORM\Column(name="longitude", type="string", length=50, nullable=true)
*/
private $longitude;

/**
* @var string
*
* @ORM\Column(name="dma_code", type="string", length=50, nullable=true)
*/
private $dmaCode;

/**
* @var string
*
* @ORM\Column(name="area_code", type="string", length=50, nullable=true)
*/
private $areaCode;

/**
* @var string
*
* @ORM\Column(name="hash", type="string", 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 class="window-BG" id="alert">
<div class="window">
<div id="alert-title" class="win-img-text">{{ 'Please solve below errors'|trans }}:</div>
<div id="alert-text" class="win-text">
{% if error %}
<ul>
<li>
{{ 'please check if you wrote correct data'|trans }}
</li>
</ul>
{% endif %}
<div style="color: white">
{{ form_errors(form) }}
</div>
</div>
<div class="window-close win-close-2">{{ 'Close'|trans }}</div>
</div>
</div>

使用{{form_errors(form)}}时,会引发表单级错误(其中6个错误)

我也尝试过分别包含每个错误字段(请参见下文),但这只会带来字段级别的错误:
<div class="window-BG" id="alert">
<div class="window">
<div id="alert-title" class="win-img-text">{{ 'Please solve below errors'|trans }}:</div>
<div id="alert-text" class="win-text">
{% if error %}
<ul>
<li>
{{ 'please check if you wrote correct data'|trans }}
</li>
</ul>
{% endif %}
<div style="color: white">
{{ 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 class="window-close win-close-2">{{ 'Close'|trans }}</div>
</div>
</div>

我也尝试过
任何建议将不胜感激

最佳答案

{{ form_errors(form) }}

将在表单级别上显示 错误
{{form_errors(form.field) }}

会在该字段
上显示 错误

默认情况下,除非将error_bubbling设置为true
,否则 错误会保留在发生错误的字段中。例如。
->add('phone', TextType::class, array(
'label' => false,
'required' => true,
/*
* Errors in phone will go to the form,
* instead of staying in the field
*/
'error_bubbling' => true,
'attr' => array(
'placeholder' => 'Mobile Phone',
'pattern' => '.{2,}', //minlength
'class' => 'form-field-set'
)))

通过error_bubbling => true,您可以在每个字段上简单地使用
{{ form_errors(form) }}

在 Twig 模板中显示所有错误。 IE。每个错误都将冒泡到表单,而不是停留在字段级别。

关于error-handling - Symfony 3表单错误未显示在 Twig 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com