Symfony2,窗体,试图获取非对象的属性

勒科波

我正在尝试创建没有实体的联系表单,但是在提交表单后,我将收到此错误:

Notice: Trying to get property of non-object in /var/www/Myblog/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php line 84

这是我的 ContactType.php

<?php

namespace Acme\ContactBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;

class ContactType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null,
            'csrf_protection' => true,
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text', array('constraints' => array(new NotBlank(array('message' => 'Name cannot be blank')), new Length(array('min' => 3, 'minMessage' => 'Name is too short')), 'label' => 'Name')))
                ->add('email', 'email', array('constraints' => array(new NotBlank(array('message' => 'E-mail cannot be blank')), new Email(array('message' => 'E-mail is not valid')), 'label' => 'E-mail')))
                ->add('content', 'textarea', array('constraints' => array(new NotBlank(array('message' => 'Message cannot be blank')), new Length(array('min' => 10, 'minMessage' => 'Message must have min. 10 characters')), 'label' => 'Message')))
                ->add('save', 'submit', array('label' => 'Send'));
    }

    public function getName()
    {
        return 'contact';
    }
}

和我的 DefaultController.php

<?php

namespace Acme\ContactBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\ContactBundle\Form\Type\ContactType;
use Acme\SettingsBundle\Entity\Settings;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
        $form = $this->createForm('contact');
        $form->handleRequest($request);

        if($form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $settings = $em->getRepository('AcmeSettingsBundle:Settings')->find(Settings::DUMMY_IDENTIFIER);

            $message = \Swift_Message::newInstance()
                                    ->setSubject('Message from contact form - ' . $settings->getPageName())
                                    ->setFrom($form->get('email')->getData())
                                    ->setTo($settings->getPageEmail())
                                    ->setBody($this->renderView('AcmeContactBundle:Default:mail.txt.twig', array('name' => $form->get('name')->getData(), 'page_name' => $settings->getPageName(), 'homepage' => $this->generateUrl('default_blog'))));

            $this->get('mailer')->send($message);

            $this->get('session')->getFlashBag()->add(
                'success',
                'Message was successfuly sent'
            );
        }

        return $this->render('AcmeContactBundle:Default:index.html.twig', array('form' => $form->createView()));
    }
}

我在网上阅读到,这是5.3版本的PHP错误,但我使用的是5.4.19 PHP版本。任何的想法?

更新-垃圾测试

    // Validate the data against the constraints defined
    // in the form
    $constraints = $config->getOption('constraints');
    var_dump($constraints); exit();

返回:

array(0) {
}
勒科波

所以我发现问题出在哪里:

    $builder->add('name', 'text', array('constraints' => array(new NotBlank(array('message' => 'Name cannot be blank')), new Length(array('min' => 3, 'minMessage' => 'Name is too short')), 'label' => 'Name')))

正确形式:

    $builder->add('name', 'text', array('constraints' => array(new NotBlank(array('message' => 'Name cannot be blank')), new Length(array('min' => 3, 'minMessage' => 'Name is too short'))), 'label' => 'Name'))

在数组中错过了,所以costraint也是label字段,所以它不是有效的数组。我的错。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Symfony2,窗体,尝试获取非对象的属性

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取属性非对象

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取非对象的属性

来自分类Dev

试图获取yii2中非对象的属性

来自分类Dev

Yii2:试图获取非对象的属性

来自分类Dev

试图获取非对象的属性-> xml

来自分类Dev

注意:试图获取非对象的属性

来自分类Dev

yii:试图获取非对象的属性

来自分类Dev

注意:试图获取非对象错误的属性

来自分类Dev

(全文搜索)试图获取非对象的属性

来自分类Dev

CodeIgniter返回试图获取非对象的属性

来自分类Dev

试图获取非对象的属性-Laravel

来自分类Dev

试图获取非对象的属性[laravel 5.2]

来自分类Dev

php Laravel试图获取非对象的属性

来自分类Dev

Laravel Join:试图获取非对象的属性

来自分类Dev

ErrorException试图获取非对象Laravel的属性

来自分类Dev

试图获取非对象Laravel的属性“浴室”

来自分类Dev

yii:试图获取非对象的属性

来自分类Dev

“试图获取非对象的属性”错误PHP

来自分类Dev

注意:试图获取非对象的属性

来自分类Dev

ErrorException试图获取非对象的属性

来自分类Dev

PHP错误“试图获取非对象的属性”

来自分类Dev

laravel试图获取非对象的属性

来自分类Dev

试图在laravel中获取非对象的属性