Konrad Podgórski - Web Developer

Personal blog about developing web applications with Symfony, Node JS and Angular JS

[Solution] Couldn’t Find Constant Monolog\Logger::DEBUG

| Comments

Note for others having same problem

1
PHP Warning:  constant(): Couldn't find constant Monolog\Logger::DEBUG in [..] /vendor/symfony/monolog-bundle/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php on line 110

This is because composer version you are running doesn\’t support PSR-4. to fix it update your composer with composer self-update

Credit: https://groups.google.com/forum/?hl=en#!topic/symfony2/8cNR9IBjuDo

How to Validate Emails Outside of Form With Symfony Validator Component

| Comments

I’m building an invitation mechanism where owner of group can invite one or many users by providing email address(es) through rest api. This is very simple using Symfony Validator Component, but I noticed strange behavior when running tests. If I do not pass any emails I get wrong status code. 200 OK instead of 400 bad request.

After a while I found that using only \Symfony\Component\Validator\Constraints\Email() is not enough. Validator will pass null values and empty string, which is obvious wrong in my case. I had to also add NotBlank constraint, don’t forget that in future.

At first I thought that this is a bug, but since it’s covered by separate 2 tests here https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php#L35 I will leave it as it is.

Credit goes to the functional tests! :)

Snippet for further reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * Validates single email (or an array of email addresses
 *
 * @param array|string $emails
 *
 * @return array
 */
public function validateEmails($emails){

    $errors = array();
    $emails = is_array($emails) ? $emails : array($emails);

    $validator = $this->container->get('validator');

    $constraints = array(
        new \Symfony\Component\Validator\Constraints\Email(),
        new \Symfony\Component\Validator\Constraints\NotBlank()
    );

    foreach ($emails as $email) {

        $error = $validator->validateValue($email, $constraints);

        if (count($error) > 0) {
            $errors[] = $error;
        }
    }

    return $errors;
}

How I Migrated My Blog From Wordpress to Octopress

| Comments

Yesterday I decided to try a markdown powered blog platform, Octopress with Github Pages hosting

Reason for this change is probably the same as yours, person who reads this

  • Wordpress sucks at formatting code
  • Most of my “notes” and “readme” from various projects are markdown files, so if I decided to publish one of them, copy paste wasn’t enough
  • 3rd, and probably most important. “Hey, we are programmers” we can’t use Wordpress ;–)

How to Setup Octopress on Ubuntu

| Comments

Guide was copied from http://stackoverflow.com/a/14353864/1562884 for my reference

Helped me with this error during configuring Octopress on Ubuntu

Error installing RedCloth:
ERROR: Failed to build gem native extension.

Guide below was written by Eric Zhang

My Ubuntu 12.04 solution is install the ruby1.9.1-full version, that can resolve this issue.

$sudo apt-get install ruby1.9.1-full

you can execute command:

$sudo update-alternatives --config ruby

to select the alternative ruby which you have installed. and then, execute below commands to finish the octopress deploy:

$git clone https://github.com/erizhang/octopress.git octopress
$cd octopress/
$sudo gem install bundler
$sudo gem install RedCloth -v '4.2.9'
$bundle install
$rake install

below commands you can find from octopress deploy guideline page:

$rake setup_github_pages
$sudo rake setup_github_pages
$sudo rake generate
$sudo rake deploy

Hope it’s helpful for you, thanks

How to Implement a Secure Choice Form Field With Symfony

| Comments

Create Util class for your entity, in my case entity is called ‘Brand’

src/KP/Brand/MainBundle/Util/BrandUtil.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php

namespace KP\Brand\MainBundle\Util;

use KP\Brand\MainBundle\Entity\Brand;

class BrandUtil
{

    /**
     * @param bool $withLabels
     *
     * @return array
     */
    public static function getAvailableLanguageCodes($withLabels = false)
    {
        $choices = array(
            'pl' => 'Polish',
            'en' => 'English'
        );

        if ($withLabels) {
            return $choices;
        } else {
            return array_keys($choices);
        }
    }
}

Create standard form type for your entity (I skipped required getName and setDefaultOptions methods here)

src/KP/Brand/MainBundle/Form/BrandType.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

namespace KP\Brand\MainBundle\Form;

use KP\Brand\MainBundle\Util\BrandUtil;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class BrandType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add(
                'languageCode',
                'choice',
                array(
                    'label' => 'Language',
                    'choices' => BrandUtil::getAvailableLanguageCodes(true),
                )
            );
    }

}

src/KP/Brand/MainBundle/Resource/config/validation.yml

1
2
3
4
KP\Brand\MainBundle\Entity\Brand:
properties:
    languageCode:
        - Choice: { callback: [KP\Brand\MainBundle\Util\BrandUtil, getAvailableLanguageCodes] }