0
votes

I don't know how to validate a checkbox with a @Assert.

Returns a string "1" if checked otherwise false

Step 1: create a class: SearchPlayerData.php to test my data (without ORM) This form is not linked to a class

<?php

namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;

class SearchPlayerData
{
     /**
     * (length=255, nullable=true) 
     * @Assert\???
     */
    public $tgg;

     /**
     * (length=255, nullable=true)
     * @Assert\???
     */
    public $selection;
   ...

Step 2: SeachPlayerType

<?php
namespace App\Form;

use app\Entity\SearchPlayerData;

class SearchMultiType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {

     $builder
            ->add('tgg', CheckboxType::class, [
                'label'             => "TGG", 
                'required'          => false,                
                'attr' => [
                    "class"         => "text-dark checkbox", 
                ], 
            ->add('selection', CheckboxType::class, [
                'label'             => "SELECTION", 
                'required'          => false,                
                'attr' => [
                    "class"         => "text-dark checkbox", 
                ], 
               ...
    
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            "data_class"    => SearchPlayerData::class
        ]);
    }

Etape 3 : Controller

    public function searchPlayers(Request $request, PlayerRepository $playerRepository)
    {
        $searchPlayerData = new SearchPlayerData(); 

        $form = $this->createForm(SearchPlayerType::class, $searchPlayerData, ["sect" => $sect]);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
        ....

Do you have an idea or another solution. I would like to keep the checkbox field if possible

Thanks for your help

2

2 Answers

0
votes

You could use @Assert\Choice with possible values.

namespace App\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class SearchPlayerData
{
    /**
     * (length=255, nullable=true)
     * @Assert\Choice(callback={"App\Entity\SearchPlayerData", "getCheckboxAllowedValues"})
     */
    public $tgg;

    /**
     * (length=255, nullable=true)
     * @Assert\Choice(callback={"App\Entity\SearchPlayerData", "getCheckboxAllowedValues"})
     */
    public $selection;

    public static function getCheckboxAllowedValues()
    {
        return [1, false];
    }
}
0
votes

Thanks,

But, there is something that I do not understand. When i do :

 $data = $form->getData();
 var_dump($data);


output : 
["tgg"]=>
  bool(false)
["selection"]=>
  bool(true) if checked

return a boolean!!!

ps: it's different from a var_dump ($ _ POST). I understood this. (return false or a string "1"];

So I fill in the values ​​true and false in function getCheckboxAllowedValues(). Ok it works;

However, why when in the inspector I change the value of the checkbox value = "%%%%", there is no error message. It validates the field to TRUE if the checkbox is checked and FALSE if it is not. Does this mean that value is never read?