Symfony形式:isValid()存在问题

时间:2019-05-24 08:49:40

标签: symfony

我需要你的帮助。

我有一个表格,当我单击提交时。我的页面重新加载,我的表单没有css,并且包含要发送的信息。

我签入了我的控制器。这不是重定向的问题。但是,我试图检查我的表单是否为SubSubmitted。没关系。但是,当我检查自己的表格是否有效时,我没有任何答案。我看不出问题出在哪里。

我的代码:

我的实体:

<?php

    namespace App\RenseignementBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use JsonSerializable;

    /**
     * @ORM\Entity(repositoryClass="App\RenseignementBundle\Repository\InformationRepository")
     * @ORM\Table(name="Information")
     */
    class Information implements JsonSerializable
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue(strategy="CUSTOM")
         * @ORM\Column(type="bigint", name="In_id")
         */
        private $id;

        /**
         * @ORM\Column(type="string", length=50, name="In_label")
         */
        private $label;

        /**
         * @ORM\Column(type="boolean", name="In_commentstate")
         */
        private $commentState;

        /**
         * @ORM\Column(type="boolean",name="In_archive")
         */
        private $archive;


        /**
         * @ORM\Column(type="integer", name="In_order")
         */
        private $order;


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

        public function setId(int $id): self
        {
            $this->id = $id;

            return $this;
        }

        public function getLabel(): ?string
        {
            return $this->label;
        }

        public function setLabel(string $label): self
        {
            $this->label = $label;

            return $this;
        }


        public function getCommentState(): ?bool
        {
            return $this->commentState;
        }


        public function setCommentState(bool $commentState): self
        {
            $this->commentState = $commentState;

            return $this;
        }

        public function getArchive(): ?bool
        {
            return $this->archive;
        }

        public function setArchive(int $archive): self
        {
            $this->archive = $archive;

            return $this;
        }

        /**
         * @ORM\ManyToOne(targetEntity="App\RenseignementBundle\Entity\Category", inversedBy="informations")
         * @ORM\JoinColumn(name="fk_category", referencedColumnName="Ca_id")
         */
        private $category;

        public function getCategory(): ?Category
        {
            return $this->category;
        }

        public function setCategory(?Category $category): self
        {
            $this->category = $category;

            return $this;
        }

        /**
         * @ORM\OneToMany(targetEntity="App\RenseignementBundle\Entity\RecordToInformation", mappedBy="information")
         */
        private $recordToInformations;

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

        /*
        /**
        * @return Collection|RecordToInformation[]
        */

        public function getRecordToInformations()
        {
            return $this->recordToInformations;
        }


        public function jsonSerialize()
        {
            $Arr = get_object_vars($this);
            return $Arr;

        }

        public function getOrder(): ?int
        {
            return $this->order;
        }

        public function setOrder(string $order): self
        {
            $this->order = $order;

            return $this;
        }
    }

我的控制器:

