Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2, Form, Trying to get property of non-object in

Tags:

forms

php

symfony

I'm trying to create a contact form without entities, but after I submit form, I'll get this error:

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

Here is my 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';
    }
}

and my 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()));
    }
}

I read on the net, that it is PHP bug for 5.3 version, but I'm using 5.4.19 PHP version. Any idea?

UPDATE - DUMP TEST

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

returns:

array(0) {
}
like image 730
Lkopo Avatar asked Oct 02 '22 17:10

Lkopo


1 Answers

So I found where my problem was:

    $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')))

Correct form:

    $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'))

I missed ) in my array, so costraint also was label field, so it was not an valid array. My bad.

like image 136
Lkopo Avatar answered Oct 13 '22 12:10

Lkopo