[英]How to work with entity form field type, and JUI autocomplete in Symfony2?
我有表单,我有entity
字段类型,使用户可以选择相关的Client
实体。 它在开发环境中运行良好,但在生产中将有数千个客户端可供选择,HTML表单字段类型将无法处理此问题。
我写过动作巫婆使用Zend Lucene并以JSON格式返回JUI自动完成的客户端,我如何使用entity
表单字段类型启用此自动完成?
这不正是你想要的答案,因为我有一个选择字段做了,而且它是一种工作的周围。 这是一个表单,您可以选择接收方发送消息(=广告系列):
在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>');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.