Here is my problem.
I use the buildForm
method on symfony 2.1 in order to build my form.
With the following code everything works just fine :
$builder->add('combat','entity',array(
class' => 'KarateCompetitionBundle:CompetitionCombat',
'empty_value' => 'Sélectionner un combat'));
But I want to filter and display only some Combat
. That's why I have to use the query_builder
option. When i do that i get the This value is not valid
error message.
Here is the code :
$builder->add('combat','entity',array(
'class' => 'KarateCompetitionBundle:CompetitionCombat',
'empty_value' => 'Sélectionner un combat',
'query_builder' => function(CombatRepository $cr) {
return $cr->getAllWithoutBilanQueryBuilder();}));
I reduce at the minimum the code (i.e. no filtering on the getAllWithoutBilanQueryBuilder
method) in order to be able to find the problem.
public function getAllWithoutBilanQueryBuilder(){
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
return $queryBuilder->select('c')->from('KarateEntrainementBundle:CompetitionCombat', 'c');
}
I've compared both html code generate on each case and they are the same.
I put a var_dump($object)
on the controller after binding the form with the request $form->bind($request)
and it appears that when i use the query_builder
option the combat
is null while it is not null if i don't use it.
I can't manage to understand why ? I found few post on the web with the same issue but none with an answer. Is it possible that there is a symfony issue here or am I doing something wrong ?
$this->Entity
is a global variable in order not to repeat each time. I edit my question and replace it for better understanding. Actually, I know thequerybuilder
is correct because my form is well generated. I have the list of combats. But when i select one, and validate the form then I have the error message. What is weird is that if i put thequery_builder
property or if I don't, both html codes are the same. But without thequery_builder
property I can select a combat and validate and there is no error. – Separates$this->getEntityManager
from a custom repository that extendsEntityRepository
– SeparatesCombat
entity :UniqueConstraint(columns={"idCompetition", "idCategorie","tour"})})
on the table ;@UniqueEntity(fields={"competition","categorie","tour"},message="my error message")
and@Assert\Callback(methods={"isValid"})
on the class. Withpublic function isValid(ExecutionContext $context){if ($this->finale && $this->demiFinale){$context->addViolation('message erreur', array(), null);}}
– Separates