gpt4 book ai didi

forms - 扩展 EntityType,仅将选择设置为选定的实体

转载 作者:行者123 更新时间:2023-12-05 06:41:31 26 4
gpt4 key购买 nike

我已将 Symfony 的 EntityType 扩展为 UserChooserType 以用于我的 User 实体和 Select2。 UserChooserType 的选择列表来自 ldap 查询(通过 ajax 调用),而不是 Doctrine 查询。所以该字段一开始是空白的。

用户实体与我的应用程序中的许多不同实体相关。但是,如果我希望 UserChooserType 加载 当前 所选用户,我必须向使用它的每个表单添加一个监听器。例如:

class SiteType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$siteAdminOpts = array('label' => 'entity.site.admin', 'required'=>false);
//opts for the UserChooserType

$builder
->add('siteName', FT\TextType::class, array('label' => 'entity.site.name'))
->add('siteAdmin', UserChooserType::class, $siteAdminOpts )

//must be added to every form type that uses UserChooserType with mod for the datatype that $event->getData() returns
->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$site = $event->getData();
$form = $event->getForm(); //SiteType

if($user = $site->getSiteAdmin()) $siteAdminOpts['choices'] = array($user);
$form->add('siteAdmin', UserChooserType::class, $siteAdminOpts);
});
}
//etc.

长话短说;

我愿意:

  • UserChooserTypechoices 选项设置为在 UserChooserType::configureOptions() 中选择的用户,或者
  • ->addEventListener(...) 移动到 UserChooserType::buildForm() 中。

知道如何实现吗?


这是 UserChooserType:

class UserChooserType extends AbstractType
{
/**
* @var UserManager
*/
protected $um;

/**
* UserChooserType constructor.
* @param UserManager $um
*/
public function __construct(UserManager $um){
$this->um = $um; //used to find and decorate User entities. It is not a Doctrine entity manager, but it uses one.
}

/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder, array $options) {

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();

if (!$data) return;

$user = $this->um->getUserByUserName($data);
if(!$user->getId()) $this->um->saveUser($user); //create User in db, if it's not there yet.
});

$builder->resetViewTransformers(); //so new choices aren't discarded

$builder->addModelTransformer(new CallbackTransformer(
function ($user) { //internal storage format to display format
return ($user instanceof User) ? $user->getUserName() : '';
},
function ($username) { //display format to storage format
return ($username) ? $this->um->getUserByUserName($username) : null;
}
));
}


/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'class' => 'ACRDUserBundle:User',
'label' => 'ldap.user.name',
'choice_label' => function($user, $key, $index){
$this->um->decorateUser($user);
$label = $user->getDetail('displayName');
return $label ? $label : $user->getUserName();
},
'choice_value' => 'userName',
'choices' => [],
'attr' => array(
'class' => 'userchooser',
'placeholder' => 'form.placeholder.userchooser'
)

));
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'my_userchooser';
}

/**
* @inheritDoc
*/
public function getParent() {
return EntityType::class;
}
}

最佳答案

这帮助了我: https://symfony.com/doc/current/reference/forms/types/entity.html#using-choices

以下是我的使用方式(同样使用 select 2):

public function buildForm(FormBuilderInterface $builder, array $options)
{
/** @var ProductCollection $productCollection */
$productCollection = $builder->getData();

$builder
->add('products', EntityType::class,
[
'class' => Product::class,
'required' => FALSE,
'expanded' => FALSE,
'multiple' => TRUE,
'choices' => $productCollection->getProducts(), // We only preload the selected products. The rest come from api.
'attr' => [
'data-toggle' => 'select',
'data-options' => json_encode([
'ajax' => [
'url' => '/admin/product/autocomplete',
'dataType' => 'json'
]
])
],
'choice_label' => function (Product $product) {
return $product->getName();
},
'choice_attr' => function (Product $product) {
return [
'data-avatarsrc' => $product->getImage()->getUrl(),
'data-caption' => $product->getSkus()->first()->getSku(),
];
},
]
)
;
}

这是我用于 select2 初始化的 JS(复制/粘贴):

function() {
var elements = document.querySelectorAll('[data-toggle="select"]');

function templateResult(element) {
let avatarsrc, caption;

if (element.id && element.avatarsrc) {
avatarsrc = element.avatarsrc
caption = element.caption ? ' ' + element.caption : ''
}
else if (element.element) {
avatarsrc = element.element.dataset.avatarsrc ? element.element.dataset.avatarsrc : ''
caption = element.element.dataset.caption ? ' ' + element.element.dataset.caption : ''
}

if (!avatarsrc) {
return element.text + caption
}

let wrapper = document.createElement("div");
return wrapper.innerHTML = '<span class="avatar avatar-xs mr-3 my-2"><img class="avatar-img rounded-circle" src="' + avatarsrc + '" alt="' + element.text + '"></span><span>' + element.text + '</span><span class="badge badge-soft-success ml-2">' + caption + '</span>', wrapper
}

jQuery().select2 && elements && [].forEach.call(elements, function(element) {
var select, additionalOptions, select2options;

additionalOptions = (select = element).dataset.options ? JSON.parse(select.dataset.options) : {};

select2options = {
containerCssClass: select.getAttribute("class"),
dropdownCssClass: "dropdown-menu show",
dropdownParent: select.closest(".modal") ? select.closest(".modal") : document.body,
templateResult: templateResult,
templateSelection: templateResult
}

$(select).select2({...select2options, ...additionalOptions})
})
}(),

关于forms - 扩展 EntityType,仅将选择设置为选定的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40294293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com