dqwh1119 2013-09-11 22:16
浏览 42
已采纳

Symfony2.3:未呈现验证错误

A contact form specifies a date, type and distribution center for a set of households. [The set of households is defined as the most recent for that center and is presented via ajax.] The type entity is required and the date must not be in the future. The type constraint is specified in the Contact entity; the date constraint is in a custom validator. If either of these constraints are violated the form is not valid as confirmed in Netbeans debug. Also, the error message appears in Netbeans.. Yet neither of the constraint messages appear in the template. Adding a dump of form_errors to the template provides nothing. I cannot determine why no messages appear.

Contact entity snippet:

namespace Mana\ClientBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Mana\ClientBundle\Validator\Constraints as ManaAssert;

/**
 * Contact
 *
 * @ORM\Table(name="contact", indexes={@ORM\Index(name="idx_contact_household_idx", columns={"household_id"}), @ORM\Index(name="idx_contact_type_idx", columns={"contact_type_id"}), @ORM\Index(name="idx_contact_center_idx", columns={"center_id"})})
 * @ORM\Entity(repositoryClass="Mana\ClientBundle\Entity\ContactRepository")
 * 
 */
class Contact
{
...
    /**
     * @var \Mana\ClientBundle\Entity\ContactType
     *
     * @ORM\ManyToOne(targetEntity="Mana\ClientBundle\Entity\ContactType", inversedBy="contacts")
     * @ORM\JoinColumn(name="contact_type_id", referencedColumnName="id")
     * @Assert\NotBlank(message="Type must be selected")
     */
    private $contactType;
...
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="contact_date", type="date", nullable=true)
     * @ManaAssert\NotFutureDate
     */
    private $contactDate;

ContactController snippet:

/**
 * @Route("/addContacts", name="contacts_add")
 * @Template("ManaClientBundle:Contact:testLatestContacts.html.twig")
 */
public function addContactsAction(Request $request) {
    $form = $this->createForm(new ContactType());
    $form->handleRequest($request);
    $message = "";
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $households = $this->getRequest()->request->get('contact_household');
        $data = $form->getData();
        $contactDate = $data->getContactDate();
        $contactCenter = $data->getCenter();
        $contactType = $data->getContactType();
        $n = count($households);
        foreach ($households as $id) {
            $household = $em->getRepository('ManaClientBundle:Household')->find($id);
            $houseContacts = $household->getContacts();
            $nContacts = count($houseContacts);
            $first = ($nContacts > 0) ? 0 : 1;
            $county = $contactCenter->getCounty();
            $contact = new Contact();
            $contact->setContactDate($contactDate);
            $contact->setCenter($contactCenter);
            $contact->setContactType($contactType);
            $contact->setCounty($county);
            $contact->setFirst($first);
            $household->addContact($contact);
            $em->persist($household);
        }
        $em->flush();
        $center = $contactCenter->getCenter();
        $desc = $contactType->getContactDesc();
        $message = "$n $desc contacts added for $center";
    }
    return array(
        'form' => $form->createView(),
        'title' => 'Add contacts',
        'message' => $message,
    );
}

Template snippet:

{{ form_errors(form.contactDate) }}
{{ form_errors(form.contactType) }}
<div class="width80">
    <form action="{{ path("contacts_add") }}" method="post">
        <div class="column1">
            <table>
                <tr>
                    <td><b>Date:</b> 
                    <td>{{ form_widget(form.contactDate) }}
                <tr>
                    <td><b>Type:</b> 
                    <td>{{ form_widget(form.contactType) }}
                <tr>
                    <td><b>Center:</b> 
                    <td>{{ form_widget(form.center)}}
            </table>

ContactType:

namespace Mana\ClientBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('contactType', 'entity', array(
                    'class' => 'ManaClientBundle:ContactType',
                    'property' => 'contactDesc',
                    'empty_value' => 'Select contact type',
                    'error_bubbling' => true,
                    'attr' => array("class" => "smallform"),
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                                ->orderBy('c.contactDesc', 'ASC');
                    },
                ))
            ->add('contactDate', 'date', array(
                'data' => date_create(),
                'format' => 'M/d/y',
                'label' => '<b>Date:</b> ',
                'years' => range(date('Y'), date('Y') - 5),
            ))
            ->add('center', 'entity', array(
                    'class' => 'ManaClientBundle:Center',
                    'property' => 'center',
                    'data' => "",
                    'empty_value' => 'Select distribution center',
                    'error_bubbling' => true,
                    'attr' => array("class" => "smallform"),
                    'query_builder' => function(EntityRepository $er) {
                        return $er->createQueryBuilder('c')
                                ->orderBy('c.center', 'ASC');
                    },
                ))
            ->add('household', 'choice', array(
                'mapped' => false,
                'expanded' => true,
                'multiple' => true,
            ))
            ->add('householdId','text',array(
                'mapped' => false,
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Mana\ClientBundle\Entity\Contact',
            'cascade_validation' => true,
            'csrf_protection' => false,
            'required' => false,
            'error_bubbling' => TRUE,
            ));
    }

    public function getName()
    {
        return 'contact';
    }
}
  • 写回答

1条回答 默认 最新

  • dongya2029 2013-09-15 17:27
    关注

    The error was due to including the code

    'error_bubbling' => true,
    

    Since the form was not a child there was no reason for that code to be there. It was an artifact from an earlier version of the application.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站