我只想简单地更新同Doctrine ORM在书状6.4中的记录。 我与一个称为“类别”的实体一起更新了这一登记册。
namespace AppEntity;
use AppRepositoryCategoriaRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentValidatorConstraintsNotNull;
#[ORMEntity(repositoryClass: CategoriaRepository::class)]
class Categoria
{
#[ORMId]
#[ORMGeneratedValue]
#[ORMColumn]
private ?int $id = null;
#[ORMColumn(length: 100), AssertNotBlank(message: ¡El nombre es requerido! )]
private ?string $nombre = null;
#[ORMColumn(length: 100), AssertNotBlank(message: ¡El slug es requerido! )]
private ?string $slug = null;
#Setters and getters...
我的控制者,特别是这种活动的方法和路线,都好像:
#[Route( /doctrine/categorias/editar/{id} , name: doctrine_categorias_editar )]
public function categoriasEditar(int $id, Request $request, ValidatorInterface $validator, SluggerInterface $slugger): Response
{
$categoria = $this->em->getRepository(Categoria::class)->find($id);
if (!$categoria) {
throw $this->createNotFoundException( La categoría no existe. );
}
$form = $this->createForm(CategoriaFormType::class, $categoria);
$form->handleRequest($request);
var_dump($form->getData());
if ($form->isSubmitted() && $form->isValid()) {
var_dump($form->getData());
// $this->em->flush();
// return $this->redirectToRoute( categoria_inicio );
} else {
//throw $this->createAccessDeniedException("Valores erroneos!");
}
return $this->render( doctrine/categorias_editar.html.twig , [
form => $form->createView(),
]);
}
形式如下:
class CategoriaFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
#I use [ required => false] for test errors
->add( nombre , TextType::class, [ required => false])
->add( slug , TextType::class, [ required => false])
->add( enviar , SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
data_class => Categoria::class,
]);
}
}
The problem is that when I click the "enviar" button, leaving the "nombre" field empty, I would expect the Asserts to do their job and redirect me or something. But I immediately get the error: Expected argument of type "string", "null" given at property path "name". Now, traditionally I also tried:
$errors = $validator->validate($categoria);
if (count($errors) > 0) {
return $this->render( doctrine/categorias_editar.html.twig , [ form => $form, errors => $errors]);
} else {
$campos = $form->getData();
$categoria->setNombre($campos->getNombre());
$categoria->setSlug($campos->getSlug());
//$this->em->persist($categoria);
$this->em->flush();
return $this->redirectToRoute( doctrine_inicio );
}
Where do I ask, if I had errors. In the same way, I would expect it to get here and just that, it would tell me that there are errors, but nothing, it just skips the message already mentioned.
Another curiosity is in the "IF" "if ($form->isSubmitted() && $form->isValid())" from the controller, If I put a var_dump() or a die( ) before, it executes it correctly, if it is inside the if, it never goes through there. only the error occurs.
What is happening? What am I doing wrong?
我曾尝试过两种办法,以“isValid(
>)”的方式来弥补这些错误,并在“$erors =$validator->”;validate($categoria);
,如果存在错误(现值($erors) >0),甚至发生差错,跨国计算机应用系统(var_dumps <>/code>或
。
if ($form->isSubmitted() && $form->isValid()) {
```"