Open LucasGreard opened 8 months ago
Hi,
Maybe like @javiereguiluz said in the last response from #5687
Try to make your AssociationField as autocomplete() and then add this HTML attribute to it:
$field->setFormTypeOption('attr.data-ea-autocomplete-render-items-as-html', 'true');
I tried and data-ea-autocomplete-render-items-as-html
seems to be optional
Don't forget to add the image in __toString affinity class
Hi ! Thanks for that I tried and it doesn't work. This forces it to be a string :/
Try '<img src="..." /> ' . $your_label;
I try return '<img src="'. $this->img .'" />';
and return '<img src="..." /> ' . $this->img;
and i've the same problem
Is that ? I'm not sure
Don't forget to add the dir. I tried and it works for me
My test code :
yield AssociationField::new('customer')
->setRequired(true)
->setFormTypeOption('attr', ['required' => true])
->setColumns(4)
->autocomplete();
In my customer class:
public function __toString(): string
{
return '<img src="media/products/images/test.png" />' . $this->fullName;
}
No need public dir, you are already in public
I don't understand why it doesn't work
My Crud : `<?php
namespace App\Controller\Admin;
use App\Entity\Champions; use App\Repository\AffinityRepository; use EasyCorp\Bundle\EasyAdminBundle\Config\Action; use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField; use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField; use EasyCorp\Bundle\EasyAdminBundle\Field\DateField; use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField; use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
class ChampionsCrudController extends AbstractCrudController { private $affinityRepository; public function __construct(AffinityRepository $affinityRepository) { $this->affinityRepository = $affinityRepository; } public static function getEntityFqcn(): string { return Champions::class; }
public function configureFields(string $pageName): iterable
{
$affinity = $this->affinityRepository->findAll();
// dd($affinity);
return [
// FormField::addPanel('Détails d\'un champion'),
// BooleanField::new('isAvailable', 'En ligne')
// ->setColumns('col-sm-2 col-lg-2 col-xxl-2'),
FormField::addTab('Détails d\'un champion')
->setIcon('fa-sharp fa-solid fa-building-user')
->addCssClass('optional')
->setHelp(''),
BooleanField::new('isAvailable', 'En ligne')
->setColumns('col-sm-2 col-lg-2 col-xxl-2'),
TextField::new('name', 'Nom')
->setColumns('col-sm-2 col-lg-2 col-xxl-2'),
ChoiceField::new('tier', 'Tier')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->setChoices([
'Tier S' => 'S',
'Tier A' => 'A',
'Tier B' => 'B',
'Tier C' => 'C',
'Tier D' => 'D',
'Tier E' => 'E',
])
->hideOnIndex(),
AssociationField::new('affinity','Affinité')
->hideOnForm()
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnIndex(),
AssociationField::new('affinity','Affinité')
->setRequired(true)
->setFormTypeOption('attr', ['required' => true])
->hideOnIndex()
->hideOnDetail()
->setColumns(4)
->autocomplete(),
AssociationField::new('faction','Faction')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnIndex(),
AssociationField::new('alliance','Alliance')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnIndex(),
AssociationField::new('rarety','Rareté')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnIndex(),
AssociationField::new('aura','Aura')
->setColumns('col-sm-4 col-lg-4 col-xxl-4')
->hideOnIndex(),
AssociationField::new('role','Rôle')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnIndex(),
FormField::addTab('Statistique de base')
->setIcon('fa-sharp fa-solid fa-building-user')
->addCssClass('optional')
->setHelp(''),
IntegerField::new('hp', 'Points de vie')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('attack', 'Attaque')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('defense', 'Défense')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('speed', 'Vitesse')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('accuracy', 'Précision')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('resistance', 'Résistance')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('criticalRate', 'Taux critique')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('criticalDmg', 'Dommage Critique')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
IntegerField::new('nbBook', 'Nombre de livre')
->setColumns('col-sm-1 col-lg-1 col-xxl-1')
->hideOnIndex(),
FormField::addTab('Compétences')
->setIcon('fa-sharp fa-solid fa-building-user')
->addCssClass('optional')
->setHelp(''),
CollectionField::new('competencesChampions','Compétences du champion')
->useEntryCrudForm()
->setColumns('col-sm-12 col-lg-12 col-xxl-12')
->hideOnIndex(),
FormField::addTab('Forces et faiblesses')
->setIcon('fa-sharp fa-solid fa-building-user')
->addCssClass('optional')
->setHelp(''),
TextareaField::new('strength', 'Forces')
->hideOnIndex(),
TextareaField::new('weakness', 'Faiblesses')
->hideOnIndex(),
FormField::addTab('Visuel'),
ImageField::new('img', 'Image')
->setUploadDir('public/uploads/champions')
->setBasePath('uploads/champions')
->setUploadedFileNamePattern('[randomhash][slug]-[contenthash].[extension]')
->setRequired(false),
DateField::new('created_at', 'créer le')
->hideOnForm()
->hideOnIndex(),
TextField::new('alt', 'ALT')
->setColumns('col-sm-2 col-lg-2 col-xxl-2')
->hideOnForm()
->hideOnIndex(),
];
}
public function configureCrud(Crud $crud): Crud
{
return $crud
->setPageTitle('index', 'Gestion des champions')
->setPageTitle('new', 'Création d\'un champion')
->setPageTitle('edit', fn (Champions $champions) => sprintf('Gestion du champion : %s', $champions->getName()))
->setPageTitle('detail', fn (Champions $champions) => sprintf('Champion %s', $champions->getName()))
->setDateFormat('dd / MM / yyyy')
->setFormThemes(['admin/form.html.twig', '@EasyAdmin/crud/form_theme.html.twig']);
}
public function configureActions(Actions $actions): Actions
{
return $actions
//will add the action in the page index view
->add(Crud::PAGE_INDEX, Action::DETAIL)->setPermission(Action::DETAIL, 'ROLE_DEV')
->setPermission(Action::DELETE, 'ROLE_DEV')
->setPermission(Action::EDIT, 'ROLE_DEV')
->setPermission(Action::NEW, 'ROLE_DEV')
->update(Crud::PAGE_INDEX, Action::NEW, function (Action $action) {
return $action->setIcon('fa-solid fa-plus')->setLabel('Créer');
})
->update(Crud::PAGE_INDEX, Action::EDIT, function (Action $action) {
return $action->setIcon('fa-solid fa-pen-to-square')->setLabel('Editer');
})
->update(Crud::PAGE_INDEX, Action::DELETE, function (Action $action) {
return $action->setIcon('fa-solid fa-minus')->setLabel('Supprimer');
})
->update(Crud::PAGE_INDEX, Action::DETAIL, function (Action $action) {
return $action->setIcon('fa-solid fa-magnifying-glass')->setLabel('En savoir +');
})
->update(Crud::PAGE_INDEX, Action::BATCH_DELETE, function (Action $action) {
return $action->setIcon('fa-solid fa-minus')->setLabel('Supprimer');
})
->update(Crud::PAGE_DETAIL, Action::EDIT, function (Action $action) {
return $action->setIcon('fa-solid fa-floppy-disk')->setLabel('Éditer');
})
->update(Crud::PAGE_DETAIL, Action::INDEX, function (Action $action) {
return $action->setIcon('fa-solid fa-list')->setLabel('Retourner à la liste');
})
->update(Crud::PAGE_DETAIL, Action::DELETE, function (Action $action) {
return $action->setIcon('fa-solid fa-trash')->setLabel('Supprimer');
})
->update(Crud::PAGE_EDIT, Action::SAVE_AND_CONTINUE, function (Action $action) {
return $action->setIcon('fa-solid fa-floppy-disk')->setLabel('Sauvegarder et continuer');
})
->update(Crud::PAGE_EDIT, Action::SAVE_AND_RETURN, function (Action $action) {
return $action->setIcon('fa-solid fa-floppy-disk')->setLabel('Sauvegarder et quitter');
})
->update(Crud::PAGE_NEW, Action::SAVE_AND_ADD_ANOTHER, function (Action $action) {
return $action->setIcon('fa-solid fa-plus')->setLabel('Créer et en ajouter un autre');
})
->update(Crud::PAGE_NEW, Action::SAVE_AND_ADD_ANOTHER, function (Action $action) {
return $action->setIcon('fa-solid fa-plus')->setLabel('Créer et en ajouter un autre');
})
->update(Crud::PAGE_NEW, Action::SAVE_AND_RETURN, function (Action $action) {
return $action->setIcon('fa-solid fa-plus')->setLabel('Créer');
});
}
}
My entity :
<?php
namespace App\Entity;
use App\Repository\AffinityRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
class Affinity {
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255, unique: true)]
private ?string $name = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $created_at = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $img = null;
#[ORM\OneToMany(mappedBy: 'affinity', targetEntity: Champions::class)]
private Collection $champions;
#[ORM\OneToMany(mappedBy: 'affinity', targetEntity: Donjons::class)]
private Collection $donjons;
#[ORM\OneToMany(mappedBy: 'affinity', targetEntity: ClanBoss::class)]
private Collection $clanBosses;
public function __toString(): string
{
// return '<img src="'. $this->img .'" />';
// return '<img src="..." /> ' . $this->img;
return '<img src="uploads/affinity/ce5b1ed2fd4304f022bf7cc58b4a5e29ca2a2e46void-33f3829b970b7812cdb90afb47f5f689b3bae848.png" />' . $this->name;
}
public function __construct()
{
$this->champions = new ArrayCollection();
$this->setCreatedAt(new \DateTime());
$this->donjons = new ArrayCollection();
$this->clanBosses = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): static
{
$this->created_at = $created_at;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): static
{
$this->img = $img;
return $this;
}
/**
* @return Collection<int, Champions>
*/
public function getChampions(): Collection
{
return $this->champions;
}
public function addChampion(Champions $champion): static
{
if (!$this->champions->contains($champion)) {
$this->champions->add($champion);
$champion->setAffinity($this);
}
return $this;
}
public function removeChampion(Champions $champion): static
{
if ($this->champions->removeElement($champion)) {
// set the owning side to null (unless already changed)
if ($champion->getAffinity() === $this) {
$champion->setAffinity(null);
}
}
return $this;
}
/**
* @return Collection<int, Donjons>
*/
public function getDonjons(): Collection
{
return $this->donjons;
}
public function addDonjon(Donjons $donjon): static
{
if (!$this->donjons->contains($donjon)) {
$this->donjons->add($donjon);
$donjon->setAffinity($this);
}
return $this;
}
public function removeDonjon(Donjons $donjon): static
{
if ($this->donjons->removeElement($donjon)) {
// set the owning side to null (unless already changed)
if ($donjon->getAffinity() === $this) {
$donjon->setAffinity(null);
}
}
return $this;
}
/**
* @return Collection<int, ClanBoss>
*/
public function getClanBosses(): Collection
{
return $this->clanBosses;
}
public function addClanBoss(ClanBoss $clanBoss): static
{
if (!$this->clanBosses->contains($clanBoss)) {
$this->clanBosses->add($clanBoss);
$clanBoss->setAffinity($this);
}
return $this;
}
public function removeClanBoss(ClanBoss $clanBoss): static
{
if ($this->clanBosses->removeElement($clanBoss)) {
// set the owning side to null (unless already changed)
if ($clanBoss->getAffinity() === $this) {
$clanBoss->setAffinity(null);
}
}
return $this;
}
}
And my entity Champions :
<?php
namespace App\Entity;
use App\Repository\ChampionsRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
class Champions {
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $created_at = null;
#[ORM\Column(length: 255, unique: true)]
private ?string $name = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Affinity $affinity = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Factions $faction = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Alliance $alliance = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Rarety $rarety = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Auras $aura = null;
#[ORM\ManyToOne(inversedBy: 'champions')]
#[ORM\JoinColumn(nullable: true)]
private ?Roles $role = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $strength = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $weakness = null;
#[ORM\Column]
private ?int $nbBook = null;
#[ORM\Column]
private ?int $hp = null;
#[ORM\Column]
private ?int $attack = null;
#[ORM\Column]
private ?int $defense = null;
#[ORM\Column]
private ?int $speed = null;
#[ORM\Column]
private ?int $criticalRate = null;
#[ORM\Column]
private ?int $criticalDmg = null;
#[ORM\Column]
private ?int $resistance = null;
#[ORM\Column]
private ?int $accuracy = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $img = null;
#[ORM\OneToMany(mappedBy: 'champion', targetEntity: CompetencesChampions::class, cascade:['persist'])]
private Collection $competencesChampions;
#[ORM\Column(length: 255, nullable: true)]
private ?string $tier = null;
#[ORM\Column]
private ?bool $isAvailable = null;
#[ORM\OneToMany(mappedBy: 'champion', targetEntity: BuildChampion::class)]
private Collection $buildChampions;
#[ORM\Column(length: 255, nullable: true)]
private ?string $alt = null;
public function __toString()
{
return $this->name;
}
public function __construct()
{
$this->competencesChampions = new ArrayCollection();
$this->setAlt($this->name);
$this->setCreatedAt(new \DateTime());
$this->buildChampions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->created_at;
}
public function setCreatedAt(\DateTimeInterface $created_at): static
{
$this->created_at = $created_at;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): static
{
$this->name = $name;
return $this;
}
public function getAffinity(): ?affinity
{
return $this->affinity;
}
public function setAffinity(?affinity $affinity): static
{
$this->affinity = $affinity;
return $this;
}
public function getFaction(): ?factions
{
return $this->faction;
}
public function setFaction(?factions $faction): static
{
$this->faction = $faction;
return $this;
}
public function getAlliance(): ?Alliance
{
return $this->alliance;
}
public function setAlliance(?Alliance $alliance): static
{
$this->alliance = $alliance;
return $this;
}
public function getRarety(): ?Rarety
{
return $this->rarety;
}
public function setRarety(?Rarety $rarety): static
{
$this->rarety = $rarety;
return $this;
}
public function getAura(): ?Auras
{
return $this->aura;
}
public function setAura(?Auras $aura): static
{
$this->aura = $aura;
return $this;
}
public function getRole(): ?Roles
{
return $this->role;
}
public function setRole(?Roles $role): static
{
$this->role = $role;
return $this;
}
public function getStrength(): ?string
{
return $this->strength;
}
public function setStrength(?string $strength): static
{
$this->strength = $strength;
return $this;
}
public function getWeakness(): ?string
{
return $this->weakness;
}
public function setWeakness(?string $weakness): static
{
$this->weakness = $weakness;
return $this;
}
public function getNbBook(): ?int
{
return $this->nbBook;
}
public function setNbBook(int $nbBook): static
{
$this->nbBook = $nbBook;
return $this;
}
public function getHp(): ?int
{
return $this->hp;
}
public function setHp(int $hp): static
{
$this->hp = $hp;
return $this;
}
public function getAttack(): ?int
{
return $this->attack;
}
public function setAttack(int $attack): static
{
$this->attack = $attack;
return $this;
}
public function getDefense(): ?int
{
return $this->defense;
}
public function setDefense(int $defense): static
{
$this->defense = $defense;
return $this;
}
public function getSpeed(): ?int
{
return $this->speed;
}
public function setSpeed(int $speed): static
{
$this->speed = $speed;
return $this;
}
public function getCriticalRate(): ?int
{
return $this->criticalRate;
}
public function setCriticalRate(int $criticalRate): static
{
$this->criticalRate = $criticalRate;
return $this;
}
public function getCriticalDmg(): ?int
{
return $this->criticalDmg;
}
public function setCriticalDmg(int $criticalDmg): static
{
$this->criticalDmg = $criticalDmg;
return $this;
}
public function getResistance(): ?int
{
return $this->resistance;
}
public function setResistance(int $resistance): static
{
$this->resistance = $resistance;
return $this;
}
public function getAccuracy(): ?int
{
return $this->accuracy;
}
public function setAccuracy(int $accuracy): static
{
$this->accuracy = $accuracy;
return $this;
}
public function getImg(): ?string
{
return $this->img;
}
public function setImg(?string $img): static
{
$this->img = $img;
return $this;
}
/**
* @return Collection<int, CompetencesChampions>
*/
public function getCompetencesChampions(): Collection
{
return $this->competencesChampions;
}
public function addCompetencesChampion(CompetencesChampions $competencesChampion): static
{
if (!$this->competencesChampions->contains($competencesChampion)) {
$this->competencesChampions->add($competencesChampion);
$competencesChampion->setChampion($this);
}
return $this;
}
public function removeCompetencesChampion(CompetencesChampions $competencesChampion): static
{
if ($this->competencesChampions->removeElement($competencesChampion)) {
// set the owning side to null (unless already changed)
if ($competencesChampion->getChampion() === $this) {
$competencesChampion->setChampion(null);
}
}
return $this;
}
public function getTier(): ?string
{
return $this->tier;
}
public function setTier(?string $tier): static
{
$this->tier = $tier;
return $this;
}
public function isIsAvailable(): ?bool
{
return $this->isAvailable;
}
public function setIsAvailable(bool $isAvailable): static
{
$this->isAvailable = $isAvailable;
return $this;
}
/**
* @return Collection<int, BuildChampion>
*/
public function getBuildChampions(): Collection
{
return $this->buildChampions;
}
public function addBuildChampion(BuildChampion $buildChampion): static
{
if (!$this->buildChampions->contains($buildChampion)) {
$this->buildChampions->add($buildChampion);
$buildChampion->setChampion($this);
}
return $this;
}
public function removeBuildChampion(BuildChampion $buildChampion): static
{
if ($this->buildChampions->removeElement($buildChampion)) {
// set the owning side to null (unless already changed)
if ($buildChampion->getChampion() === $this) {
$buildChampion->setChampion(null);
}
}
return $this;
}
public function getAlt(): ?string
{
return $this->alt;
}
// public function setAlt(string $alt): static
// {
// $this->alt = $alt;
// return $this;
// }
public function setAlt(): static
{
$this->alt = $this->getName();
return $this;
}
} `
I'm in 4.6.6, maybe that. I will try in 4.8
It's a bug, 4.6.6 works 4.8.12 doesn't work
I gonna try with 4.6.6
yeah it works in 4.6.6, thanks you ! I love you :D
Thanks you !
Hi ! I search to insert an image into a select. I use easyadmin 4.8 and symfony 6. I've an entity Champions with manytomany affinity and i need to have a visual of the affinity in my form. I call affinity with AssociationField How I do that ? Thanks a lot for your response :D