Symfony 2: Annoying Issue with Data Transformers, DateTime, Timezones and PHP.ini


I spent almost the entire day going through the low level code of Symfony 2 trying to determine a really obscure error with my code. The issue started from dealing with using a Time type for one of my columns and having the form validate it appropriately. Unfortunately, the error message that bubbled up did not adequately report the exact issue with the validation.

To start, let me show how I declared my time type in my Doctrine entity:

/**
* @Assert\Time()
* @ORM\Column(type="time", name="meal_time", nullable=true)
* @Expose
* @Groups({"details"})
* @Type("DateTime<'H:i:s'>")
private $mealTime;

My form type related to this entity has this code snippet in the way I used this in the form:

public function buildForm(FormBuilderInterface $b, array $options){
$b->add('mealTime', 'time', array('input' => 'datetime', 'widget' => 'choice')
// other columns ignored for brevity
}

So what was going on for me was that upon hitting submit, even with the time fields using correct values (even being manually entered via a plain text input type), I kept receiving a “This value is not a valid” error on the field. Upon delving deeper into the issue, I learned that the timezone kept throwing out an issue where the timezone (particularly US/Pacific) “was not in the database.”

What I learned was that my php.ini file had the wrong date.timezone value set. After correcting it to “America/Los_Angeles”, I still kept receiving the same error. At first, I thought that the issue was in another php.ini file. Next, I dumped the entire phpinfo() content out and learned that my php.ini was correct. However, the real issue was that I needed to restart my local server to re-read the php.ini configuration value. Upon restarting the server and re-submitting the form, I managed to successfully save the data this time around.

Either way, that was one of those rare “not fun at all” issues. Someone else mentioned in a forum post that the lower level error describing the timezone issue should’ve bubbled up further rather than being hidden behind the more obscure “This value is not a valid” error. Hopefully, the developers over at Symfony 2 will agree and correct this on the validator/transformer for this one.

(Visited 653 times, 1 visits today)

Comments

comments