How do I check for a selection, fetched from the database and then translated into symfony2?

** update **

I somehow found a way to set a select validator callback function:

Part of my FormType.php:

\\ ABCFormType.php
$builder
    ->add('categories', 'entity', array(
        'class' => 'ABCCommonBundle:Category',
        'query_builder' => function(\path\to\my\custom\repository\CategoryRepository $repo) {
            return $repo->findBaseLevel();
        },
        'expanded' => true,
        'multiple' => true, ));

      

Part of my custom repository:

\\ CategoryRepository.php
class CategoryRepository extends NestedTreeRepository
{
    private static $baseLevel = null;

    public function findCategoriesOfLevel($level = 0)
    {
        return $this->createQueryBuilder('p')
                    ->where('p.lvl = :level')
                    ->setParameter('level', $level)
                    ->orderBy('p.id', 'ASC');

    }

    public function __construct(EntityManager $em, ClassMetadata $class)
    {
        parent::__construct($em, $class);
        if (null == self::$baseLevel) {
            self::$baseLevel = $this->findBaseLevel()->getQuery()->getResult();
        }
    }

    public function findBaseLevel()
    {
        return $this->findCategoriesOfLevel(0);
    }

    public static function getBaseLevel()
    {
        return self::$baseLevel;
    }

      

I now have a static method, so I configured it in the callback option of the select validator:

/**
 * @ORM\ManyToMany(targetEntity="path\to\Entity\Category")
 * @Assert\Choice(callback="\path\to\CategoryRepository::getBaseLevel", min="1")
 * 
 * @var Category
 */
protected $categories;

      

It has now been confirmed that the correct "English" array has been retrieved from the database, but since I translated the English category names into Chinese using the symfony translation service, so although I selected some options, the validation always failed !!! Please, help.

+3


source to share


1 answer


There is really no need to add a Choice constraint to your model property. The NotBlank limitation is enough to get the job done. If you have a select box type or any other that inherits from it (object field type inherits from selection), it will automatically check if the selected object is selected in the selected options or not.

/**
* @ORM\ManyToMany(targetEntity="path\to\Entity\Category")
* @Assert\NotBlank()
* 
* @var Category
*/
protected $categories;

      

So your form might look like this:

$builder
    ->add('categories', 'entity', array(
        'class' => 'ABCCommonBundle:Category',
        'query_builder' => function(\path\to\my\custom\repository\CategoryRepository $repo) {
            return $repo->findBaseLevel();
        },
        'expanded' => true,
        'multiple' => true,
        'property' => 'title',
    )
);

      



You can read more about the property parameter here: http://symfony.com/doc/current/reference/forms/types/entity.html#choice-label

Now the displayed checkboxes will look like this:

<input type="checkbox" value="1" /> CategoryTitle

If the value attribute contains the category's primary key (it will probably be the ID), and instead of CategoryTitle, you will see the value translated to Category :: title.

+1


source







All Articles