我的断言有问题,只有几本书。
例如,使用"firstName"
的断言是可以的,我有错误消息,但是对于lastName和“ introduction”没有任何帮助。
此外,我的EqualTo
似乎不可用:(。
我做错了吗?
实体
<?php
namespace App\Entity;
use App\Entity\Ad;
use Cocur\Slugify\Slugify;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"email"}, message= "User already exist")
*/
class User implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Champ Obligatoire")
* @Assert\Length(min = 2, minMessage=" Spice de petit troll ! ")
* @Assert\Valid
* @Assert\NotNull
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="Champ Obligatoire")
* @Assert\Length(min = 2, minMessage=" Spice de petit troll ! ")
* @Assert\Valid
* @Assert\NotNull
*/
private $lastName;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Email()
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Url(message="Entrez une Url valide !")
*/
private $picture;
/**
* @ORM\Column(type="string", length=255)
*/
private $hash;
/**
* @ORM\Column(type="string", length=255)
* @Assert\Length(min=10, minMessage="Au moins 10 lettres mon coco ! ")
*
*/
private $introduction;
/**
* @Assert\EqualTo(propertyPath="hash")
*/
public $passwordConfirm;
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Ad", mappedBy="author")
*/
private $ads;
/**
* Permet d'initialiser le slug
*
* @ORM\PrePersist
* @ORM\PreUpdate
*
* @return void
*/
public function initializeSlug()
{
if(empty($this->slug)){
$slugify = new Slugify();
$this->slug = $slugify->slugify($this->firstName . ' ' . $this->lastName);
}
}
public function __construct()
{
$this->ads = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function setLastName(string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPicture(): ?string
{
return $this->picture;
}
public function setPicture(?string $picture): self
{
$this->picture = $picture;
return $this;
}
public function getHash(): ?string
{
return $this->hash;
}
public function setHash(string $hash): self
{
$this->hash = $hash;
return $this;
}
public function getIntroduction(): ?string
{
return $this->introduction;
}
public function setIntroduction(string $introduction): self
{
$this->introduction = $introduction;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return Collection|Ad[]
*/
public function getAds(): Collection
{
return $this->ads;
}
public function addAd(Ad $ad): self
{
if (!$this->ads->contains($ad)) {
$this->ads[] = $ad;
$ad->setAuthor($this);
}
return $this;
}
public function removeAd(Ad $ad): self
{
if ($this->ads->contains($ad)) {
$this->ads->removeElement($ad);
// set the owning side to null (unless already changed)
if ($ad->getAuthor() === $this) {
$ad->setAuthor(null);
}
}
return $this;
}
// IMPLEMENTATION USERINTERFACE COMPLEMENTS
public function getRoles() {
return ['ROLE_USER'];
}
public function getPassword()
{
return $this->hash;
}
public function getSalt() {}
public function getUsername()
{
return $this->email;
}
public function eraseCredentials(){}
}
表格
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class RegistrationType extends ApplicationType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class, $this->getConfiguration("Prénom", "Votre prénom ..."))
->add('lastName', TextType::class, $this->getConfiguration("Nom", "Votre Nom ..."))
->add('email', EmailType::class, $this->getConfiguration("Email", "Votre adresse email"))
->add('picture', UrlType::class, $this->getConfiguration("Photo de profil", "URL de votre avatar ..."))
->add('hash', PasswordType::class, $this->getConfiguration("Mot de Passe", "Choisissez un mot de passe ..."))
->add('passwordConfirm', PasswordType::class, $this->getConfiguration("Confirmation de mot de passe", "Veuillez confirmer cotre mot de passe"))
->add('introduction', TextType::class, $this->getConfiguration("Decription", " Présentez-vous !"))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
控制器
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class RegistrationType extends ApplicationType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class, $this->getConfiguration("Prénom", "Votre prénom ..."))
->add('lastName', TextType::class, $this->getConfiguration("Nom", "Votre Nom ..."))
->add('email', EmailType::class, $this->getConfiguration("Email", "Votre adresse email"))
->add('picture', UrlType::class, $this->getConfiguration("Photo de profil", "URL de votre avatar ..."))
->add('hash', PasswordType::class, $this->getConfiguration("Mot de Passe", "Choisissez un mot de passe ..."))
->add('passwordConfirm', PasswordType::class, $this->getConfiguration("Confirmation de mot de passe", "Veuillez confirmer cotre mot de passe"))
->add('introduction', TextType::class, $this->getConfiguration("Decription", " Présentez-vous !"))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
嫩枝
{% extends 'base.html.twig' %}
{% block title %} Inscription {% endblock %}
{% block body %}
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="bg-dark py-3 px-3">
<h1>Inscrivez-vous sur notre site !</h1>
{{ form_start(form)}}
{{ form_widget(form)}}
<button type="submit" class="btn btn-success"> Confirmez votre inscription</button>
</div>
</div>
</div>
<br/><br/>
{% endblock %}
感谢您的观看:)<3
我希望所有断言消息都能正常工作。