如何在 Symfony2 中使用实体表单字段类型和 JUI 自动完成功能?

发布于 2024-12-09 02:51:50 字数 232 浏览 0 评论 0原文

我有一个表单,其中有 entity 字段类型,它使用户能够选择相关的 Client 实体。它在开发环境中运行良好,但在生产环境中将有数千个客户端可供选择,而 HTML 表单字段类型将无法处理此问题。

我已经编写了使用 Zend Lucene 的操作并以 JSON 格式返回客户端以进行 JUI 自动完成,如何使用 entity 表单字段类型启用此自动完成?

I have form where I have entity field type, witch enables user to choose the related Client entity. It works great in dev environment but in production there will be thousands of clients to choose from, and HTML form field types will not be able to handle this.

I've written action witch uses Zend Lucene and returns clients in JSON format for JUI autocomplete, how I can enable this autocomplete with entity form field type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-12-16 02:51:50

这并不完全是您想要的答案,因为我是通过选择字段完成的,这是一种解决方法。这是一个表单,您可以在其中选择发送消息的接收者(=营销活动):

在 FormType 中:

    public function __construct(EntityManager $em, Campaign $campaign) {
        $this->campaign = $campaign;
        $this->em = $em;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $contactChoices = array('0'=>'');
        if($this->campaign && $this->campaign->getRecipientContacts()){
            foreach($this->campaign->getRecipientContacts() as $contact){
                $contactChoices[$contact->getHash()] = $contact->getName();
            }
        }
        $builder->add('subject')
                ->add('message','textarea')
                ->add('recipientContacts','choice', array(
                        'required' => false,
                        'multiple' => true, // manage multiple choices
                        'choices' => $contactChoices,
                        'property_path' => false,
                    ))
                ...

然后在控制器中:检索发布的联系人并将其分配给营销活动:

       if($this->getRequest()->getMethod() == 'POST'){
            $campaign->removeRecipientContacts();
            $data = $this->getRequest()->get('campaignForm');

            if(isset($data['recipientContacts'])){
                foreach($data['recipientContacts'] as $hash){
                   $contact = $this->getRepo()->getContactByHash($hash);
                   $campaign->addRecipientContact($contact);
                }
            }
        }

这允许您使用任何 JS 小部件(自动完成、. ..) 在前端。只需将选项添加到您的选择字段即可。有点儿:

function addContact(hash,name){
     $('#campaignContactChoiceSelectField').append('<option value="'+hash+'">'+name+'</option>');
}

This is not exactly the answer you want, cause I did it with a choice field, and it's kind of a work around. This is a form where you can select receivers for sending a message(=campaign):

In the FormType:

    public function __construct(EntityManager $em, Campaign $campaign) {
        $this->campaign = $campaign;
        $this->em = $em;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        $contactChoices = array('0'=>'');
        if($this->campaign && $this->campaign->getRecipientContacts()){
            foreach($this->campaign->getRecipientContacts() as $contact){
                $contactChoices[$contact->getHash()] = $contact->getName();
            }
        }
        $builder->add('subject')
                ->add('message','textarea')
                ->add('recipientContacts','choice', array(
                        'required' => false,
                        'multiple' => true, // manage multiple choices
                        'choices' => $contactChoices,
                        'property_path' => false,
                    ))
                ...

Then in the controller: retrieve the posted contacts and assign them to the campaign:

       if($this->getRequest()->getMethod() == 'POST'){
            $campaign->removeRecipientContacts();
            $data = $this->getRequest()->get('campaignForm');

            if(isset($data['recipientContacts'])){
                foreach($data['recipientContacts'] as $hash){
                   $contact = $this->getRepo()->getContactByHash($hash);
                   $campaign->addRecipientContact($contact);
                }
            }
        }

This allows you to use whatever JS widget (autocomplete,...) on the frontend. Simply add options to your choice field. Kind of:

function addContact(hash,name){
     $('#campaignContactChoiceSelectField').append('<option value="'+hash+'">'+name+'</option>');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文