Symfony4-处理带有表单收集原型的数据原型发送

时间:2019-05-06 09:08:36

标签: php forms collections prototype symfony4

我现在要添加附件的邮件表单(功能性邮件)。

我先前创建的上载收藏集的附件,问题是:

  • 当我向此表单添加默认值时
    在控制器中创建表单之前,基本上是$ form-> addPiece(上传))正确显示的字段或可以传递数据的字段,或者我希望选择不显示默认字段
  • 当我不符合默认字段时,我会使用javascript显示原型,但是当我验证表单时,该集合中的任何数据都不会发送到控制器

MailType.php

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            (...)
            ->add('piece',CollectionType::class, [
                'entry_type' => UploadType::class,
                'entry_options' => ['label' => false],
                'by_reference' => false,
                'required' => false,
                'allow_add' => true,
                'allow_delete' => true
            ])
            ->add('save', SubmitType::class, [
                'label' => 'Envoyer le mail',
                'attr' => [
                    'class' => 'btn btn-success'
                ]
            ])
            ;

    }

    public function configureOptions(\Symfony\Component\OptionsResolver\OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Mail::class,
            'cascade_validation' => true
        ]);
    }

Mail.php

/**
 * @ORM\Entity(repositoryClass="App\Repository\MailRepository")
 */
class Mail
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string|null
     */
    private $emetteur;

    /**
     * @var string|null
     */
    private $destinataires;

    /**
     * @var string|null
     */
    private $cc;

    /**
     * @var string|null
     */
    private $ccHide;

    /**
     * @var string|null
     */
    private $objet;

    /**
     * @var string|null
     */
    private $message;

    /**
     * @var Upload $piece
     * 
     * @ORM\OneToMany(targetEntity="Upload", mappedBy="mailParent", cascade="all", orphanRemoval=true)
     */
    private $piece;


    public function __construct() {
        $this->piece = new ArrayCollection();
    }

    /** 
     * Get the value of pieces
     *
     * @return  Collection
     */ 
    public function getPiece()
    {
        return $this->piece;   
    }

    /**
     * add  value of pieces
     *
     * @param  Upload $piece
     *
     * @return  Mail
     */ 
    public function addPiece(Upload $piece)
    {
        $this->piece[] = $piece;
        $piece->setMailParent($this);

        return $this;
    }

    /**
     * remove  value of pieces
     *
     * @param  Upload $piece
     *
     */ 
    public function removePiece(Upload $piece)
    {
        $this->piece->removeElement($piece);
    }

    /**
     * Get the value of emetteur
     *
     * @return  string|null
     */ 
    public function getEmetteur()
    {
        return $this->emetteur;
    }

    /**
     * Set the value of emetteur
     *
     * @param  string|null  $emetteur
     *
     */ 
    public function setEmetteur($emetteur)
    {
        $this->emetteur = $emetteur;
    }

    /**
     * Get the value of destinataires
     *
     * @return  array|null
     */ 
    public function getDestinataires()
    {
        return $this->destinataires;
    }

    /**
     * Set the value of destinataires
     *
     * @param  array|null  $destinataires
     *
     * @return  self
     */ 
    public function setDestinataires($destinataires)
    {
        $this->destinataires = $destinataires;

        return $this;
    }

    /**
     * Get the value of cc
     *
     * @return  string|null
     */ 
    public function getCc()
    {
        return $this->cc;
    }

    /**
     * Set the value of cc
     *
     * @param  string|null  $cc
     *
     * @return  self
     */ 
    public function setCc($cc)
    {
        $this->cc = $cc;

        return $this;
    }

    /**
     * Get the value of ccHide
     *
     * @return  string|null
     */ 
    public function getCcHide()
    {
        return $this->ccHide;
    }

    /**
     * Set the value of ccHide
     *
     * @param  string|null  $ccHide
     *
     * @return  self
     */ 
    public function setCcHide($ccHide)
    {
        $this->ccHide = $ccHide;

        return $this;
    }

    /**
     * Get the value of objet
     *
     * @return  string|null
     */ 
    public function getObjet()
    {
        return $this->objet;
    }

    /**
     * Set the value of objet
     *
     * @param  string|null  $objet
     *
     * @return  self
     */ 
    public function setObjet($objet)
    {
        $this->objet = $objet;

        return $this;
    }

    /**
     * Get the value of message
     *
     * @return  string|null
     */ 
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * Set the value of message
     *
     * @param  string|null  $message
     *
     * @return  self
     */ 
    public function setMessage($message)
    {
        $this->message = $message;

        return $this;
    }

    /**
     * Return string representation
     * 
     * @return string The name of the directory
     */
    public function __toString()
    {
        return $this->name;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

}

Upload.php

/**
 * @ORM\Entity(repositoryClass="App\Repository\UploadRepository")
 */
class Upload
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Mail", inversedBy="piece")
     * @ORM\JoinColumn(nullable=false)
     */
    private $mailParent;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName(File $file = null)
    {
        $this->name = $file;

        return $this;
    }

    /**
     * Set mail parent
     *
     * @param Mail $mailParent
     *
     * @return Upload
     */
    public function setMailParent(Mail $mailParent)
    {
        $this->mailParent = $mailParent;
        return $this;
    }
    /**
     * Get mail Parent
     *
     * @return Mail
     */
    public function getMailParent()
    {
        return $this->mailParent;
    }
}

控制器

/**
     * @Route("/mail", name="mail")
     */
    public function mail(Request $request, \Swift_Mailer $mailer)
    {
        $mail = new Mail();
        $upload = new Upload();

        // $mail->addPiece($upload);

        $form = $this->createForm(MailType::class, $mail);
        $form->handleRequest($request);

        // var_dump($_FILES);
        var_dump($_POST);

        (...)

        if ($form->isSubmitted() && $form->isValid()) {
            (...)
        }

        return $this->render('accueil/mail.html.twig', [
            'controller_name' => 'AccueilController',
            'form' => $form->createView()
            ]); 
    }

0 个答案:

没有答案