Symfony form: filter collection with a parameter
up vote
1
down vote
favorite
I am using Symfony 3.4.
I have a form to edit some answers for a given country and I would need to edit these answers on a per category basis.
I have setup a filter in my entity but I don't know how to call this getter with a parameter from my form.
The dirty alternative would be to remove non required elements from my form in my controller but this is not ideal...
Here is my form:
<?php
namespace cwtpsmdbBundleForm;
use cwtpsmdbBundleEntityCcdbServicesAnswers;
use cwtpsmdbBundleEntitycountries;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
class countriesCCDBServicesAnswersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('AnswersOfCategory', CollectionType::class, array(
'entry_type' => CcdbServicesAnswersType::class,
'entry_options' => array('label' => false,),
'label' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'cwtpsmdbBundleEntitycountries',
'categoryID' => 1,
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'cwt_psmdbbundle_countries';
}
}
Here is my entity:
<?php
namespace cwtpsmdbBundleEntity;
use APYDataGridBundleGridMapping as GRID;
use DoctrineORMMapping as ORM;
/**
* countries
*
* @ORMTable()
* @ORMEntity(repositoryClass="cwtpsmdbBundleEntityRepositorycountriesRepository")
*/
class countries
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="code", type="string", length=2)
*/
private $code;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="cwtpsmdbBundleEntityCcdbServicesAnswers", mappedBy="country", cascade={"persist", "remove"})
*/
private $ccdbServicesAnswers;
// Specific Functions
/**
* Get ccdbServicesAnswers.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAnswersOfCategory($categoryID)
{
return $this->getCcdbServicesAnswers()->filter(function(CcdbServicesAnswers $answer) use($categoryID) {
return $answer->getQuestion()->getCategory()->getId() == $categoryID;
});
}
And here is my create form function in my controller:
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId())),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
[UPDATE 20/11/2018]
Here is what I did so far. This work but I would prefer a cleaner solution which deals with this filter with a where clause at database level and which would be more DRY. Doing this at the conroller level means that I have to do it everywhere I need this filter.
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId(), 'categoryID' => $categoryID)),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$answers = $form->get('ccdbServicesAnswers')->getData();
foreach ($answers as $answer) {
if($answer->getQuestion()->getCategory()->getId() != $categoryID) {
$answers->removeElement($answer);
}
}
$form->get('ccdbServicesAnswers')->setData($answers);
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
symfony doctrine2 symfony-forms
add a comment |
up vote
1
down vote
favorite
I am using Symfony 3.4.
I have a form to edit some answers for a given country and I would need to edit these answers on a per category basis.
I have setup a filter in my entity but I don't know how to call this getter with a parameter from my form.
The dirty alternative would be to remove non required elements from my form in my controller but this is not ideal...
Here is my form:
<?php
namespace cwtpsmdbBundleForm;
use cwtpsmdbBundleEntityCcdbServicesAnswers;
use cwtpsmdbBundleEntitycountries;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
class countriesCCDBServicesAnswersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('AnswersOfCategory', CollectionType::class, array(
'entry_type' => CcdbServicesAnswersType::class,
'entry_options' => array('label' => false,),
'label' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'cwtpsmdbBundleEntitycountries',
'categoryID' => 1,
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'cwt_psmdbbundle_countries';
}
}
Here is my entity:
<?php
namespace cwtpsmdbBundleEntity;
use APYDataGridBundleGridMapping as GRID;
use DoctrineORMMapping as ORM;
/**
* countries
*
* @ORMTable()
* @ORMEntity(repositoryClass="cwtpsmdbBundleEntityRepositorycountriesRepository")
*/
class countries
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="code", type="string", length=2)
*/
private $code;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="cwtpsmdbBundleEntityCcdbServicesAnswers", mappedBy="country", cascade={"persist", "remove"})
*/
private $ccdbServicesAnswers;
// Specific Functions
/**
* Get ccdbServicesAnswers.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAnswersOfCategory($categoryID)
{
return $this->getCcdbServicesAnswers()->filter(function(CcdbServicesAnswers $answer) use($categoryID) {
return $answer->getQuestion()->getCategory()->getId() == $categoryID;
});
}
And here is my create form function in my controller:
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId())),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
[UPDATE 20/11/2018]
Here is what I did so far. This work but I would prefer a cleaner solution which deals with this filter with a where clause at database level and which would be more DRY. Doing this at the conroller level means that I have to do it everywhere I need this filter.
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId(), 'categoryID' => $categoryID)),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$answers = $form->get('ccdbServicesAnswers')->getData();
foreach ($answers as $answer) {
if($answer->getQuestion()->getCategory()->getId() != $categoryID) {
$answers->removeElement($answer);
}
}
$form->get('ccdbServicesAnswers')->setData($answers);
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
symfony doctrine2 symfony-forms
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am using Symfony 3.4.
I have a form to edit some answers for a given country and I would need to edit these answers on a per category basis.
I have setup a filter in my entity but I don't know how to call this getter with a parameter from my form.
The dirty alternative would be to remove non required elements from my form in my controller but this is not ideal...
Here is my form:
<?php
namespace cwtpsmdbBundleForm;
use cwtpsmdbBundleEntityCcdbServicesAnswers;
use cwtpsmdbBundleEntitycountries;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
class countriesCCDBServicesAnswersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('AnswersOfCategory', CollectionType::class, array(
'entry_type' => CcdbServicesAnswersType::class,
'entry_options' => array('label' => false,),
'label' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'cwtpsmdbBundleEntitycountries',
'categoryID' => 1,
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'cwt_psmdbbundle_countries';
}
}
Here is my entity:
<?php
namespace cwtpsmdbBundleEntity;
use APYDataGridBundleGridMapping as GRID;
use DoctrineORMMapping as ORM;
/**
* countries
*
* @ORMTable()
* @ORMEntity(repositoryClass="cwtpsmdbBundleEntityRepositorycountriesRepository")
*/
class countries
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="code", type="string", length=2)
*/
private $code;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="cwtpsmdbBundleEntityCcdbServicesAnswers", mappedBy="country", cascade={"persist", "remove"})
*/
private $ccdbServicesAnswers;
// Specific Functions
/**
* Get ccdbServicesAnswers.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAnswersOfCategory($categoryID)
{
return $this->getCcdbServicesAnswers()->filter(function(CcdbServicesAnswers $answer) use($categoryID) {
return $answer->getQuestion()->getCategory()->getId() == $categoryID;
});
}
And here is my create form function in my controller:
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId())),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
[UPDATE 20/11/2018]
Here is what I did so far. This work but I would prefer a cleaner solution which deals with this filter with a where clause at database level and which would be more DRY. Doing this at the conroller level means that I have to do it everywhere I need this filter.
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId(), 'categoryID' => $categoryID)),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$answers = $form->get('ccdbServicesAnswers')->getData();
foreach ($answers as $answer) {
if($answer->getQuestion()->getCategory()->getId() != $categoryID) {
$answers->removeElement($answer);
}
}
$form->get('ccdbServicesAnswers')->setData($answers);
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
symfony doctrine2 symfony-forms
I am using Symfony 3.4.
I have a form to edit some answers for a given country and I would need to edit these answers on a per category basis.
I have setup a filter in my entity but I don't know how to call this getter with a parameter from my form.
The dirty alternative would be to remove non required elements from my form in my controller but this is not ideal...
Here is my form:
<?php
namespace cwtpsmdbBundleForm;
use cwtpsmdbBundleEntityCcdbServicesAnswers;
use cwtpsmdbBundleEntitycountries;
use DoctrineORMEntityRepository;
use SymfonyBridgeDoctrineFormTypeEntityType;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeCollectionType;
class countriesCCDBServicesAnswersType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('AnswersOfCategory', CollectionType::class, array(
'entry_type' => CcdbServicesAnswersType::class,
'entry_options' => array('label' => false,),
'label' => false,
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'cwtpsmdbBundleEntitycountries',
'categoryID' => 1,
));
}
/**
* @return string
*/
public function getBlockPrefix()
{
return 'cwt_psmdbbundle_countries';
}
}
Here is my entity:
<?php
namespace cwtpsmdbBundleEntity;
use APYDataGridBundleGridMapping as GRID;
use DoctrineORMMapping as ORM;
/**
* countries
*
* @ORMTable()
* @ORMEntity(repositoryClass="cwtpsmdbBundleEntityRepositorycountriesRepository")
*/
class countries
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="code", type="string", length=2)
*/
private $code;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=100)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="cwtpsmdbBundleEntityCcdbServicesAnswers", mappedBy="country", cascade={"persist", "remove"})
*/
private $ccdbServicesAnswers;
// Specific Functions
/**
* Get ccdbServicesAnswers.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAnswersOfCategory($categoryID)
{
return $this->getCcdbServicesAnswers()->filter(function(CcdbServicesAnswers $answer) use($categoryID) {
return $answer->getQuestion()->getCategory()->getId() == $categoryID;
});
}
And here is my create form function in my controller:
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId())),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
[UPDATE 20/11/2018]
Here is what I did so far. This work but I would prefer a cleaner solution which deals with this filter with a where clause at database level and which would be more DRY. Doing this at the conroller level means that I have to do it everywhere I need this filter.
private function createCountryEditForm(Countries $entity, $categoryID)
{
$form = $this->createForm(countriesCCDBServicesAnswersType::class, $entity, array(
'action' => $this->generateUrl('updateCountryCcdbServicesAnswers', array('countryID' => $entity->getId(), 'categoryID' => $categoryID)),
'method' => 'PUT',
'categoryID' => $categoryID,
));
$answers = $form->get('ccdbServicesAnswers')->getData();
foreach ($answers as $answer) {
if($answer->getQuestion()->getCategory()->getId() != $categoryID) {
$answers->removeElement($answer);
}
}
$form->get('ccdbServicesAnswers')->setData($answers);
$form->add('submit', submitType::class, array('label' => 'Update', 'attr' => array('class' => 'hidden')));
return $form;
}
symfony doctrine2 symfony-forms
symfony doctrine2 symfony-forms
edited Nov 20 at 16:06
asked Nov 19 at 19:13
curuba
338318
338318
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381168%2fsymfony-form-filter-collection-with-a-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown