9

I was following tutorial book for Symfony2. I was doing project with forms.

Chapter 12: Forms | 150

My code:

    public function newAction(Request $request)
    {
   $task = new Task();
   $task->setTask('Find EiM group');
   $task->setDueDate(new DateTime('tomorrow'));

   $form = $this->createFormBuilder($task)
      ->add('task', 'text')
      ->add('dueDate', 'date')
      ->add('save', 'submit')
      ->getForm();

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

And I'm getting error from the topic.

No default option is configured for constraint Symfony\Component\Validator\Constraints\DateTime

What it can be? How to fix it? I followed example in book strictly.

2 Answers 2

29

This:

$task->setDueDate(new DateTime('tomorrow'));

should be:

$task->setDueDate(new \DateTime('tomorrow'));

Notice the forward slash before Datetime. Check your "use" statement, you probably imported and are using the class 'Symfony\Component\Validator\Constraints\DateTime"

Sign up to request clarification or add additional context in comments.

Comments

1

http://symfony.com/doc/current/reference/forms/types/date.html

I'm not sure, but after this doc, the basic usage is :

$builder->add('publishedAt', 'date', array(
    'input'  => 'datetime',
    'widget' => 'choice',
));

or

$builder->add('publishedAt', 'date', array(
    'input'  => 'timestamp',
    'widget' => 'choice',
));

Maybe should you try to insert the option array after the

'date'

2 Comments

Jahnux73: am I missing something? your first and second example are identical.
When reading the docs, i've seen that you have to insert the options array after the 'date' in the builder.... I update the second exemple, it's from the doc... (array('input=>..........)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.