duanfaxin7014 2013-07-20 13:29
浏览 34

FOSMessageBundle - 如何在没有字段表单收件人的情况下发送邮件

I've hide my recipient field form and now I would like to understand where in the controller I can tell the value of the recipient

MessageController :

/**
 * Create a new message thread
 *
 * @return Response
 */
public function newThreadAction()
{
    $form = $this->container->get('fos_message.new_thread_form.factory')->create();
    $formHandler = $this->container->get('fos_message.new_thread_form.handler');

    if ($message = $formHandler->process($form)) {
        return new RedirectResponse($this->container->get('router')->generate('fos_message_thread_view', array(
            'threadId' => $message->getThread()->getId()
        )));
    }

    return $this->container->get('templating')->renderResponse('FOSMessageBundle:Message:newThread.html.twig', array(
        'form' => $form->createView(),
        'data' => $form->getData()
    ));
}

$form math to :

class NewThreadMessageFormFactory extends AbstractMessageFormFactory
{
    /**
     * Creates a new thread message
     *
     * @return Form
     */
    public function create()
    {
        $message = $this->createModelInstance();

        return $this->formFactory->createNamed($this->formName, $this->formType, $message);
    }
}

$formHandler match to :

class NewThreadMessageFormHandler extends AbstractMessageFormHandler
{
    /**
     * Composes a message from the form data
     *
     * @param AbstractMessage $message
     * @return MessageInterface the composed message ready to be sent
     * @throws InvalidArgumentException if the message is not a NewThreadMessage
     */
    public function composeMessage(AbstractMessage $message)
    {
        if (!$message instanceof NewThreadMessage) {
            throw new \InvalidArgumentException(sprintf('Message must be a NewThreadMessage instance, "%s" given', get_class($message)));
        }

        return $this->composer->newThread()
                    ->setSubject($message->getSubject())
                    ->addRecipient($message->getRecipient())
                    ->setSender($this->getAuthenticatedParticipant())
                    ->setBody($message->getBody())
                    ->getMessage();
    }
}

I hope have some solutions !

Thanks a lot !

  • 写回答

1条回答 默认 最新

  • dongtuo5611 2013-09-13 10:48
    关注

    You could set the form data of the form, after you created the form. My form is resolved in the following way:

    In your newThreadAction:

    $form = $this->getFormForNewThreadAction($account);
    

    Keep in mind that $account has the ParticipantInterface in my example.

    In your MessageController:

    /**
     * @param ParticipantInterface $account
     * @return \FOS\MessageBundle\FormFactory\Form|FormInterface
     */
    private function getFormForNewThreadAction(ParticipantInterface $account)
    {
        $form = $this->getNewThreadFormFactory()->create();
        $preSetFormData = $this->getPreSetFormData($account, $form);
        $form->setData($preSetFormData);
    
        return $form;
    }
    
    /**
     * @param ParticipantInterface $account
     * @param Form $form
     * @return NewThreadMessage
     */
    private function getPreSetFormData(ParticipantInterface $account, Form $form)
    {
        $recipients = new ArrayCollection();
        $recipients->add($account);
    
        /** @var NewThreadMessage $newThreadMessage */
        $newThreadMessage = $form->getData();
        $newThreadMessage->setRecipients($recipients);
    
        return $newThreadMessage;
    }
    

    The form needs his NewThreadMessage as data and you can set your recipients there. That is all folks.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)