Commit: b94ab813c7e040ce044b38a120247b99b9aac2fc

Author: gwoo | Date: 2010-01-25 15:16:38 -0800
adding isUnique validation to Plugin model
diff --git a/models/Plugin.php b/models/Plugin.php index 204f9da..f610921 100644 --- a/models/Plugin.php +++ b/models/Plugin.php @@ -34,7 +34,10 @@ class Plugin extends \lithium\data\Model { * @var string */ public $validates = array( - 'name' => 'You must specify a name for this plugin.', + 'name' => array( + array('notEmpty', 'message' => 'You must specify a name for this plugin.'), + array('isUnique', 'message' => 'Name must be unique.') + ), 'version' => 'You must specify a version for this plugin.', 'summary' => 'You must specify a short summary for this plugin', 'sources' => array( @@ -62,6 +65,19 @@ class Plugin extends \lithium\data\Model { return false; } }); + $self = static::_instance(); + Validator::add('isUnique', function ($data, $params, $options) use ($self){ + $plugin = $self::find('first', array( + 'conditions' => array( + 'design' => 'by_field', 'view' => 'name', + 'key' => json_encode($data) + ) + )); + if (!empty($plugin->name)) { + return false; + } + return true; + }); } /** diff --git a/tests/cases/models/PluginTest.php b/tests/cases/models/PluginTest.php index b2dba9e..4bdd054 100644 --- a/tests/cases/models/PluginTest.php +++ b/tests/cases/models/PluginTest.php @@ -48,7 +48,10 @@ class PluginTest extends \lithium\test\Unit { $this->assertTrue(!empty($result)); $expected = array( - 'name' => array('You must specify a name for this plugin.'), + 'name' => array( + 'You must specify a name for this plugin.', + 'Name must be unique.' + ), 'version' => array('You must specify a version for this plugin.'), 'summary' => array('You must specify a short summary for this plugin'), 'sources' => array('You must specify a source for this plugin.'), @@ -73,7 +76,7 @@ class PluginTest extends \lithium\test\Unit { $expected = 'li3_bot'; $result = $plugin->name; $this->assertEqual($expected, $result); - + MockPlugin::create()->delete(); } }