-1
我只會顯示與住所相關的用戶,這裏是我的錯誤。錯誤FormType Symfony
值爲「username」的選項「options」預期爲類型「null」或「array」或「\ Traversable」,但類型爲「string」。
我FormType:
<?php
namespace AppBundle\Form;
use AppBundle\Entity\User;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BadgeType extends AbstractType
{
/**
* @var bool $admin
*/
private $admin;
/**
* @var User $user
*/
private $user;
/**
* DemandeType constructor.
* @param bool|false $admin
*/
public function __construct($admin = false, $user)
{
$this->admin = $admin;
$this->user = $user;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$residences = $this->user->getResidences();
$builder
->add('tag')
->add('typeBadge', ChoiceType::class, array(
'choices' => array(
'Parking' => 'Parking',
'SAS' => 'SAS'
)
))
->add('dateCreate')
->add('active');
if ($this->admin){
$builder->add('user', EntityType::class, array(
'class' => 'AppBundle\Entity\User',
'multiple' => false,
'required' => true
));
} else {
$builder->add('user', EntityType::class, array(
'class' => 'AppBundle\Entity\User',
'choices' => 'username'
));
};
if ($this->admin) {
$builder->add('residence', 'entity', array(
'label' => 'Residence',
'property' => 'name',
'multiple' => true,
'required' => true,
'expanded' => false,
'class' => 'AppBundle\Entity\Residence'
));
} else {
$builder->add('residence', EntityType::class, array(
'class' => 'AppBundle:Residence',
'label' => 'Residence',
'property' => 'name',
'multiple' => true,
'required' => true,
'expanded' => false,
'choices' => $residences,
));
};
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Badge'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_badge';
}
謝謝。
它的工作原理。現在我只需要顯示住所的使用者 – CHRISCOM