环境: symfony 5.3.10 / php 8.0
我需要提交一个特定的表单,具有多个级别(与我的实体不匹配),所以我用集合创建了模型,每个模型都创建了一个formType
我的主要模型"FirstModel“是经过验证的,但是集合中包含的模型不是。确认有效。我可以添加一个空的或空的标签,在提交我的表单时不进行验证(没有特定的验证组)。
<?php declare(strict_types=1);
namespace App\Model\Test;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
class FirstModel
{
/**
* @Assert\NotBlank
*/
private ?string $numero = null;
/**
* @Assert\All({
* @Assert\Type(type="App\Model\Test\SecondModel")
* })
* @Assert\Valid()
*/
private Collection $listItems;
public function __construct()
{
$this->listItems= new ArrayCollection();
}
/**
* @return string|null
*/
public function getNumero(): ?string
{
return $this->numero;
}
/**
* @param string|null $numero
*/
public function setNumero(?string $numero): void
{
$this->numero = $numero;
}
/**
* @return Collection
*/
public function getListItems(): Collection
{
return $this->listItems;
}
/**
* @param Collection $listItems
*/
public function setListItems(Collection $listItems): void
{
$this->listItems= $listItems;
}
public function addListItem(SecondModel $secondModel): void
{
if (!$this->listItems->contains($secondModel)) {
$this->listItems[] = $secondModel;
}
}
public function removeListItem(SecondModel $secondModel): void
{
if ($this->listItems->contains($secondModel)) {
$this->listItems->removeElement($secondModel);
}
}
}SecondModel:
<?php declare(strict_types=1);
namespace App\Model\Test;
use Symfony\Component\Validator\Constraints as Assert;
class SecondModel
{
/**
* @Assert\NotBlank
*/
private ?string $label= null;
public function getLabel(): ?string
{
return $this->numero;
}
public function setLabel(?string $label): void
{
$this->label= $label;
}
}第一个formType:
<?php declare(strict_types=1);
namespace App\Form\Test;
use App\Model\Test\FirstModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Valid;
class FirstModelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('numero', TextType::class)
->add(
'listItems',
CollectionType::class,
[
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'entry_type' => SecondModelType::class,
'constraints' => array(new Valid())
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => FirstModel::class,
'csrf_protection' => false,
'allow_extra_fields' => false,
]);
}
}二次FormType
<?php declare(strict_types=1);
namespace App\Form\Test;
use App\Model\Test\SecondModel;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class SecondModelType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('label', TextType::class);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => SecondModel::class,
'csrf_protection' => false,
'allow_extra_fields' => false,
]);
}
}谢谢你的帮忙
发布于 2021-12-22 12:50:13
我解决了我的问题
我的文件中有一个错误,不在我的例子中。
我错过了一个"*",原来我有:
/*
* @Assert\NotBlank
*/抱歉的
https://stackoverflow.com/questions/70394325
复制相似问题