Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add README.md and testcase.

  • Loading branch information...
commit 725f373ce9b8f314d519a1b0eb5a81a46754e5b2 1 parent a760585
@k1LoW authored
View
5 Model/Behavior/AdditionalValidationRulesBehavior.php
@@ -27,6 +27,9 @@ public function notEmptyWith(Model $model, $field, $with = array()){
$value = array_shift($field);
$v = new Validation();
foreach ((array)$with as $withField) {
+ if (!array_key_exists($withField, $model->data[$model->alias])) {
+ continue;
+ }
if($v->notEmpty($model->data[$model->alias][$withField])) {
return $v->notEmpty($value);
}
@@ -42,7 +45,7 @@ public function notEmptyWith(Model $model, $field, $with = array()){
public function katakanaAndSpace(Model $model, $field){
$key = key($field);
$value = array_shift($field);
- $field = array($key => preg_replace('/ /','', $value));
+ $field = array($key => str_replace(' ','', $value));
return $model->katakanaOnly($field);
}
}
View
11 README.md
@@ -0,0 +1,11 @@
+# Yav: Yer Another Validation Set
+
+My own custom Validation Set
+
+## Require
+
+- Cakeplus
+
+## License
+
+under MIT License
View
87 Test/Case/Model/Behavior/AdditionalValidationRulesTest.php
@@ -0,0 +1,87 @@
+<?php
+
+class YavPost extends CakeTestModel {
+
+ public $name = 'YavPost';
+ public $actsAs = array(
+ 'Cakeplus.AddValidationRule',
+ 'Yav.AdditionalValidationRules'
+ );
+
+ public $validate = array(
+ 'not_empty_with1' => array('notEmptyWith' => array('rule' => array('notEmptyWith', array('not_empty_with2', 'not_empty_with3')))),
+ 'katakana_and_space' => array('katakanaAndSpace' => array('rule' => array('katakanaAndSpace'))),
+ );
+}
+
+class AdditionalValidationRulesTest extends CakeTestCase {
+
+ public $fixtures = array('plugin.yav.yav_post');
+
+ /**
+ * setUp
+ *
+ */
+ public function setUp(){
+ $this->YavPost = ClassRegistry::init('YavPost');
+ }
+
+ /**
+ * tearDown
+ *
+ */
+ public function tearDown(){
+ unset($this->YavPost);
+ }
+
+ /**
+ * test_notEmptyWith
+ *
+ */
+ public function test_notEmptyWith(){
+ $data = array(
+ 'YavPost' => array(
+ 'title' => 'タイトル',
+ 'not_empty_with1' => '',
+ ),
+ );
+ $this->assertIdentical( $this->YavPost->create( $data ) , $data);
+ $this->YavPost->validates();
+ $this->assertFalse( array_key_exists('not_empty_with1' , $this->YavPost->validationErrors ) );
+
+ $data = array(
+ 'YavPost' => array(
+ 'title' => 'タイトル',
+ 'not_empty_with1' => '',
+ 'not_empty_with3' => '空でない',
+ ),
+ );
+ $this->assertIdentical( $this->YavPost->create( $data ) , $data);
+ $this->YavPost->validates();
+ $this->assertTrue( array_key_exists('not_empty_with1' , $this->YavPost->validationErrors ) );
+ }
+
+ /**
+ * test_katakanaAndSpace
+ *
+ */
+ public function test_katakanaAndSpace(){
+ $data = array(
+ 'YavPost' => array(
+ 'katakana_and_space' => 'hiragana',
+ ),
+ );
+ $this->assertIdentical( $this->YavPost->create( $data ) , $data);
+ $this->YavPost->validates();
+ $this->assertTrue( array_key_exists('katakana_and_space' , $this->YavPost->validationErrors ) );
+
+ $data = array(
+ 'YavPost' => array(
+ 'katakana_and_space' => 'カタカナ ト スペース',
+ ),
+ );
+ $this->assertIdentical( $this->YavPost->create( $data ) , $data);
+ $this->YavPost->validates();
+ $this->assertFalse( array_key_exists('katakana_and_space' , $this->YavPost->validationErrors ) );
+ }
+}
View
16 Test/Fixture/YavPostFixture.php
@@ -0,0 +1,16 @@
+<?php
+class YavPostFixture extends CakeTestFixture {
+ var $name = 'YavPost';
+
+ var $fields = array(
+ 'id' => array('type' => 'integer', 'null' => false, 'default' => NULL, 'length' => 20, 'key' => 'primary'),
+ 'title' => array('type' => 'text', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'not_empty_with1' => array('type' => 'text', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'not_empty_with2' => array('type' => 'text', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'not_empty_with3' => array('type' => 'text', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'katakana_and_space' => array('type' => 'text', 'null' => true, 'default' => NULL, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ );
+
+ var $records = array(
+ );
+}
Please sign in to comment.
Something went wrong with that request. Please try again.