浏览代码

Add ConfirmableBehavior.php

Mark Scherer 11 年之前
父节点
当前提交
ea73a57a71
共有 2 个文件被更改,包括 128 次插入0 次删除
  1. 64 0
      src/Model/Behavior/ConfirmableBehavior.php
  2. 64 0
      tests/TestCase/Model/Behavior/ConfirmableBehaviorTest.php

+ 64 - 0
src/Model/Behavior/ConfirmableBehavior.php

@@ -0,0 +1,64 @@
+<?php
+namespace Tools\Model\Behavior;
+
+use Cake\Core\Configure;
+use Cake\ORM\Behavior;
+use Cake\ORM\Entity;
+use Cake\ORM\Query;
+use Cake\ORM\Table;
+use Cake\Event\Event;
+use ArrayObject;
+
+/**
+ * ConfirmableBehavior allows forms to easily require a checkbox toggled (confirmed).
+ * Example: Terms of use on registration forms or some "confirm delete checkbox"
+ *
+ * Copyright 2011, dereuromark (http://www.dereuromark.de)
+ *
+ * @link http://github.com/dereuromark/
+ * @license http://opensource.org/licenses/mit-license.php MIT
+ * @link http://www.dereuromark.de/2011/07/05/introducing-two-cakephp-behaviors/
+ */
+class ConfirmableBehavior extends Behavior {
+
+	protected $_defaultConfig = [
+		'message' => null,
+		'field' => 'confirm',
+		//'table' => null,
+		'validator' => 'default',
+	];
+
+	public function __construct(Table $table, array $config = []) {
+		parent::__construct($table, $config);
+
+		if (!$this->_config['message']) {
+			$this->_config['message'] =  __d('tools', 'Please confirm the checkbox');
+		}
+	}
+
+	/**
+	 * ConfirmableBehavior::initialize()
+	 *
+	 * @param mixed $config
+	 * @return void
+	 */
+	public function initialize(array $config) {
+		$validator = $this->_table->validator($this->_config['validator']);
+
+		$field = $this->_config['field'];
+		$message = $this->_config['message'];
+		$validator->add($field, 'notEmpty', [
+				'rule' => function ($value, $context) {
+					return !empty($value);
+				},
+				'message' => $message,
+				//'provider' => 'table',
+				'requirePresence' => true,
+				'allowEmpty' => false,
+				'last' => true]
+		);
+		$validator->requirePresence($field);
+		//$validator->allowEmpty($field, false);
+	}
+
+}

+ 64 - 0
tests/TestCase/Model/Behavior/ConfirmableBehaviorTest.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace Tools\Test\TestCase\Model\Behavior;
+
+use Cake\ORM\TableRegistry;
+use Tools\TestSuite\TestCase;
+//use Cake\Core\Configure;
+use Tools\Model\Behavior\ConfirmableBehavior;
+
+class ConfirmableBehaviorTest extends TestCase {
+
+	public $ConfirmableBehavior;
+
+	public $fixtures = array('plugin.Tools.SluggedArticles');
+
+	public function setUp() {
+		parent::setUp();
+	}
+
+	/**
+	 * ConfirmableBehaviorTest::testBasicValidation()
+	 *
+	 * @return void
+	 */
+	public function testBasicValidation() {
+		$this->Animals = TableRegistry::get('SluggedArticles');
+		$this->Animals->addBehavior('Tools.Confirmable');
+
+		$animal = $this->Animals->newEntity();
+
+		$data = array(
+			'name' => 'FooBar',
+			'confirm' => '0'
+		);
+		$animal = $this->Animals->patchEntity($animal, $data);
+		$this->assertNotEmpty($animal->errors());
+		$this->assertSame(array('confirm' => array('notEmpty' => 'The provided value is invalid')), $animal->errors());
+
+		$data = array(
+			'name' => 'FooBar',
+			'confirm' => '1'
+		);
+		$animal = $this->Animals->patchEntity($animal, $data);
+		$this->assertEmpty($animal->errors());
+	}
+
+	/**
+	 * ConfirmableBehaviorTest::testValidationFieldMissing()
+	 *
+	 * @return void
+	 */
+	public function testValidationFieldMissing() {
+		$this->Animals = TableRegistry::get('SluggedArticles');
+		$this->Animals->addBehavior('Tools.Confirmable');
+
+		$animal = $this->Animals->newEntity();
+		$data = array(
+			'name' => 'FooBar'
+		);
+		$animal = $this->Animals->patchEntity($animal, $data);
+		$this->assertSame(array('confirm' => array('This field is required')), $animal->errors());
+	}
+
+}