Merge branch '2.0' of github.com:cakephp/cakephp into 2.0

This commit is contained in:
AD7six 2011-04-18 12:06:11 +02:00
commit da9ee49db9
5 changed files with 166 additions and 100 deletions

View file

@ -2589,7 +2589,7 @@ class Model extends Object {
'allowEmpty' => null,
'required' => null,
'rule' => 'blank',
'last' => false,
'last' => true,
'on' => null
);
@ -2728,7 +2728,7 @@ class Model extends Object {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
$this->validationErrors[$field] = $value;
$this->validationErrors[$field] []= $value;
}
/**

View file

@ -469,8 +469,8 @@ class FormHelper extends AppHelper {
* - `class` string The classname for the error message
*
* @param string $field A field name, like "Modelname.fieldname"
* @param mixed $text Error message or array of $options. If array, `attributes` key
* will get used as html attributes for error container
* @param mixed $text Error message as string or array of messages.
* If array contains `attributes` key it will be used as options for error container
* @param array $options Rendering options for <div /> wrapper tag
* @return string If there are errors this method returns an error message, otherwise null.
* @access public
@ -482,36 +482,63 @@ class FormHelper extends AppHelper {
$this->setEntity($field);
if ($error = $this->tagIsInvalid()) {
if (is_array($error)) {
list(,,$field) = explode('.', $field);
if (isset($error[$field])) {
$error = $error[$field];
} else {
return null;
}
}
if (is_array($text) && is_numeric($error) && $error > 0) {
$error--;
}
if (is_array($text)) {
$options = array_merge($options, array_intersect_key($text, $defaults));
if (isset($text['attributes']) && is_array($text['attributes'])) {
$options = array_merge($options, $text['attributes']);
unset($text['attributes']);
}
$text = isset($text[$error]) ? $text[$error] : null;
unset($options[$error]);
$tmp = array();
foreach ($error as &$e) {
if (isset($text[$e])) {
$tmp []= $text[$e];
} else {
$tmp []= $e;
}
}
$text = $tmp;
}
if ($text != null) {
$error = $text;
} elseif (is_numeric($error)) {
$error = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
}
if (is_array($error)) {
foreach ($error as &$e) {
if (is_numeric($e)) {
$e = __d('cake', 'Error in field %s', Inflector::humanize($this->field()));
}
}
}
if ($options['escape']) {
$error = h($error);
unset($options['escape']);
}
if (is_array($error)) {
if (count($error) > 1) {
$listParams = array();
if (isset($options['listOptions'])) {
if (is_string($options['listOptions'])) {
$listParams []= $options['listOptions'];
} else {
if (isset($options['listOptions']['itemOptions'])) {
$listParams []= $options['listOptions']['itemOptions'];
unset($options['listOptions']['itemOptions']);
} else {
$listParams []= array();
}
if (isset($options['listOptions']['tag'])) {
$listParams []= $options['listOptions']['tag'];
unset($options['listOptions']['tag']);
}
array_unshift($listParams, $options['listOptions']);
}
unset($options['listOptions']);
}
array_unshift($listParams, $error);
$error = call_user_func_array(array($this->Html, 'nestedList'), $listParams);
} else {
$error = array_pop($error);
}
}
if ($options['wrap']) {
$tag = is_string($options['wrap']) ? $options['wrap'] : 'div';
unset($options['wrap']);

View file

@ -49,7 +49,7 @@ class ModelValidationTest extends BaseModelTest {
'validator' => array(
'rule' => 'customValidatorWithParams',
'on' => null,
'last' => false,
'last' => true,
'allowEmpty' => false,
'required' => true
),
@ -63,7 +63,7 @@ class ModelValidationTest extends BaseModelTest {
'required' => true
);
$expected = array(
'title' => 'This field will *never* validate! Muhahaha!'
'title' => array('This field will *never* validate! Muhahaha!')
);
$this->assertEqual($TestModel->invalidFields(), $expected);
@ -85,7 +85,7 @@ class ModelValidationTest extends BaseModelTest {
'five' => array(
'rule' => array(1 => 'one', 2 => 'two', 3 => null, 4 => 'four'),
'on' => null,
'last' => false,
'last' => true,
'allowEmpty' => false,
'required' => true
),
@ -111,7 +111,7 @@ class ModelValidationTest extends BaseModelTest {
'six' => array(
'rule' => array(1 => 'one', 2 => array('two'), 3 => null, 4 => 'four', 5 => array('five' => 5)),
'on' => null,
'last' => false,
'last' => true,
'allowEmpty' => false,
'required' => true
)
@ -139,29 +139,29 @@ class ModelValidationTest extends BaseModelTest {
$TestModel->set(array('title' => '$$', 'name' => '##'));
$TestModel->invalidFields(array('fieldList' => array('title')));
$expected = array(
'title' => 'This field cannot be left blank'
'title' => array('This field cannot be left blank')
);
$this->assertEqual($TestModel->validationErrors, $expected);
$TestModel->validationErrors = array();
$TestModel->invalidFields(array('fieldList' => array('name')));
$expected = array(
'name' => 'This field cannot be left blank'
'name' => array('This field cannot be left blank')
);
$this->assertEqual($TestModel->validationErrors, $expected);
$TestModel->validationErrors = array();
$TestModel->invalidFields(array('fieldList' => array('name', 'title')));
$expected = array(
'name' => 'This field cannot be left blank',
'title' => 'This field cannot be left blank'
'name' => array('This field cannot be left blank'),
'title' => array('This field cannot be left blank')
);
$this->assertEqual($TestModel->validationErrors, $expected);
$TestModel->validationErrors = array();
$TestModel->whitelist = array('name');
$TestModel->invalidFields();
$expected = array('name' => 'This field cannot be left blank');
$expected = array('name' => array('This field cannot be left blank'));
$this->assertEqual($TestModel->validationErrors, $expected);
$this->assertEqual($TestModel->validate, $validate);
@ -187,7 +187,7 @@ class ModelValidationTest extends BaseModelTest {
$TestModel->whitelist = array('name');
$TestModel->save(array('name' => '#$$#', 'title' => '$$$$'));
$expected = array('name' => 'This field cannot be left blank');
$expected = array('name' => array('This field cannot be left blank'));
$this->assertEqual($TestModel->validationErrors, $expected);
}
@ -518,7 +518,7 @@ class ModelValidationTest extends BaseModelTest {
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'onlyLetters'
'title' => array('tooShort')
);
$this->assertEqual($result, $expected);
@ -526,7 +526,7 @@ class ModelValidationTest extends BaseModelTest {
'title' => array(
'tooShort' => array(
'rule' => array('minLength', 50),
'last' => true
'last' => false
),
'onlyLetters' => array('rule' => '/^[a-z]+$/i')
),
@ -539,7 +539,7 @@ class ModelValidationTest extends BaseModelTest {
$this->assertFalse($result);
$result = $TestModel->validationErrors;
$expected = array(
'title' => 'tooShort'
'title' => array('tooShort', 'onlyLetters')
);
$this->assertEqual($result, $expected);
}
@ -569,7 +569,7 @@ class ModelValidationTest extends BaseModelTest {
$JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
$expectedError = array('doomed' => 'This field cannot be left blank');
$expectedError = array('doomed' => array('This field cannot be left blank'));
$Something->create();
$result = $Something->save($data);
@ -621,7 +621,7 @@ class ModelValidationTest extends BaseModelTest {
$JoinThing =& $Something->JoinThing;
$JoinThing->validate = array('doomed' => array('rule' => 'notEmpty'));
$expectedError = array('doomed' => 'This field cannot be left blank');
$expectedError = array('doomed' => array('This field cannot be left blank'));
$Something->create();
$result = $Something->saveAll($data, array('validate' => 'only'));

View file

@ -2765,8 +2765,8 @@ class ModelWriteTest extends BaseModelTest {
array('validate' => 'first')
), false);
$expected = array(
'Comment' => array('comment' => 'This field cannot be left blank'),
'Attachment' => array('attachment' => 'This field cannot be left blank')
'Comment' => array('comment' => array('This field cannot be left blank')),
'Attachment' => array('attachment' => array('This field cannot be left blank'))
);
$this->assertEqual($model->validationErrors, $expected['Comment']);
$this->assertEqual($model->Attachment->validationErrors, $expected['Attachment']);
@ -2956,11 +2956,11 @@ class ModelWriteTest extends BaseModelTest {
$this->assertEqual($result, $expected);
$expected = array('Comment' => array(
array('comment' => 'This field cannot be left blank')
array('comment' => array('This field cannot be left blank'))
));
$this->assertEqual($TestModel->validationErrors, $expected);
$expected = array(
array('comment' => 'This field cannot be left blank')
array('comment' => array('This field cannot be left blank'))
);
$this->assertEqual($TestModel->Comment->validationErrors, $expected);
@ -3356,7 +3356,7 @@ class ModelWriteTest extends BaseModelTest {
$this->assertFalse($result);
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
$errors = array(1 => array('title' => 'This field cannot be left blank'));
$errors = array(1 => array('title' => array('This field cannot be left blank')));
$transactionWorked = Set::matches('/Post[1][title=Baleeted First Post]', $result);
if (!$transactionWorked) {
$this->assertTrue(Set::matches('/Post[1][title=Un-Baleeted First Post]', $result));
@ -3381,7 +3381,7 @@ class ModelWriteTest extends BaseModelTest {
$result = $TestModel->saveAll($data, array('validate' => true, 'atomic' => false));
$this->assertEqual($result, array(true, false));
$result = $TestModel->find('all', array('recursive' => -1, 'order' => 'Post.id ASC'));
$errors = array(1 => array('title' => 'This field cannot be left blank'));
$errors = array(1 => array('title' => array('This field cannot be left blank')));
$newTs = date('Y-m-d H:i:s');
$expected = array(
array(
@ -3494,7 +3494,7 @@ class ModelWriteTest extends BaseModelTest {
);
$this->assertFalse($result);
$expected = array(
0 => array('title' => 'This field cannot be left blank'),
0 => array('title' => array('This field cannot be left blank')),
);
$this->assertEqual($TestModel->validationErrors, $expected);
@ -3508,7 +3508,7 @@ class ModelWriteTest extends BaseModelTest {
);
$this->assertFalse($result);
$expected = array(
1 => array('title' => 'This field cannot be left blank'),
1 => array('title' => array('This field cannot be left blank')),
);
$this->assertEqual($TestModel->validationErrors, $expected);
}
@ -3541,7 +3541,7 @@ class ModelWriteTest extends BaseModelTest {
$result = $model->find('all');
$this->assertEqual($result, array());
$expected = array('Comment' => array(
1 => array('comment' => 'This field cannot be left blank')
1 => array('comment' => array('This field cannot be left blank'))
));
$this->assertEqual($model->Comment->validationErrors, $expected['Comment']);
@ -3747,14 +3747,14 @@ class ModelWriteTest extends BaseModelTest {
$this->assertIdentical($result, $expected);
$expected = array('Comment' => array(
0 => array('comment' => 'This field cannot be left blank'),
2 => array('comment' => 'This field cannot be left blank')
0 => array('comment' => array('This field cannot be left blank')),
2 => array('comment' => array('This field cannot be left blank'))
));
$this->assertEqual($TestModel->validationErrors, $expected);
$expected = array(
0 => array('comment' => 'This field cannot be left blank'),
2 => array('comment' => 'This field cannot be left blank')
0 => array('comment' => array('This field cannot be left blank')),
2 => array('comment' => array('This field cannot be left blank'))
);
$this->assertEqual($TestModel->Comment->validationErrors, $expected);
}

View file

@ -821,7 +821,7 @@ class FormHelperTest extends CakeTestCase {
function testFormSecurityFields() {
$key = 'testKey';
$fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
$this->Form->request['_Token'] = array('key' => $key);
$result = $this->Form->secure($fields);
@ -1292,7 +1292,7 @@ class FormHelperTest extends CakeTestCase {
* @return void
*/
function testPasswordValidation() {
$this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
$this->Form->validationErrors['Contact']['password'] = array('Please provide a password');
$result = $this->Form->input('Contact.password');
$expected = array(
'div' => array('class' => 'input password error'),
@ -1345,7 +1345,7 @@ class FormHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$expected = array('OpenidUrl' => array('openid_not_registered' => true));
$expected = array('OpenidUrl' => array('openid_not_registered' => array(true)));
$this->assertEqual($this->Form->validationErrors, $expected);
$result = $this->Form->error(
@ -1389,8 +1389,8 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$expected = array(
'ValidateUser' => array('email' => true),
'ValidateProfile' => array('full_name' => true, 'city' => true)
'ValidateUser' => array('email' => array(true)),
'ValidateProfile' => array('full_name' => array(true), 'city' => array(true))
);
$this->assertEqual($this->Form->validationErrors, $expected);
@ -1434,9 +1434,9 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$expected = array(
'ValidateUser' => array('email' => true),
'ValidateProfile' => array('full_name' => true, 'city' => true),
'ValidateItem' => array('description' => true)
'ValidateUser' => array('email' => array(true)),
'ValidateProfile' => array('full_name' => array(true), 'city' => array(true)),
'ValidateItem' => array('description' => array(true))
);
$this->assertEqual($this->Form->validationErrors, $expected);
@ -1455,7 +1455,7 @@ class FormHelperTest extends CakeTestCase {
*/
function testFormValidationMultiRecord() {
$this->Form->validationErrors['Contact'] = array(2 => array(
'name' => 'This field cannot be left blank'
'name' => array('This field cannot be left blank')
));
$result = $this->Form->input('Contact.2.name');
$expected = array(
@ -1473,25 +1473,6 @@ class FormHelperTest extends CakeTestCase {
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['UserForm'] = array(
'OpenidUrl' => array('url' => 'You must provide a URL'
));
$this->Form->create('UserForm');
$result = $this->Form->input('OpenidUrl.url');
$expected = array(
'div' => array('class'),
'label' => array('for'),
'preg:/[^<]+/',
'/label',
'input' => array(
'type' => 'text', 'name', 'id', 'class' => 'form-error'
),
array('div' => array('class' => 'error-message')),
'You must provide a URL',
'/div',
'/div'
);
}
/**
@ -1504,9 +1485,9 @@ class FormHelperTest extends CakeTestCase {
*/
function testMultipleInputValidation() {
$this->Form->create();
$this->Form->validationErrors['Address'][0]['title'] = 'This field cannot be empty';
$this->Form->validationErrors['Address'][0]['first_name'] = 'This field cannot be empty';
$this->Form->validationErrors['Address'][1]['last_name'] = 'You must have a last name';
$this->Form->validationErrors['Address'][0]['title'] = array('This field cannot be empty');
$this->Form->validationErrors['Address'][0]['first_name'] = array('This field cannot be empty');
$this->Form->validationErrors['Address'][1]['last_name'] = array('You must have a last name');
$result = $this->Form->input('Address.0.title');
$expected = array(
@ -1713,7 +1694,7 @@ class FormHelperTest extends CakeTestCase {
unset($this->Form->request->data);
$this->Form->validationErrors['Model']['field'] = 'Badness!';
$this->Form->validationErrors['Model']['field'] = array('Badness!');
$result = $this->Form->input('Model.field');
$expected = array(
'div' => array('class' => 'input text error'),
@ -1732,7 +1713,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'div' => false, 'error' => array('wrap' => 'span')
'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
));
$expected = array(
'label' => array('for' => 'ModelField'),
@ -1749,7 +1730,7 @@ class FormHelperTest extends CakeTestCase {
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.field', array(
'div' => array('tag' => 'span'), 'error' => array('wrap' => false)
'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
));
$expected = array(
'span' => array('class' => 'input text error'),
@ -1812,7 +1793,7 @@ class FormHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = 'minLength';
$this->Form->validationErrors['Model']['field'] = array('minLength');
$result = $this->Form->input('Model.field', array(
'error' => array(
'minLength' => 'Le login doit contenir au moins 2 caractères',
@ -1832,11 +1813,10 @@ class FormHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = 'maxLength';
$this->Form->validationErrors['Model']['field'] = array('maxLength');
$result = $this->Form->input('Model.field', array(
'error' => array(
'wrap' => 'span',
'attributes' => array('rel' => 'fake'),
'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
'minLength' => 'Le login doit contenir au moins 2 caractères',
'maxLength' => 'login too large',
)
@ -2676,18 +2656,18 @@ class FormHelperTest extends CakeTestCase {
* @return void
*/
function testError() {
$this->Form->validationErrors['Model']['field'] = 1;
$this->Form->validationErrors['Model']['field'] = array(1);
$result = $this->Form->error('Model.field');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
$result = $this->Form->error('Model.field', null, array('wrap' => false));
$this->assertEqual($result, 'Error in field Field');
$this->Form->validationErrors['Model']['field'] = "This field contains invalid input";
$this->Form->validationErrors['Model']['field'] = array("This field contains invalid input");
$result = $this->Form->error('Model.field', null, array('wrap' => false));
$this->assertEqual($result, 'This field contains invalid input');
$this->Form->validationErrors['Model']['field'] = "This field contains invalid input";
$this->Form->validationErrors['Model']['field'] = array("This field contains invalid input");
$result = $this->Form->error('Model.field', null, array('wrap' => 'span'));
$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
@ -2703,14 +2683,73 @@ class FormHelperTest extends CakeTestCase {
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
$this->assertEqual($result, '<strong>Badness!</strong>');
$this->Form->validationErrors['Model']['field'] = "email";
$result = $this->Form->error('Model.field', array('class' => 'field-error', 'email' => 'No good!'));
$this->Form->validationErrors['Model']['field'] = array("email");
$result = $this->Form->error('Model.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
$expected = array(
'div' => array('class' => 'field-error'),
'No good!',
'/div'
);
$this->assertTags($result, $expected);
$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email', 'Something else');
$result = $this->Form->error('Model.field', array(
'notEmpty' => 'Cannot be empty',
'email' => 'No good!'
));
$expected = array(
'div' => array('class' => 'error-message'),
'ul' => array(),
'<li', 'Cannot be empty', '/li',
'<li', 'No good!', '/li',
'<li', 'Something else', '/li',
'/ul',
'/div'
);
$this->assertTags($result, $expected);
/** Testing error messages list options **/
$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email');
$result = $this->Form->error('Model.field', null, array('listOptions' => 'ol'));
$expected = array(
'div' => array('class' => 'error-message'),
'ol' => array(),
'<li', 'notEmpty', '/li',
'<li', 'email', '/li',
'/ol',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->error('Model.field', null, array('listOptions' => array('tag' => 'ol')));
$expected = array(
'div' => array('class' => 'error-message'),
'ol' => array(),
'<li', 'notEmpty', '/li',
'<li', 'email', '/li',
'/ol',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->error('Model.field', null, array(
'listOptions' => array(
'class' => 'ul-class',
'itemOptions' => array(
'class' => 'li-class'
)
)
));
$expected = array(
'div' => array('class' => 'error-message'),
'ul' => array('class' => 'ul-class'),
array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
array('li' => array('class' => 'li-class')), 'email', '/li',
'/ul',
'/div'
);
$this->assertTags($result, $expected);
}
/**
@ -2720,11 +2759,11 @@ class FormHelperTest extends CakeTestCase {
*/
function testInputErrorEscape() {
$this->Form->create('ValidateProfile');
$this->Form->validationErrors['ValidateProfile']['city'] = 'required<br>';
$result = $this->Form->input('city',array('error' => array('escape' => true)));
$this->Form->validationErrors['ValidateProfile']['city'] = array('required<br>');
$result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
$this->assertPattern('/required&lt;br&gt;/', $result);
$result = $this->Form->input('city',array('error' => array('escape' => false)));
$result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
$this->assertPattern('/required<br>/', $result);
}
@ -6033,7 +6072,7 @@ class FormHelperTest extends CakeTestCase {
'email' => 'nate@example.com'
));
$this->Form->request->addParams(array(
'models' => array('Person'),
'models' => array('Person'),
'controller' => 'people',
'action' => 'add'
));
@ -6850,11 +6889,11 @@ class FormHelperTest extends CakeTestCase {
*/
function testMultiRecordFormValidationErrors() {
$this->Form->create('ValidateProfile');
$this->Form->validationErrors['ValidateProfile'][2]['ValidateItem'][1]['name'] = 'Error in field name';
$this->Form->validationErrors['ValidateProfile'][2]['ValidateItem'][1]['name'] = array('Error in field name');
$result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
$this->Form->validationErrors['ValidateProfile'][2]['city'] = 'Error in field city';
$this->Form->validationErrors['ValidateProfile'][2]['city'] = array('Error in field city');
$result = $this->Form->error('ValidateProfile.2.city');
$this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
@ -6948,7 +6987,7 @@ class FormHelperTest extends CakeTestCase {
'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
);
$this->assertTags($result, $expected);
$result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
$expected = array(
'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
@ -6957,7 +6996,7 @@ class FormHelperTest extends CakeTestCase {
}
/**
*
*
* @expectedException CakeException
* @return void
*/