Onur Özgür ÖZKAN

Php, Ruby, Kebab, Git Geek

Doctrine Table and Zend Validate Integration

If you use Doctrine and want to make validation with Zend Framework. This tutorial is for you.

  1. Create a TestTable.Yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Test:
  tableName: system_test
  columns:
    email:
      type: string
      email: true
    minlength:
      type: string
      minlength: 4
    range:
      type: integer
      range: [10, 100]
    regexp:
      type: string
      regexp: '/^[a-zA-Z0-9]+$/'


  options:
    type: INNODB
    collate: utf8_bin
    charset: utf8
  1. Build the Doctrine_Record with doctrine.php build-all comment then we get the record
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
31
32
33
34
35
36
37
38
<?php
class Model_Entity_Test extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('system_test');
        $this->hasColumn('email', 'string', null, array(
             'type' => 'string',
             'email' => true,
             ));
        $this->hasColumn('minlength', 'string', null, array(
             'type' => 'string',
             'minlength' => 4,
             ));
        $this->hasColumn('range', 'integer', null, array(
             'type' => 'integer',
             'range' =>
             array(
              0 => 10,
              1 => 100,
             ),
             ));
        $this->hasColumn('regexp', 'string', null, array(
             'type' => 'string',
             'regexp' => '/^[a-zA-Z0-9]+$/',
             ));

        $this->option('type', 'INNODB');
        $this->option('collate', 'utf8_bin');
        $this->option('charset', 'utf8');
    }

    public function setUp()
    {
        parent::setUp();

    }
}
  1. In our controller we use the Kebab_Validation_DoctrineTable
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
<?php
class IndexController extends Kebab_Controller_Action
{
    /**
     * Front-end area index action
     * @return void
     */
    public function indexAction()
    {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $test = new Model_Entity_Test();
        $test->email = 'asdf#$½2';
        $test->minlength = '123';
        $test->range = '101';
        $test->regexp = 'asdf#£$½£#½';

        $validator = new Kebab_Validate_DoctrineTable($test, 'Model_Entity_Test');
        if(!$validator->isValid()) {
            Zend_Debug::dump($validator->getErrors());
        } else {
            echo 'ok';
        }
    }
}
  1. The output is
1
2
3
4
5
6
7
8
9
10
11
array(3) {
  ["email"] => array(1) {
    ["emailAddressInvalidFormat"] => string(77) "'asdf#$½2' is no valid email address in the basic format local-part@hostname"
  }
  ["range"] => array(1) {
    ["notBetween"] => string(48) "'101' is not between '10' and '100', inclusively"
  }
  ["regexp"] => array(1) {
    ["regexNotMatch"] => string(67) "'asdf#£$½£#½' does not match against pattern '/^[a-zA-Z0-9]+$/'"
  }
}

You can find the code on github at kebab-project.com repos. https://github.com/kebab-project/kebab-project/blob/master/library/Kebab/Validate/DoctrineTable.php

Bugs and feedback are always welcome.

Best regards.