Symfony2: How to validate an input field is not-blank, only when checkbox is true?

后端 未结 3 1735
日久生厌
日久生厌 2020-12-30 14:51

Within Symfony2 how to validate an inputfield is not-blank, only when the value of a checkbox is 1 (True) - otherwise blank is allowed?

To be more precise, I have a

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 15:04

    I think this is the solution:

    createForm(new CustomerType(), new Customer());
            $form->handleRequest($request);
    
            if ($form->isValid()) {
                $data = $form->getData();
                $em = $this->getDoctrine()->getManager();
                $em->persist($data);
                $em->flush();
    
                return $this->redirect($this->generateUrl('customer_list'));
                // return $this->redirect($this->generateUrl('task_success'));
            }
    
            return $this->render('InvoicingBundle:Page:customers.html.twig', array(
                'form' => $form->createView()
            ));   
        }
    
        public function listAction() {
            $customers = $this->getDoctrine()->getManager()
                ->createQuery('SELECT c FROM InvoicingBundle:Customer c')
                ->execute();
    
            return $this->render(
                'InvoicingBundle:Customer:list.html.twig', 
                array('customers' => $customers));
        }
    
        public function showAction($id) {
            $customer = $this->get('doctrine')
                ->getManager()
                ->getRepository('InvoicingBundle:Customer')
                ->find($id);
    
            if (!$post) {
                // cause the 404 page not found to be displayed
                throw $this->createNotFoundException();
            }
    
            return $this->render(
                'InvoicingBundle:Page:customers.html.twig',
                array('customer' => $customer)
            );
        }
    
    }
    

    ?>

        add('billing_id');
            $builder->add('billing_name');
            $builder->add('billing_consignee');
            $builder->add('billing_street');
            $builder->add('billing_address');
            $builder->add('billing_zip');
            $builder->add('billing_city');
            $builder->add('billing_country');
            $builder->add('billing_email', 'email');
            $builder->add('billing_debit', 'checkbox');
            $builder->add('billing_iban');
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver) {
            $resolver->setDefaults(array(
                'data_class' => 'ZeroSpace\InvoicingBundle\Entity\Customer',
                'validation_groups' => function(FormInterface $form) {
                    $data = $form->getData();
                    if($data->getBillingDebit() == 1) {
                        return array('Default', 'iban_required');
                    }
                    else {
                        return array('Default');
                    }
                },
            ));
        }
    
        public function getName() {
            return 'customer';
        }
    }
    
    
    ?>
    
        id;
        }
    
        /**
         * Set billing_id
         *
         * @param integer $billingId
         * @return Customer
         */
        public function setBillingId($billingId)
        {
            $this->billing_id = $billingId;
    
            return $this;
        }
    
        /**
         * Get billing_id
         *
         * @return integer 
         */
        public function getBillingId()
        {
            return $this->billing_id;
        }
    
        /**
         * Set billing_name
         *
         * @param string $billingName
         * @return Customer
         */
        public function setBillingName($billingName)
        {
            $this->billing_name = $billingName;
    
            return $this;
        }
    
        /**
         * Get billing_name
         *
         * @return string 
         */
        public function getBillingName()
        {
            return $this->billing_name;
        }
    
        /**
         * Set billing_consignee
         *
         * @param string $billingConsignee
         * @return Customer
         */
        public function setBillingConsignee($billingConsignee)
        {
            $this->billing_consignee = $billingConsignee;
    
            return $this;
        }
    
        /**
         * Get billing_consignee
         *
         * @return string 
         */
        public function getBillingConsignee()
        {
            return $this->billing_consignee;
        }
    
        /**
         * Set billing_street
         *
         * @param string $billingStreet
         * @return Customer
         */
        public function setBillingStreet($billingStreet)
        {
            $this->billing_street = $billingStreet;
    
            return $this;
        }
    
        /**
         * Get billing_street
         *
         * @return string 
         */
        public function getBillingStreet()
        {
            return $this->billing_street;
        }
    
        /**
         * Set billing_address
         *
         * @param string $billingAddress
         * @return Customer
         */
        public function setBillingAddress($billingAddress)
        {
            $this->billing_address = $billingAddress;
    
            return $this;
        }
    
        /**
         * Get billing_address
         *
         * @return string 
         */
        public function getBillingAddress()
        {
            return $this->billing_address;
        }
    
        /**
         * Set billing_zip
         *
         * @param string $billingZip
         * @return Customer
         */
        public function setBillingZip($billingZip)
        {
            $this->billing_zip = $billingZip;
    
            return $this;
        }
    
        /**
         * Get billing_zip
         *
         * @return string 
         */
        public function getBillingZip()
        {
            return $this->billing_zip;
        }
    
        /**
         * Set billing_city
         *
         * @param string $billingCity
         * @return Customer
         */
        public function setBillingCity($billingCity)
        {
            $this->billing_city = $billingCity;
    
            return $this;
        }
    
        /**
         * Get billing_city
         *
         * @return string 
         */
        public function getBillingCity()
        {
            return $this->billing_city;
        }
    
        /**
         * Set billing_country
         *
         * @param string $billingCountry
         * @return Customer
         */
        public function setBillingCountry($billingCountry)
        {
            $this->billing_country = $billingCountry;
    
            return $this;
        }
    
        /**
         * Get billing_country
         *
         * @return string 
         */
        public function getBillingCountry()
        {
            return $this->billing_country;
        }
    
        /**
         * Set billing_email
         *
         * @param string $billingEmail
         * @return Customer
         */
        public function setBillingEmail($billingEmail)
        {
            $this->billing_email = $billingEmail;
    
            return $this;
        }
    
        /**
         * Get billing_email
         *
         * @return string 
         */
        public function getBillingEmail()
        {
            return $this->billing_email;
        }
    
        /**
         * Set billing_debit
         *
         * @param boolean $billingDebit
         * @return Customer
         */
        public function setBillingDebit($billingDebit)
        {
            $this->billing_debit = $billingDebit;
    
            return $this;
        }
    
        /**
         * Get billing_debit
         *
         * @return boolean 
         */
        public function getBillingDebit()
        {
            return $this->billing_debit;
        }
    
        /**
         * Set billing_iban
         *
         * @param string $billingIban
         * @return Customer
         */
        public function setBillingIban($billingIban)
        {
            $this->billing_iban = $billingIban;
            return $this;
        }
    
        /**
         * Get billing_iban
         *
         * @return string 
         */
        public function getBillingIban() {
            return $this->billing_iban;
        }
    
    }
    

提交回复
热议问题