2017 © Pedro Peláez
 

cakephp-plugin cakephp3-database

CakePHP 3 plugin that provides classes for adding default validation rules from the database table schema and cleaning data being saved.

image

jmjjg/cakephp3-database

CakePHP 3 plugin that provides classes for adding default validation rules from the database table schema and cleaning data being saved.

  • Sunday, January 22, 2017
  • by jmjjg
  • Repository
  • 1 Watchers
  • 1 Stars
  • 21 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 2 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Database

Description

CakePHP 3 plugin that provides classes for adding default validation rules from the database table schema and cleaning data being saved., (*1)

Caching is enabled by default but can be disabled in the configuration or at run-time., (*2)

Works well only with CakePHP 3.3.x (tested with CakePHP 3.3.12)., (*3)

Main classes

AutovalidateBehavior reads various informations from the database table schema, such as column type, the fact that the column can be NULL, foreign key constraints, unique constraints and automatically adds those validation rules to the default ones., (*4)

FormattableBehavior cleans up data before validation or saving using configurable static class methods. The columns on which the formatters are applied can be defined based on column type and field name regular expression., (*5)

Setup

Assuming the plugin is installed under plugins/Database, add the following to config/bootstrap.php:, (*6)

Plugin::load('Database', ['autoload' => true]);

Settings

The following global settings can optionally be added to the config/app.php file., (*7)

return [
    // ...
    'plugin' =>  [
        // Custom Database plugin behavior configuration
        'Database' => [
            'AutovalidateBehavior' => [
                'cache' => false
            ],
            'FormattableBehavior' => [
                'cache' => false
            ]
        ]
    ],
    // ...
];

Usage

The following code should be added to your table classes, inside the initialize() method., (*8)

The two behaviors are independant and can be loaded in any order., (*9)

Note that NULL and boolean TRUE and are equivalent as configuration values., (*10)

public function initialize(array $config)
{
    // ...
        $this->addBehavior('DatabaseAutovalidate',
            [
                'className' => 'Database.Autovalidate',
                // Default values
                // 1°) Accepted validator names, as a string or an array of strings, NULL for any
                'accepted' => null,
                // 2°) Cache validation rules and their error messages ? NULL for global settings
                'cache' => null,
                // 3°) Domain to use for error messages
                'domain' => 'database'
            ]
        );

        $this->addBehavior('DatabaseFormattable',
            [
                'className' => 'Database.Formattable',
                // Default values
                // 1°) Cache formatters and the field list they apply to ? NULL for global settings
                'cache' => null,
                // 2°) List of formatter functions or static methods as keys, fields they apply to as values
                    // a°) A NOT key is allowed to negate the condition
                    // b°) Boolean true means all fields, false means the formatter is not used
                    // c°) Strings or array of strings are allowed
                        // * regular expressions (delimited by "/") are used to filter field names
                        // * other strings are used to filter field types
                'formatters' => [
                    // Extract the part after the last "_" character
                    '\\Database\\Utility\\Formatter::formatSuffix' => '/_id$/',
                    // Trim the value
                    '\\Database\\Utility\\Formatter::formatTrim' => [ 'NOT' => 'binary'],
                    // Transform empty string to a NULL value
                    '\\Database\\Utility\\Formatter::formatNull' => true,
                    // Tries to parse an integer value using the current intl.default_locale value
                    '\\Database\\Utility\\Formatter::formatInteger' => ['integer', 'biginteger'],
                    // Tries to parse a decimal value using the current intl.default_locale value
                    '\\Database\\Utility\\Formatter::formatDecimal' => ['decimal', 'float', 'numeric']
                ]
            ]
        );
    // ...
}

Code quality

sudo bash -c "( rm -rf logs/quality ; find logs -type f -iname '*.log' -exec rm {} \;  ; find tmp -type f ! -name 'empty' -exec rm {} \; )"
sudo -u apache ant quality -f vendor/jmjjg/cakephp3-database/vendor/Jenkins/build.xml

The Versions

22/01 2017

dev-master

9999999-dev https://github.com/jmjjg/cakephp3-database

CakePHP 3 plugin that provides classes for adding default validation rules from the database table schema and cleaning data being saved.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

database cakephp validation marshalling

22/01 2017

1.0.0-RC1

1.0.0.0-RC1 https://github.com/jmjjg/cakephp3-database

CakePHP 3 plugin that provides classes for adding default validation rules from the database table schema and cleaning data being saved.

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

database cakephp validation marshalling