Andree
Andree

Reputation: 3103

Model-layer validation in Doctrine, Symfony

I have a schema.yml containing something similiar to the following:

Character:
  tableName: characters 
  actAs: { Timestampable: ~ }
  columns:
    id: { type: integer(4), primary: true, autoincrement: true }
    name: { type: string(255), notnull: true, notblank: true, minlength: 3 }

I define the minlength of the column name to be 3. I created a unit test to test the minlength validation, and I found out that the validation is not working.

$character = new Character();
$character->set('name', 'Dw');
$t->ok(! $character->isValid()); # This test failed

Can someone tell me what might be the problem here?

Thanks, Andree

Upvotes: 2

Views: 916

Answers (2)

Pavel Linkesch
Pavel Linkesch

Reputation: 3546

Andree's answer is working, but there's other, a bit simlier way to turn on Doctrine's validation.

Add this into your databases.yml config file:

all:
  doctrine:
    param:
      attributes:
        validate: validate_all

Upvotes: 1

Andree
Andree

Reputation: 3103

I found it.

Doctrine's validation is turned off by default, so you have to turned it on using the following code:

$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);

In Symfony, I add the following code to /config/ProjectConfiguration.class.php

  public function configureDoctrine(Doctrine_Manager $manager) 
  { 
    $manager->setAttribute(Doctrine::ATTR_VALIDATE, Doctrine::VALIDATE_ALL);
  }

Upvotes: 5

Related Questions