codaxis / cakephp-multivalidation-behavior
A CakePHP behavior that allows easy modifying of validation rules on the fly.
Installs: 15
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 0
Type:cakephp-plugin
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-11-09 15:50:14 UTC
README
A CakePHP behavior that allows easy modifying of validation rules on the fly.
Compatible with Cake 2.4.7+
Feel free to make any code/docs contributions or post any issues.
How to install
You can just copy the behavior file to your APP/Model/Behavior folder
or
You can also install the plugin as with every other plugin:
- Put the files in
APP/Plugin/MultiValidation
- In your bootstrap.php add
CakePlugin::load('MultiValidation')
or justCakePlugin::loadAll()
How to use
Enable the plugin in your target model:
class User extends AppModel { public $actsAs = array( 'MultiValidation.MultiValidation' => array( 'types' => array( 'enforceUsernameAndEmail' => array( 'username' => array( 'onlyAlpha' => array( 'rule' => 'alphaNumeric' ), ), 'email' => 'validEmail' => array( 'rule' => 'email', 'required' => true ), ) ), 'enforcePassword' => array( 'password' => array( 'minCharsAllowed' => array( 'rule' => array('minLength', 8) ), ), ), ) ) ) public $validate = array( 'username' => array( 'mustNotBeBlank' => array( 'rule' => 'notEmpty' ) ), 'password' => array( 'minCharsAllowed' => array( 'rule' => array('minLength', 4) ) ), ); }
Then in your model or controller you can do the following actions:
// Add new username and email validation: $User->addValidation('enforceUsernameAndEmail'); // $User->loadedValidation() would return array('enforceUsernameAndEmail') // Reset to default state: $User->resetValidation(); // $User->loadedValidation() would return array('_default') // Load and set only the password validation: $User->loadValidation('enforcePassword'); // $User->loadedValidation() would return array('enforcePassword') // Add the other validation type also: $User->addValidation('enforceUsernameAndEmail'); // $User->loadedValidation() would return array('enforcePassword', 'enforceUsernameAndEmail') // Reset again to default state: $User->resetValidation(); // $User->loadedValidation() would return array('_default')