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

时间:2013-07-20 13:29:29

标签: php symfony

我隐藏了我的收件人字段表单,现在我想了解控制器中我可以告诉收件人的价值

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匹配到:

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();
    }
}

我希望有一些解决方案!

非常感谢!

1 个答案:

答案 0 :(得分:1)

创建表单后,您可以设置表单的表单数据。 我的表单以下列方式解决:

在你的newThreadAction中:

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

请注意,$ account在我的示例中包含ParticipantInterface。

在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;
}

表单需要将NewThreadMessage作为数据,您可以在那里设置收件人。这就是所有人。