<?php

    namespace App\RenseignementBundle\Controller;

    use App\RenseignementBundle\Entity\Information;
    use App\RenseignementBundle\Form\InformationFormType;
    use Doctrine\ORM\EntityManagerInterface;
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Routing\Annotation\Route;

    /**
     * Information Controller
     *
     * @Route("/informations/information")
     */
    class InformationController extends AbstractController
    {
        /**
         * @Route("/", name="information.list")
         * @param EntityManagerInterface $em
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function index(EntityManagerInterface $em, Request $request)
        {


            $categinformation = $em->getRepository('RenseignementBundle:Category')->findAll();

            $informations = $em->getRepository('RenseignementBundle:Information')->findInformation();

            return $this->render('RenseignementBundle/Informations/listInformation.html.twig', array(
                'categinformations' => $categinformation,
                'informations' => $informations,
            ));
        }


        /**
         * @Route("/add", name="information.new")
         * @param EntityManagerInterface $em
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function add(EntityManagerInterface $em, Request $request)
        {
            $information = new Information();
            $form = $this->createForm(InformationFormType::class, $information,[
                'action' => $this->generateUrl('information.new'),
                'method' => 'POST'
            ]);

            $form->handleRequest($request);

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

                $em->persist($information);
                $em->flush();

                $this->addFlash('success', 'Renseignement ajouté');

                return $this->redirectToRoute('information.list');
            }

            return $this->render('RenseignementBundle/Informations/newInformation.html.twig', [
                'recordForm' => $form->createView()
            ]);
        }

        /**
         * @Route("/update/{id}/", name="information.update")
         * @param EntityManagerInterface $em
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function update($id, EntityManagerInterface $em, Request $request)
        {

            $information = $em->getRepository('RenseignementBundle:Information')->find($id);

            $form = $this->createForm(InformationFormType::class, $information,[
                'action' => $this->generateUrl('information.update', ['id'=> $id]),
                'method' => 'POST'
            ]);

            $form->handleRequest($request);


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

                    $em = $this->getDoctrine()->getManager();

                    $em->persist($information);

                    $em->flush();
                    $this->addFlash('success', 'Renseignement modifié');

                }

            if($form->isSubmitted()){
           print_r(" isValid:".$form->isValid());
        }

            return $this->render('RenseignementBundle/Informations/newInformation.html.twig', [
                'recordForm' => $form->createView(),
                'information' => $information
            ]);
        }


    }

我的FormType:

    namespace App\RenseignementBundle\Form;

    use App\RenseignementBundle\Entity\Category;
    use Doctrine\ORM\EntityRepository;
    use Symfony\Bridge\Doctrine\Form\Type\EntityType;
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\IntegerType;

    class InformationFormType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('category', EntityType::class, [
                    'class' => Category::class,
                    // a request to find only the non archived categories
                    'query_builder' => function (EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                            ->where('c.archive = 0')
                            ->orderBy('c.label', 'ASC');
                    },
                    'choice_label' => 'label',
                    'required' => true
                ])
                ->add('label', TextType::class)
                ->add('commentState', CheckboxType::class, array(
                        'required' => false,
                        'label' => 'configuration.newInformation.commentState',
                        'translation_domain' => 'configInformations')
                )
                ->add('archive', CheckboxType::class, array(
                        'required' => false,
                        'label' => 'configuration.newInformation.archive',
                        'translation_domain' => 'configInformations')
                )
                ->add('order', IntegerType::class);
        }
    }

我的树枝:

<div class="card mb-3">


        <div class="card-body">

            {{ form_start(recordForm) }}
            <div class="form-group">
                <label>{{ 'configuration.newInformation.category'|trans({}, 'configInformations') }}<span
                            class="text-danger">*</span></label>
                {{ form_widget(recordForm.category) }}
            </div>

            <div class="form-group">
                <label>{{ 'configuration.newInformation.name'|trans({}, 'configInformations') }}</label>
                {{ form_widget(recordForm.label) }}
            </div>
            <div class="form-group">
                <label>{{ 'configuration.newInformation.order'|trans({}, 'configInformations') }}</label>
                {{ form_widget(recordForm.order) }}
            </div>
            <div class="form-check">

            </div>

            <div class="form-group text-right m-b-0">
                <label class="form-check-label">
                    {{ form_widget(recordForm.commentState) }}
                    {{ form_errors(recordForm.commentState) }}
                </label>

            </div>
            <div class="form-group text-right m-b-0">
                <label class="form-check-label">
                    {{ form_widget(recordForm.archive) }}
                    {{ form_errors(recordForm.archive) }}
                </label>

            </div>

            {% if information is defined %}
                <button class="btn btn-primary" type="submit">
                    {{ 'configuration.newInformation.modifyInformation'|trans({}, 'configInformations') }}
                </button>
            {% else %}
                <button class="btn btn-primary" type="submit">
                    {{ 'configuration.newInformation.addInformation'|trans({}, 'configInformations') }}
                </button>
            {% endif %}
            <button type="reset" class="btn btn-secondary m-l-5">
                {{ 'configuration.newCategory.cancel'|trans({}, 'configInformations') }}
            </button>
        </div>

        {{ form_rest(recordForm) }}
        {{ form_end(recordForm) }}
    </div>

我的JS:

<div class="card mb-3">


        <div class="card-body">

            {{ form_start(recordForm) }}
            <div class="form-group">
                <label>{{ 'configuration.newInformation.category'|trans({}, 'configInformations') }}<span
                            class="text-danger">*</span></label>
                {{ form_widget(recordForm.category) }}
            </div>

            <div class="form-group">
                <label>{{ 'configuration.newInformation.name'|trans({}, 'configInformations') }}</label>
                {{ form_widget(recordForm.label) }}
            </div>
            <div class="form-group">
                <label>{{ 'configuration.newInformation.order'|trans({}, 'configInformations') }}</label>
                {{ form_widget(recordForm.order) }}
            </div>
            <div class="form-check">

            </div>

            <div class="form-group text-right m-b-0">
                <label class="form-check-label">
                    {{ form_widget(recordForm.commentState) }}
                    {{ form_errors(recordForm.commentState) }}
                </label>

            </div>
            <div class="form-group text-right m-b-0">
                <label class="form-check-label">
                    {{ form_widget(recordForm.archive) }}
                    {{ form_errors(recordForm.archive) }}
                </label>

            </div>

            {% if information is defined %}
                <button class="btn btn-primary" type="submit">
                    {{ 'configuration.newInformation.modifyInformation'|trans({}, 'configInformations') }}
                </button>
            {% else %}
                <button class="btn btn-primary" type="submit">
                    {{ 'configuration.newInformation.addInformation'|trans({}, 'configInformations') }}
                </button>
            {% endif %}
            <button type="reset" class="btn btn-secondary m-l-5">
                {{ 'configuration.newCategory.cancel'|trans({}, 'configInformations') }}
            </button>
        </div>

        {{ form_rest(recordForm) }}
        {{ form_end(recordForm) }}
    </div>

谢谢您的帮助。

编辑:

我决定用JS验证表单中的一个字段。如果我的验证JS正常,则将数据发送到控制器。如果还不行,我停止提交。 希望它能对某人有所帮助。

0 个答案:

没有答案
相关问题