这不是完全你想要的答案,因为我是用一个选择字段来做的,这是一种解决方法。这是一个表单,您可以在其中选择发送消息的接收者(=campaign):
在表单类型中:
公共功能 __construct(EntityManager $em, Campaign $campaign) {
$this->campaign = $campaign;
$this->em = $em;
}
公共函数 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('主题')
->添加('消息','文本区域')
->添加('recipientContacts','选择',数组(
'必需' => 假,
'multiple' => true, // 管理多项选择
'选择' => $contactChoices,
'property_path' => 假,
))
...
然后在控制器中:检索已发布的联系人并将其分配给活动:
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>');
}