我遇到了表单没有通过验证的问题,无论值是否正确传递。此外,这只发生在一个skuBrand字段上。
我还从实体中删除了所有验证断言。我不明白为什么$form->isValid() = false方法。解析请求。它有一个值(对象Id)并且该对象在数据库中。{{ form_errors(form) }}变量不显示错误。
你怎么能理解为什么表单没有通过验证?
代码:表单类
class FiltersType extends AbstractType
{
protected $securityContext;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$formOptions = $this->getFormOption('barcodeConsumer', array( 'required' => false, 'label' => 'fields.skuGlobal.barcodeConsumer', 'translation_domain' => 'Admin',));
$builder->add('barcodeConsumer', 'text', $formOptions);
$formOptions = $this->getFormOption('barcodeLogistics', array( 'required' => false, 'label' => 'fields.skuGlobal.barcodeLogistics', 'translation_domain' => 'Admin',));
$builder->add('barcodeLogistics', 'text', $formOptions);
$formOptions = $this->getFormOption('name', array( 'required' => false, 'label' => 'fields.skuGlobal.name', 'translation_domain' => 'Admin',));
$builder->add('name', 'text', $formOptions);
$formOptions = $this->getFormOption('nameShort', array( 'required' => false, 'label' => 'fields.skuGlobal.nameShort', 'translation_domain' => 'Admin',));
$builder->add('nameShort', 'text', $formOptions);
$formOptions = $this->getFormOption('skuCategory', array( 'multiple' => false, 'em' => 'default', 'class' => 'Nitra\\SchemaCDBBundle\\Entity\\SkuCategory', 'required' => false, 'group_by' => 'skuGroup.name', 'label' => 'fields.skuGlobal.skuCategory', 'translation_domain' => 'Admin',));
$builder->add('skuCategory', 'entity', $formOptions);
$formOptions = $this->getFormOption('skuBrand', array( 'multiple' => false, 'em' => 'default', 'class' => 'Nitra\\SchemaCDBBundle\\Entity\\SkuBrand', 'required' => false, 'label' => 'fields.skuGlobal.skuBrand', 'translation_domain' => 'Admin',));
$builder->add('skuBrand', 'entity', $formOptions);
$formOptions = $this->getFormOption('skuType', array( 'multiple' => false, 'em' => 'default', 'class' => 'Nitra\\SchemaCDBBundle\\Entity\\SkuType', 'required' => false, 'label' => 'fields.skuGlobal.skuType', 'translation_domain' => 'Admin',));
$builder->add('skuType', 'entity', $formOptions);
$formOptions = $this->getFormOption('externalKey1', array( 'required' => false, 'label' => 'fields.skuGlobal.externalKey1', 'translation_domain' => 'Admin',));
$builder->add('externalKey1', 'text', $formOptions);
$formOptions = $this->getFormOption('externalKey2', array( 'required' => false, 'label' => 'fields.skuGlobal.externalKey2', 'translation_domain' => 'Admin',));
$builder->add('externalKey2', 'text', $formOptions);
$formOptions = $this->getFormOption('status', array( 'required' => false, 'choices' => array( 0 => 'Нет', 1 => 'Да', ), 'empty_value' => '', 'label' => 'fields.skuGlobal.status', 'translation_domain' => 'Admin',));
$builder->add('status', 'choice', $formOptions);
}
protected function getFormOption($name, array $formOptions)
{
return $formOptions;
}
public function getName()
{
return 'filters_skuglobal';
}
public function setSecurityContext($securityContext)
{
$this->securityContext = $securityContext;
}
}
SkuGlobal的精髓
class SkuGlobal extends BaseEntity {
use \Nitra\SchemaCDBBundle\Model\TranslatableEx;
public function __toString() {
return (string) $this->translate()->getName();
}
/**
* Бренд СКЮ
* @ORM\ManyToOne(targetEntity="SkuBrand", inversedBy="skuGlobals")
*/
protected $skuBrand;
}
SKU_品牌实体。
class SkuBrand extends BaseEntity
{
use \Nitra\SchemaCDBBundle\Model\TranslatableEx;
public function __toString()
{
return (string) $this->translate()->getName();
}
/**
* Глобальные СКЮ привязанные к бренду
*
* @ORM\OneToMany(targetEntity="SkuGlobal", mappedBy="skuBrand")
*/
protected $skuGlobals;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->skuGlobals = new \Doctrine\Common\Collections\ArrayCollection();
$this->target = new \Doctrine\Common\Collections\ArrayCollection();
$this->promotionSkuListDetail = new \Doctrine\Common\Collections\ArrayCollection();
$this->updateTranslations();
}
}
要显示表单未通过验证的错误,您需要执行以下操作。1 创建一个 Validator 类型的对象并调用 validate 方法并将验证对象传入其中。