Tuesday, September 17, 2013

Symfony 2 symblog tutorial errors

It seems that the Symfony 2 tutorial ("Symblog") is a little out of date. While going through it, I stumbled on some unexpected behaviour. There were exceptions getting thrown when the tutorial made no mention of them. This could be due to several behaviours being deprecated since 2.1; I was using 2.3.4.

So without further ado...

The errors and their fixes as you work through the tutorial

1) When creating a controller method for the Contact page, $form->bind($this->bindRequest($request) resulted in:

FatalErrorException: Error: Call to undefined method Symfony\Component\Form\Form::bindRequest() in /path/to/root/src/Blogger/BlogBundle/Controller/PageController.php line 31

I poked around at code that I already knew worked and found this alternative:
$form->bind($this->getRequest());

2) While editing src/Blogger/BlogBundle/Entity/EnquiryType.php, adding this line $metadata->addPropertyConstraint('body', new MaxLength(50)); in static function loadValidatorMetadata(ClassMetadata $metadata) resulted in an exception:

FatalErrorException: Error: Class 'Symfony\Component\Validator\Constraints\MaxLength' not found in /path/to/root/src/Blogger/BlogBundle/Entity/Enquiry.php line xx

I read elsewhere that MaxLength and MinLength are deprecated since Symfony 2.1, so you're better off declaring the above line as
$metadata->addPropertyConstraint('subject', new Length(array('max' => 50)));

Just make sure to add this to the top of the class: use Symfony\Component\Validator\Constraints\Length;

3)When told to add the following lines to src/Blogger/BlogBundle/Resources/config/config.yml

parameters: blogger_blog.comments.latest_comment_limit: 10
You will instead get an error along the lines of blogger_blog.comments.latest_comment_limit must be defined. Now, the solution is to either do this tutorial (http://symfony.com/doc/current/cookbook/bundles/extension.html), or do the shortcut, which I did (because lazy).

4) While trying to enable Assetic for BloggerBlogBundle, you are instructed to put it in app/config/config.yml. However, it doesn't take effect. Instead all sidebar.css-ing disappeared. This is because you should have put BloggerBlogBundle in app/config/config_dev.yml.

5) Yui Compressor jar file not found - Make sure that 1) you've put the jar file in app/Resources/java and that 2) the name of that jar file is the same as in config_dev.yml.

If there will be more, I will update this post. Happy coding!