Browse files

Fix validation detection in FormHelper

string values for validation means a single validation method.
That implies the field is required.

Fixes #2181
  • Loading branch information...
1 parent 11e2ef6 commit ce9bb6b632858d80467d269a6970c229c670698b @markstory markstory committed Oct 28, 2011
Showing with 111 additions and 84 deletions.
  1. +107 −82 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +4 −2 lib/Cake/View/Helper/FormHelper.php
View
189 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -104,6 +104,7 @@ class Contact extends CakeTestModel {
'non_existing' => array(),
'idontexist' => array(),
'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
+ 'string_required' => 'notEmpty',
'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
'imrequiredtoo' => array('rule' => 'notEmpty'),
'required_one' => array('required' => array('rule' => array('notEmpty'))),
@@ -6585,91 +6586,12 @@ public function testEditFormWithData() {
}
/**
- * testFormMagicInput method
+ * Test that required fields are created for various types of validation.
*
* @return void
*/
- public function testFormMagicInput() {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create('Contact');
- $expected = array(
- 'form' => array(
- 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
- 'accept-charset' => $encoding
- ),
- 'div' => array('style' => 'display:none;'),
- 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('name');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'ContactName'),
- 'Name',
- '/label',
- 'input' => array(
- 'type' => 'text', 'name' => 'data[Contact][name]',
- 'id' => 'ContactName', 'maxlength' => '255'
- ),
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('non_existing_field_in_contact_model');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
- 'Non Existing Field In Contact Model',
- '/label',
- 'input' => array(
- 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
- 'id' => 'ContactNonExistingFieldInContactModel'
- ),
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('Address.street');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'AddressStreet'),
- 'Street',
- '/label',
- 'input' => array(
- 'type' => 'text', 'name' => 'data[Address][street]',
- 'id' => 'AddressStreet'
- ),
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('Address.non_existing_field_in_model');
- $expected = array(
- 'div' => array('class' => 'input text'),
- 'label' => array('for' => 'AddressNonExistingFieldInModel'),
- 'Non Existing Field In Model',
- '/label',
- 'input' => array(
- 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
- 'id' => 'AddressNonExistingFieldInModel'
- ),
- '/div'
- );
- $this->assertTags($result, $expected);
-
- $result = $this->Form->input('name', array('div' => false));
- $expected = array(
- 'label' => array('for' => 'ContactName'),
- 'Name',
- '/label',
- 'input' => array(
- 'type' => 'text', 'name' => 'data[Contact][name]',
- 'id' => 'ContactName', 'maxlength' => '255'
- )
- );
- $this->assertTags($result, $expected);
+ function testFormInputRequiredDetection() {
+ $this->Form->create('Contact');
$result = $this->Form->input('Contact.non_existing');
$expected = array(
@@ -6741,6 +6663,20 @@ public function testFormMagicInput() {
);
$this->assertTags($result, $expected);
+ $result = $this->Form->input('Contact.string_required');
+ $expected = array(
+ 'div' => array('class' => 'input text required'),
+ 'label' => array('for' => 'ContactStringRequired'),
+ 'String Required',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Contact][string_required]',
+ 'id' => 'ContactStringRequired'
+ ),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
$result = $this->Form->input('Contact.imnotrequired');
$expected = array(
'div' => array('class' => 'input text'),
@@ -6782,6 +6718,95 @@ public function testFormMagicInput() {
'/div'
);
$this->assertTags($result, $expected);
+ }
+
+/**
+ * testFormMagicInput method
+ *
+ * @return void
+ */
+ public function testFormMagicInput() {
+ $encoding = strtolower(Configure::read('App.encoding'));
+ $result = $this->Form->create('Contact');
+ $expected = array(
+ 'form' => array(
+ 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
+ 'accept-charset' => $encoding
+ ),
+ 'div' => array('style' => 'display:none;'),
+ 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('name');
+ $expected = array(
+ 'div' => array('class' => 'input text'),
+ 'label' => array('for' => 'ContactName'),
+ 'Name',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Contact][name]',
+ 'id' => 'ContactName', 'maxlength' => '255'
+ ),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('non_existing_field_in_contact_model');
+ $expected = array(
+ 'div' => array('class' => 'input text'),
+ 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
+ 'Non Existing Field In Contact Model',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
+ 'id' => 'ContactNonExistingFieldInContactModel'
+ ),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('Address.street');
+ $expected = array(
+ 'div' => array('class' => 'input text'),
+ 'label' => array('for' => 'AddressStreet'),
+ 'Street',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Address][street]',
+ 'id' => 'AddressStreet'
+ ),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('Address.non_existing_field_in_model');
+ $expected = array(
+ 'div' => array('class' => 'input text'),
+ 'label' => array('for' => 'AddressNonExistingFieldInModel'),
+ 'Non Existing Field In Model',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
+ 'id' => 'AddressNonExistingFieldInModel'
+ ),
+ '/div'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->input('name', array('div' => false));
+ $expected = array(
+ 'label' => array('for' => 'ContactName'),
+ 'Name',
+ '/label',
+ 'input' => array(
+ 'type' => 'text', 'name' => 'data[Contact][name]',
+ 'id' => 'ContactName', 'maxlength' => '255'
+ )
+ );
+ $this->assertTags($result, $expected);
+
extract($this->dateRegex);
$now = strtotime('now');
View
6 lib/Cake/View/Helper/FormHelper.php
@@ -239,14 +239,16 @@ protected function _introspectModel($model, $key, $field = null) {
}
/**
- * Returns if a field is required to be filled based on validation properties from the validating object
+ * Returns if a field is required to be filled based on validation properties from the validating object.
*
* @param array $validateProperties
* @return boolean true if field is required to be filled, false otherwise
*/
protected function _isRequiredField($validateProperties) {
$required = false;
- if (is_array($validateProperties)) {
+ if (is_string($validateProperties)) {
+ return true;
+ } elseif (is_array($validateProperties)) {
$dims = Set::countDim($validateProperties);
if ($dims == 1 || ($dims == 2 && isset($validateProperties['rule']))) {

0 comments on commit ce9bb6b

Please sign in to comment.