0

I have an Entity named Task and build a Symfony TaskType.php for the form. It is my aim to set the endDate datetime field by default to the input of the startDate datime field (which is required).

I tried this, but it doesn't work.

$builder->add('name');
    $builder->add('startDate', 'datetime');
    $builder->add('endDate', 'datetime', array(
        'empty_value' => array('year' => 'Year', 'month' => 'Month', 'day' => 'Day'),
        'required' => false,
        'data' => isset($options['data']) ? $options['data']->getEndDate() : $options['data']->getStartDate(),

    ));

Exception:

An exception occurred while executing 'INSERT INTO Task (name, startDate, endDate) VALUES (?, ?, ?)' with params {"1":"test","2":"2013-03-30 00:00:00","3":null}:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'endDate' cannot be null 500 Internal Server Error - DBALException 1 linked Exception:

PDOException »
4
  • Could you be a bit more specific than 'it doesn't work'? How does it not work? Error message, unexpected behavior? Unfortunately most of us don't have the benefit of the force to help understand cryptic error descriptions like this :) Commented Mar 24, 2013 at 14:37
  • yes of course, sry . I edited the Post with the exception. Commented Mar 24, 2013 at 16:13
  • Well, there's your answer in the error message. You are trying to insert a null value into a column that is not allowed to contain null. Commented Mar 24, 2013 at 16:32
  • Yes, but i don't know how to retrieve the startDate data into the endDate data. Commented Mar 24, 2013 at 17:03

2 Answers 2

3

Yes, of course it doesn't work. I recommend to read about how form works, if you have no time you can just read this "cheatsheet" - http://blog.stfalcon.com/wp-content/uploads/2012/01/how_symfony2_forms_works.pdf.

You can set the value after data was set. For example, try:

$form->bindRequest($request); 
// now you can get data and set 
$form->get('endDate')->setData($form->get('startDate')->getData());

Hope, it helps.

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

1 Comment

thanks, but when i set the data after the bindRequest i get a "You cannot change the data of a bound form "
0

You can also set the values in your entity class when it is instantiated:

class Task
{
    protected $startDate;
    protected $endDate;

    public function __construct($startDate)
    {
        $this->startDate = $startDate;
        $this->endDate = $startDate;
    }
}

2 Comments

thanks, but when i go to my Route addTask , i get the "Missing argument 1 for Task\AppBundle\Entity\Task::__constructor ... " exception, because he don't get the $startdate value, how to handle that?
And when i put for example $task = new Task(new \DateTime('NOW')); in my TaskController, I can open the addTask Route, but when i flush the form data to the database, the endDate has only the new DateTime values instead of the releaseDate form data

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.