我有兩個類User.php
和Group.php
,擴展自fosuserbundle中定義的類。實體類型字段的默認選擇選項
除此之外,我有他們之間的ManyToMany
關係。
該應用程序的想法是當我添加一個新用戶,我分配給用戶一個或多個組(我正在使用材料設計多選擇選項)。當用戶沒有分配任何組時(例如,當組選項爲空時)出現問題。我在控制檯中得到這個javascript錯誤:
name ='user [groups] []'的無效窗體控件不可聚焦。
所以要解決此問題,我需要爲組字段提供默認值。如果未選擇組,則如何設置默認用戶組?我的課程定義如下。
UserType.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'username',
null,
array(
'attr' => array(
'placeholder' => 'username'
),
))
->add(
'email',
null,
array(
'attr' => array(
'placeholder' => 'email'
),
))
/*->add('groups', EntityType::class, array(
'class' => 'AppBundle\Entity\Group',
'choice_label' => 'name',
'expanded' => false,
'multiple' => false
)) */
->add('groups', EntityType::class, array(
'class' => 'AppBundle\Entity\Group',
'choice_label' => 'name',
'attr'=>array(
'class' => 'mdb-select'
),
'multiple' => true,
))
->add('plainPassword', RepeatedType::class, array(
'invalid_message' => 'Les mots de passe doivent être identiques.',
'first_options' => array('label' => 'Mot de passe'),
'second_options' => array('label' => 'Répétez le mot de passe'),
))
//<input id="input-id" type="file" class="file" multiple data-show-upload="false" data-show-caption="true">
->add('enabled', null, array(
'required' => false,
))
->add('imageFile',VichFileType::class, [
'required' => false,
'download_link' => true,
'attr'=>array(
'type' => 'file',
'onchange' => 'loadFile(event)'), // not mandatory, default is true
])
;
}
GroupEntity.php
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('roles', CollectionType::class, array(
'entry_type' => ChoiceType::class,
'entry_options' => array(
'attr' => array(
'class'=>'mdb-select colorful-select dropdown-default'
),
'choices' => array(
'Admin' => 'ROLE_ADMIN',
'USER' => 'ROLE_USER',
),
)
))
;
}
user.php的
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @Vich\Uploadable
* @ORM\Entity
* @ORM\Table(name="fos_user")
* @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used", groups={"Default", "Registration", "Profile"})
* @UniqueEntity(fields="emailCanonical", errorPath="email", message="fos_user.email.already_used", groups={"Default", "Registration", "Profile"})
*/
class User extends BaseUser {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $id;
/**
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Group", inversedBy="users", cascade={"remove"})
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
*)
*/
protected $groups;
/**
* @ORM\Column(type="integer", length=6, options={"default":0})
*/
protected $loginCount = 0;
/**
* @var \DateTime
*
* @ORM\Column(type="datetime", nullable=true)
*/
protected $firstLogin;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="user_image", fileNameProperty="imageName")
*
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="string", length=255,nullable=true)
*
* @var string
*/
private $imageName;
/**
* @ORM\Column(type="datetime",nullable=true)
*
* @var \DateTime
*/
private $updatedAt;
public function __construct() {
parent::__construct();
$this->enabled = true;
$this->groups = new ArrayCollection();
}
}
Group.php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_group")
*/
class Group extends BaseGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\User", mappedBy="groups")
*
*
*/
protected $users;
/**
* @var string
*/
protected $name;
/**
* @var array
*/
protected $roles;
/**
* Group constructor.
*
* @param string $name
* @param array $roles
*/
public function __construct($name, $roles = array())
{
$this->name = $name;
$this->roles = $roles;
}
}
感謝您的u'r效應初探,我想牛逼說,我在我的控制檯中的JavaScript錯誤: 新:1名無效表單控件= '用戶[組] []'不可聚焦。 – ahmedbhs
奇怪的嘗試,看看有關的腳本,如果你設法找到錯誤來自哪裏。不應該與你的表單監聽器有關。你可能應該問另一個問題。請考慮接受和/或upvoting我的答案,如果你發現它有幫助,以便其他人可以從中受益。 – Mawcel
完成,並感謝您的回覆,我真的很感謝您的幫助。 – ahmedbhs