mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-08 04:22:40 +00:00
Merge branch '2.0-form-helper' into 2.0
This commit is contained in:
commit
108505a6a0
10 changed files with 523 additions and 344 deletions
|
@ -127,12 +127,6 @@ class Controller extends Object {
|
|||
*/
|
||||
public $viewVars = array();
|
||||
|
||||
/**
|
||||
* An array containing the class names of the models this controller uses.
|
||||
*
|
||||
* @var array Array of model objects.
|
||||
*/
|
||||
public $modelNames = array();
|
||||
|
||||
/**
|
||||
* The name of the view file to render. The name specified
|
||||
|
@ -596,9 +590,13 @@ class Controller extends Object {
|
|||
if ($modelClass === null) {
|
||||
$modelClass = $this->modelClass;
|
||||
}
|
||||
list($plugin, $modelClass) = pluginSplit($modelClass, true);
|
||||
|
||||
$this->modelNames[] = $modelClass;
|
||||
$this->uses = ($this->uses) ? $this->uses : array();
|
||||
if (!in_array($modelClass, $this->uses)) {
|
||||
$this->uses[] = $modelClass;
|
||||
}
|
||||
|
||||
list($plugin, $modelClass) = pluginSplit($modelClass, true);
|
||||
|
||||
$this->{$modelClass} = ClassRegistry::init(array(
|
||||
'class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id
|
||||
|
@ -806,34 +804,25 @@ class Controller extends Object {
|
|||
App::uses($viewClass, $plugin . 'View');
|
||||
}
|
||||
|
||||
$this->request->params['models'] = $this->modelNames;
|
||||
|
||||
$View = new $viewClass($this);
|
||||
|
||||
if (!empty($this->modelNames)) {
|
||||
$models = array();
|
||||
foreach ($this->modelNames as $currentModel) {
|
||||
if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
|
||||
$models[] = Inflector::underscore($currentModel);
|
||||
if (!empty($this->uses)) {
|
||||
foreach ($this->uses as $model) {
|
||||
list($plugin, $className) = pluginSplit($model);
|
||||
$this->request->params['models'][$model] = compact('plugin', 'className');
|
||||
}
|
||||
$isValidModel = (
|
||||
isset($this->$currentModel) && is_a($this->$currentModel, 'Model') &&
|
||||
!empty($this->$currentModel->validationErrors)
|
||||
);
|
||||
if ($isValidModel) {
|
||||
$View->validationErrors[Inflector::camelize($currentModel)] =&
|
||||
$this->$currentModel->validationErrors;
|
||||
} if ($this->uses === false || $this->uses === array()) {
|
||||
$this->request->params['models'][$this->modelClass] = array('plugin' => $this->plugin, 'className' => $this->modelClass);
|
||||
}
|
||||
}
|
||||
$models = array_diff(ClassRegistry::keys(), $models);
|
||||
|
||||
$models = ClassRegistry::keys();
|
||||
foreach ($models as $currentModel) {
|
||||
if (ClassRegistry::isKeySet($currentModel)) {
|
||||
$currentObject = ClassRegistry::getObject($currentModel);
|
||||
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
||||
$View->validationErrors[Inflector::camelize($currentModel)] =&
|
||||
$currentObject->validationErrors;
|
||||
}
|
||||
}
|
||||
if (is_a($currentObject, 'Model')) {
|
||||
$className = get_class($currentObject);
|
||||
list($plugin, $package) = pluginSplit(App::location($className));
|
||||
$this->request->params['models'][$currentObject->alias] = compact('plugin', 'className');
|
||||
$View->validationErrors[$currentObject->alias] =& $currentObject->validationErrors;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -587,6 +587,19 @@ class App {
|
|||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the package name where a class was defined to be located at
|
||||
*
|
||||
* @param string $className name of the class to obtain the package name from
|
||||
* @return string package name or null if not declared
|
||||
*/
|
||||
public static function location($className) {
|
||||
if (!empty(self::$__classMap[$className])) {
|
||||
return self::$__classMap[$className];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds classes based on $name or specific file(s) to search. Calling App::import() will
|
||||
* not construct any classes contained in the files. It will only find and require() the file.
|
||||
|
|
|
@ -702,10 +702,13 @@ class Model extends Object {
|
|||
}
|
||||
|
||||
if (!isset($this->{$assoc}) || $this->{$assoc}->name !== $className) {
|
||||
$model = array('class' => $plugin . '.' . $className, 'alias' => $assoc);
|
||||
if ($plugin) {
|
||||
$plugin .= '.';
|
||||
}
|
||||
$model = array('class' => $plugin . $className, 'alias' => $assoc);
|
||||
$this->{$assoc} = ClassRegistry::init($model);
|
||||
if ($plugin) {
|
||||
ClassRegistry::addObject($plugin . '.' . $className, $this->{$assoc});
|
||||
ClassRegistry::addObject($plugin . $className, $this->{$assoc});
|
||||
}
|
||||
if ($assoc) {
|
||||
$this->tableToModel[$this->{$assoc}->table] = $assoc;
|
||||
|
|
|
@ -425,7 +425,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$result = $Controller->loadModel('ControllerPost');
|
||||
$this->assertTrue($result);
|
||||
$this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
|
||||
$this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
|
||||
$this->assertTrue(in_array('ControllerPost', $Controller->uses));
|
||||
|
||||
ClassRegistry::flush();
|
||||
unset($Controller);
|
||||
|
@ -456,7 +456,7 @@ class ControllerTest extends CakeTestCase {
|
|||
$result = $Controller->loadModel('Comment');
|
||||
$this->assertTrue($result);
|
||||
$this->assertInstanceOf('Comment', $Controller->Comment);
|
||||
$this->assertTrue(in_array('Comment', $Controller->modelNames));
|
||||
$this->assertTrue(in_array('Comment', $Controller->uses));
|
||||
|
||||
ClassRegistry::flush();
|
||||
unset($Controller);
|
||||
|
@ -613,6 +613,7 @@ class ControllerTest extends CakeTestCase {
|
|||
App::build(array(
|
||||
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
|
||||
), true);
|
||||
ClassRegistry::flush();
|
||||
$request = new CakeRequest('controller_posts/index');
|
||||
$request->params['action'] = 'index';
|
||||
|
||||
|
@ -636,13 +637,20 @@ class ControllerTest extends CakeTestCase {
|
|||
$Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
|
||||
$expected = $Controller->ControllerComment->validationErrors;
|
||||
|
||||
ClassRegistry::flush();
|
||||
$Controller->viewPath = 'Posts';
|
||||
$result = $Controller->render('index');
|
||||
$View = $Controller->View;
|
||||
$this->assertTrue(isset($View->validationErrors['ControllerComment']));
|
||||
$this->assertEqual($expected, $View->validationErrors['ControllerComment']);
|
||||
|
||||
$expectedModels = array(
|
||||
'ControllerAlias' => array('plugin' => null, 'className' => 'ControllerAlias'),
|
||||
'ControllerComment' => array('plugin' => null, 'className' => 'ControllerComment'),
|
||||
'ControllerPost' => array('plugin' => null, 'className' => 'ControllerPost')
|
||||
);
|
||||
$this->assertEqual($expectedModels, $Controller->request->params['models']);
|
||||
|
||||
|
||||
$Controller->ControllerComment->validationErrors = array();
|
||||
ClassRegistry::flush();
|
||||
|
||||
|
@ -998,6 +1006,7 @@ class ControllerTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testValidateErrors() {
|
||||
ClassRegistry::flush();
|
||||
$request = new CakeRequest('controller_posts/index');
|
||||
|
||||
$TestController = new TestController($request);
|
||||
|
|
|
@ -736,4 +736,14 @@ class AppTest extends CakeTestCase {
|
|||
App::uses('TestUtilityClass', 'Utility');
|
||||
$this->assertTrue(class_exists('CustomLibClass'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that App::location() returns the defined path for a class
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testClassLocation() {
|
||||
App::uses('MyCustomClass', 'MyPackage/Name');
|
||||
$this->assertEquals('MyPackage/Name', App::location('MyCustomClass'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -280,4 +280,12 @@ class ClassRegistryTest extends CakeTestCase {
|
|||
$this->assertSame($PluginUser, $PluginUserCopy);
|
||||
CakePlugin::unload();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that passing the string parameter to init() will return false if the model does not exists
|
||||
*
|
||||
*/
|
||||
public function testInitStrict() {
|
||||
$this->assertFalse(ClassRegistry::init('NonExistent', true));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -659,6 +659,20 @@ class TestMail extends CakeTestModel {
|
|||
*/
|
||||
class FormHelperTest extends CakeTestCase {
|
||||
|
||||
/**
|
||||
* Fixtures to be used
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $fixtures = array('core.post');
|
||||
|
||||
/**
|
||||
* Do not load the fixtures by default
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public $autoFixtures = false;
|
||||
|
||||
/**
|
||||
* setUp method
|
||||
*
|
||||
|
@ -1361,7 +1375,9 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testPasswordValidation() {
|
||||
$this->Form->validationErrors['Contact']['password'] = array('Please provide a password');
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['password'] = array('Please provide a password');
|
||||
|
||||
$result = $this->Form->input('Contact.password');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input password error'),
|
||||
|
@ -1414,9 +1430,6 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$expected = array('OpenidUrl' => array('openid_not_registered' => array(true)));
|
||||
$this->assertEqual($this->Form->validationErrors, $expected);
|
||||
|
||||
$result = $this->Form->error(
|
||||
'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
|
||||
);
|
||||
|
@ -1457,11 +1470,20 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$expected = array(
|
||||
'ValidateUser' => array('email' => array(true)),
|
||||
'ValidateProfile' => array('full_name' => array(true), 'city' => array(true))
|
||||
$result = $this->Form->error(
|
||||
'ValidateUser.email', 'Invalid email', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($this->Form->validationErrors, $expected);
|
||||
$this->assertEqual($result, 'Invalid email');
|
||||
|
||||
$result = $this->Form->error(
|
||||
'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($result, 'Invalid name');
|
||||
|
||||
$result = $this->Form->error(
|
||||
'ValidateProfile.city', 'Invalid city', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($result, 'Invalid city');
|
||||
|
||||
unset($this->ValidateUser->ValidateProfile);
|
||||
unset($this->ValidateUser);
|
||||
|
@ -1502,12 +1524,24 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$expected = array(
|
||||
'ValidateUser' => array('email' => array(true)),
|
||||
'ValidateProfile' => array('full_name' => array(true), 'city' => array(true)),
|
||||
'ValidateItem' => array('description' => array(true))
|
||||
$result = $this->Form->error(
|
||||
'ValidateUser.email', 'Invalid email', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($this->Form->validationErrors, $expected);
|
||||
$this->assertEqual($result, 'Invalid email');
|
||||
|
||||
$result = $this->Form->error(
|
||||
'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($result, 'Invalid name');
|
||||
|
||||
$result = $this->Form->error(
|
||||
'ValidateProfile.city', 'Invalid city', array('wrap' => false)
|
||||
);
|
||||
|
||||
$result = $this->Form->error(
|
||||
'ValidateItem.description', 'Invalid description', array('wrap' => false)
|
||||
);
|
||||
$this->assertEqual($result, 'Invalid description');
|
||||
|
||||
unset($this->ValidateUser->ValidateProfile->ValidateItem);
|
||||
unset($this->ValidateUser->ValidateProfile);
|
||||
|
@ -1523,9 +1557,10 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testFormValidationMultiRecord() {
|
||||
$this->Form->validationErrors['Contact'] = array(2 => array(
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors[2] = array(
|
||||
'name' => array('This field cannot be left blank')
|
||||
));
|
||||
);
|
||||
$result = $this->Form->input('Contact.2.name');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text error'),
|
||||
|
@ -1553,10 +1588,15 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testMultipleInputValidation() {
|
||||
$Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
|
||||
$Address->validationErrors[0] = array(
|
||||
'title' => array('This field cannot be empty'),
|
||||
'first_name' => array('This field cannot be empty')
|
||||
);
|
||||
$Address->validationErrors[1] = array(
|
||||
'last_name' => array('You must have a last name')
|
||||
);
|
||||
$this->Form->create();
|
||||
$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(
|
||||
|
@ -1763,16 +1803,17 @@ class FormHelperTest extends CakeTestCase {
|
|||
|
||||
unset($this->Form->request->data);
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array('Badness!');
|
||||
$result = $this->Form->input('Model.field');
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['field'] = array('Badness!');
|
||||
$result = $this->Form->input('Contact.field');
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text error'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Model][field]',
|
||||
'id' => 'ModelField', 'class' => 'form-error'
|
||||
'type' => 'text', 'name' => 'data[Contact][field]',
|
||||
'id' => 'ContactField', 'class' => 'form-error'
|
||||
),
|
||||
array('div' => array('class' => 'error-message')),
|
||||
'Badness!',
|
||||
|
@ -1781,16 +1822,16 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Model.field', array(
|
||||
$result = $this->Form->input('Contact.field', array(
|
||||
'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
|
||||
));
|
||||
$expected = array(
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Model][field]',
|
||||
'id' => 'ModelField', 'class' => 'form-error'
|
||||
'type' => 'text', 'name' => 'data[Contact][field]',
|
||||
'id' => 'ContactField', 'class' => 'form-error'
|
||||
),
|
||||
array('span' => array('class' => 'error-message')),
|
||||
'Badness!',
|
||||
|
@ -1798,32 +1839,32 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Model.field', array(
|
||||
$result = $this->Form->input('Contact.field', array(
|
||||
'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
|
||||
));
|
||||
$expected = array(
|
||||
'span' => array('class' => 'input text error'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Model][field]',
|
||||
'id' => 'ModelField', 'class' => 'form-error'
|
||||
'type' => 'text', 'name' => 'data[Contact][field]',
|
||||
'id' => 'ContactField', 'class' => 'form-error'
|
||||
),
|
||||
'Badness!',
|
||||
'/span'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
|
||||
$result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text error'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array(
|
||||
'type' => 'text', 'name' => 'data[Model][field]',
|
||||
'id' => 'ModelField', 'class' => 'form-error'
|
||||
'type' => 'text', 'name' => 'data[Contact][field]',
|
||||
'id' => 'ContactField', 'class' => 'form-error'
|
||||
),
|
||||
'A message to you, Rudy',
|
||||
array('div' => array('class' => 'error-message')),
|
||||
|
@ -1834,36 +1875,35 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->setEntity(null);
|
||||
$this->Form->setEntity('Model.field');
|
||||
$result = $this->Form->input('Model.field', array(
|
||||
$this->Form->setEntity('Contact.field');
|
||||
$result = $this->Form->input('Contact.field', array(
|
||||
'after' => 'A message to you, Rudy', 'error' => false
|
||||
));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
|
||||
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
|
||||
'A message to you, Rudy',
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
unset($this->Form->validationErrors['Model']['field']);
|
||||
$result = $this->Form->input('Model.field', array('after' => 'A message to you, Rudy'));
|
||||
$result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ObjectField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField'),
|
||||
'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
|
||||
'A message to you, Rudy',
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array('minLength');
|
||||
$result = $this->Form->input('Model.field', array(
|
||||
$Contact->validationErrors['field'] = array('minLength');
|
||||
$result = $this->Form->input('Contact.field', array(
|
||||
'error' => array(
|
||||
'minLength' => 'Le login doit contenir au moins 2 caractères',
|
||||
'maxLength' => 'login too large'
|
||||
|
@ -1871,10 +1911,10 @@ class FormHelperTest extends CakeTestCase {
|
|||
));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text error'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
|
||||
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
|
||||
array('div' => array('class' => 'error-message')),
|
||||
'Le login doit contenir au moins 2 caractères',
|
||||
'/div',
|
||||
|
@ -1882,8 +1922,8 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array('maxLength');
|
||||
$result = $this->Form->input('Model.field', array(
|
||||
$Contact->validationErrors['field'] = array('maxLength');
|
||||
$result = $this->Form->input('Contact.field', array(
|
||||
'error' => array(
|
||||
'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
|
||||
'minLength' => 'Le login doit contenir au moins 2 caractères',
|
||||
|
@ -1892,10 +1932,10 @@ class FormHelperTest extends CakeTestCase {
|
|||
));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input text error'),
|
||||
'label' => array('for' => 'ModelField'),
|
||||
'label' => array('for' => 'ContactField'),
|
||||
'Field',
|
||||
'/label',
|
||||
'input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField', 'class' => 'form-error'),
|
||||
'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
|
||||
array('span' => array('class' => 'error-message', 'rel' => 'fake')),
|
||||
'login too large',
|
||||
'/span',
|
||||
|
@ -2186,14 +2226,14 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
|
||||
$this->Form->request->data = array('Model' => array('user_id' => null));
|
||||
$result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
|
||||
$this->Form->request->data = array('Thing' => array('user_id' => null));
|
||||
$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input select'),
|
||||
'label' => array('for' => 'ModelUserId'),
|
||||
'label' => array('for' => 'ThingUserId'),
|
||||
'User',
|
||||
'/label',
|
||||
'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
|
||||
'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
|
||||
array('option' => array('value' => '')),
|
||||
'Some Empty',
|
||||
'/option',
|
||||
|
@ -2209,14 +2249,14 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
|
||||
$this->Form->request->data = array('Model' => array('user_id' => 'value'));
|
||||
$result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
|
||||
$this->Form->request->data = array('Thing' => array('user_id' => 'value'));
|
||||
$result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'input select'),
|
||||
'label' => array('for' => 'ModelUserId'),
|
||||
'label' => array('for' => 'ThingUserId'),
|
||||
'User',
|
||||
'/label',
|
||||
'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
|
||||
'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
|
||||
array('option' => array('value' => '')),
|
||||
'Some Empty',
|
||||
'/option',
|
||||
|
@ -2674,10 +2714,11 @@ class FormHelperTest extends CakeTestCase {
|
|||
$result = $this->Form->text('Model.text');
|
||||
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test <strong>HTML</strong> values', 'id' => 'ModelText')));
|
||||
|
||||
$this->Form->validationErrors['Model']['text'] = 1;
|
||||
$this->Form->request->data['Model']['text'] = 'test';
|
||||
$result = $this->Form->text('Model.text', array('id' => 'theID'));
|
||||
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['text'] = array(true);
|
||||
$this->Form->request->data['Contact']['text'] = 'test';
|
||||
$result = $this->Form->text('Contact.text', array('id' => 'theID'));
|
||||
$this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
|
||||
|
||||
$this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
|
||||
$result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
|
||||
|
@ -2740,35 +2781,36 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testError() {
|
||||
$this->Form->validationErrors['Model']['field'] = array(1);
|
||||
$result = $this->Form->error('Model.field');
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['field'] = array(1);
|
||||
$result = $this->Form->error('Contact.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));
|
||||
$result = $this->Form->error('Contact.field', null, array('wrap' => false));
|
||||
$this->assertEqual($result, 'Error in field Field');
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array("This field contains invalid input");
|
||||
$result = $this->Form->error('Model.field', null, array('wrap' => false));
|
||||
$Contact->validationErrors['field'] = array("This field contains invalid input");
|
||||
$result = $this->Form->error('Contact.field', null, array('wrap' => false));
|
||||
$this->assertEqual($result, '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'));
|
||||
$Contact->validationErrors['field'] = array("This field contains invalid input");
|
||||
$result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
|
||||
$this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
|
||||
|
||||
$result = $this->Form->error('Model.field', 'There is an error fool!', array('wrap' => 'span'));
|
||||
$result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
|
||||
$this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
|
||||
|
||||
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false));
|
||||
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
|
||||
$this->assertEqual($result, '<strong>Badness!</strong>');
|
||||
|
||||
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
|
||||
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
|
||||
$this->assertEqual($result, '<strong>Badness!</strong>');
|
||||
|
||||
$result = $this->Form->error('Model.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
|
||||
$result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
|
||||
$this->assertEqual($result, '<strong>Badness!</strong>');
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array("email");
|
||||
$result = $this->Form->error('Model.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
|
||||
$Contact->validationErrors['field'] = array("email");
|
||||
$result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'field-error'),
|
||||
'No good!',
|
||||
|
@ -2776,8 +2818,8 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email', 'Something else');
|
||||
$result = $this->Form->error('Model.field', array(
|
||||
$Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
|
||||
$result = $this->Form->error('Contact.field', array(
|
||||
'notEmpty' => 'Cannot be empty',
|
||||
'email' => 'No good!'
|
||||
));
|
||||
|
@ -2793,9 +2835,9 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTags($result, $expected);
|
||||
|
||||
/** Testing error messages list options **/
|
||||
$this->Form->validationErrors['Model']['field'] = array('notEmpty', 'email');
|
||||
$Contact->validationErrors['field'] = array('notEmpty', 'email');
|
||||
|
||||
$result = $this->Form->error('Model.field', null, array('listOptions' => 'ol'));
|
||||
$result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'error-message'),
|
||||
'ol' => array(),
|
||||
|
@ -2806,7 +2848,7 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->error('Model.field', null, array('listOptions' => array('tag' => 'ol')));
|
||||
$result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
|
||||
$expected = array(
|
||||
'div' => array('class' => 'error-message'),
|
||||
'ol' => array(),
|
||||
|
@ -2817,7 +2859,7 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->error('Model.field', null, array(
|
||||
$result = $this->Form->error('Contact.field', null, array(
|
||||
'listOptions' => array(
|
||||
'class' => 'ul-class',
|
||||
'itemOptions' => array(
|
||||
|
@ -2843,7 +2885,8 @@ class FormHelperTest extends CakeTestCase {
|
|||
*/
|
||||
public function testInputErrorEscape() {
|
||||
$this->Form->create('ValidateProfile');
|
||||
$this->Form->validationErrors['ValidateProfile']['city'] = array('required<br>');
|
||||
$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
|
||||
$ValidateProfile->validationErrors['city'] = array('required<br>');
|
||||
$result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
|
||||
$this->assertPattern('/required<br>/', $result);
|
||||
|
||||
|
@ -2860,13 +2903,14 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testPassword() {
|
||||
$result = $this->Form->password('Model.field');
|
||||
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$result = $this->Form->password('Contact.field');
|
||||
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
|
||||
|
||||
$this->Form->validationErrors['Model']['passwd'] = 1;
|
||||
$this->Form->request->data['Model']['passwd'] = 'test';
|
||||
$result = $this->Form->password('Model.passwd', array('id' => 'theID'));
|
||||
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
|
||||
$Contact->validationErrors['passwd'] = 1;
|
||||
$this->Form->request->data['Contact']['passwd'] = 'test';
|
||||
$result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
|
||||
$this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3783,25 +3827,26 @@ class FormHelperTest extends CakeTestCase {
|
|||
));
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['Model']['tags'] = 'Select atleast one option';
|
||||
$result = $this->Form->input('Model.tags', array(
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['tags'] = 'Select atleast one option';
|
||||
$result = $this->Form->input('Contact.tags', array(
|
||||
'options' => array('one'),
|
||||
'multiple' => 'checkbox',
|
||||
'label' => false,
|
||||
'div' => false
|
||||
));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
|
||||
array('div' => array('class' => 'checkbox form-error')),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
|
||||
array('label' => array('for' => 'ModelTags0')),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
|
||||
array('label' => array('for' => 'ContactTags0')),
|
||||
'one',
|
||||
'/label',
|
||||
'/div'
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->input('Model.tags', array(
|
||||
$result = $this->Form->input('Contact.tags', array(
|
||||
'options' => array('one'),
|
||||
'multiple' => 'checkbox',
|
||||
'class' => 'mycheckbox',
|
||||
|
@ -3809,10 +3854,10 @@ class FormHelperTest extends CakeTestCase {
|
|||
'div' => false
|
||||
));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'),
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
|
||||
array('div' => array('class' => 'mycheckbox form-error')),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][tags][]', 'value' => '0', 'id' => 'ModelTags0')),
|
||||
array('label' => array('for' => 'ModelTags0')),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
|
||||
array('label' => array('for' => 'ContactTags0')),
|
||||
'one',
|
||||
'/label',
|
||||
'/div'
|
||||
|
@ -4102,35 +4147,36 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['Model']['field'] = 1;
|
||||
$this->Form->request->data['Model']['field'] = 'myvalue';
|
||||
$result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['field'] = 1;
|
||||
$this->Form->request->data['Contact']['field'] = 'myvalue';
|
||||
$result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
|
||||
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
|
||||
$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
|
||||
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ModelField', 'checked' => 'checked', 'class' => 'form-error'))
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
|
||||
array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->request->data['Model']['field'] = '';
|
||||
$result = $this->Form->checkbox('Model.field', array('id' => 'theID'));
|
||||
$this->Form->request->data['Contact']['field'] = '';
|
||||
$result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
unset($this->Form->validationErrors['Model']['field']);
|
||||
$result = $this->Form->checkbox('Model.field', array('value' => 'myvalue'));
|
||||
$Contact->validationErrors = array();
|
||||
$result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
|
||||
$expected = array(
|
||||
'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'ModelField'))
|
||||
'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
|
||||
array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
|
@ -5506,11 +5552,12 @@ class FormHelperTest extends CakeTestCase {
|
|||
* @return void
|
||||
*/
|
||||
public function testHiddenField() {
|
||||
$this->Form->validationErrors['Model']['field'] = 1;
|
||||
$this->Form->request->data['Model']['field'] = 'test';
|
||||
$result = $this->Form->hidden('Model.field', array('id' => 'theID'));
|
||||
$Contact = ClassRegistry::getObject('Contact');
|
||||
$Contact->validationErrors['field'] = 1;
|
||||
$this->Form->request->data['Contact']['field'] = 'test';
|
||||
$result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
|
||||
$this->assertTags($result, array(
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Model][field]', 'id' => 'theID', 'value' => 'test'))
|
||||
'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -5990,7 +6037,6 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->request['controller'] = 'pages';
|
||||
$this->Form->request['models'] = array('User', 'Post');
|
||||
$result = $this->Form->create('User', array('action' => 'signup'));
|
||||
$expected = array(
|
||||
'form' => array(
|
||||
|
@ -6005,7 +6051,7 @@ class FormHelperTest extends CakeTestCase {
|
|||
|
||||
$this->Form->request->data = array();
|
||||
$this->Form->request['controller'] = 'contacts';
|
||||
$this->Form->request['models'] = array('Contact');
|
||||
$this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
|
||||
$result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
|
||||
$expected = array(
|
||||
'form' => array(
|
||||
|
@ -7105,7 +7151,8 @@ class FormHelperTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$this->Form->validationErrors['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = 'Error';
|
||||
$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
|
||||
$ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
|
||||
$this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
|
||||
$result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
|
||||
$expected = array(
|
||||
|
@ -7134,11 +7181,12 @@ class FormHelperTest extends CakeTestCase {
|
|||
*/
|
||||
public function testMultiRecordFormValidationErrors() {
|
||||
$this->Form->create('ValidateProfile');
|
||||
$this->Form->validationErrors['ValidateProfile'][2]['ValidateItem'][1]['name'] = array('Error in field name');
|
||||
$ValidateProfile = ClassRegistry::getObject('ValidateProfile');
|
||||
$ValidateProfile->validationErrors[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'] = array('Error in field city');
|
||||
$ValidateProfile->validationErrors[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'));
|
||||
|
||||
|
@ -7249,4 +7297,36 @@ class FormHelperTest extends CakeTestCase {
|
|||
$this->Form->email();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a model can be loaded from the model names passed in the request object
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testIntrospectModelFromRequest() {
|
||||
$this->loadFixtures('Post');
|
||||
App::build(array(
|
||||
'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
|
||||
));
|
||||
CakePlugin::load('TestPlugin');
|
||||
$this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
|
||||
|
||||
$this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
|
||||
$this->Form->create('TestPluginPost');
|
||||
$this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
|
||||
$this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
|
||||
|
||||
CakePlugin::unload();
|
||||
App::build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that it is possible to set the validation errors directly in the helper for a field
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testCustomValidationErrors() {
|
||||
$this->Form->validationErrors['Thing']['field'] = 'Badness!';
|
||||
$result = $this->Form->error('Thing.field', null, array('wrap' => false));
|
||||
$this->assertEquals('Badness!', $result);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ class ClassRegistry {
|
|||
* Examples
|
||||
* Simple Use: Get a Post model instance ```ClassRegistry::init('Post');```
|
||||
*
|
||||
* Exapanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
|
||||
* Expanded: ```array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model');```
|
||||
*
|
||||
* Model Classes can accept optional ```array('id' => $id, 'table' => $table, 'ds' => $ds, 'alias' => $alias);```
|
||||
*
|
||||
|
@ -82,25 +82,22 @@ class ClassRegistry {
|
|||
* no instance of the object will be returned
|
||||
* {{{
|
||||
* array(
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model'),
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry', 'type' => 'Model')
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry'),
|
||||
* array('class' => 'ClassName', 'alias' => 'AliasNameStoredInTheRegistry')
|
||||
* );
|
||||
* }}}
|
||||
* @param mixed $class as a string or a single key => value array instance will be created,
|
||||
* stored in the registry and returned.
|
||||
* @param string $type Only model is accepted as a valid value for $type.
|
||||
* @param boolean $strict if set to true it will return false if the class was not found instead
|
||||
* of trying to create an AppModel
|
||||
* @return object instance of ClassName
|
||||
*/
|
||||
public static function &init($class, $type = null) {
|
||||
public static function init($class, $strict = false) {
|
||||
$_this = ClassRegistry::getInstance();
|
||||
$false = false;
|
||||
$true = true;
|
||||
|
||||
if (!$type) {
|
||||
$type = 'Model';
|
||||
}
|
||||
|
||||
if (is_array($class)) {
|
||||
$objects = $class;
|
||||
if (!isset($class[0])) {
|
||||
|
@ -109,7 +106,7 @@ class ClassRegistry {
|
|||
} else {
|
||||
$objects = array(array('class' => $class));
|
||||
}
|
||||
$defaults = isset($_this->__config[$type]) ? $_this->__config[$type] : array();
|
||||
$defaults = isset($_this->__config['Model']) ? $_this->__config['Model'] : array();
|
||||
$count = count($objects);
|
||||
|
||||
foreach ($objects as $key => $settings) {
|
||||
|
@ -135,16 +132,22 @@ class ClassRegistry {
|
|||
|
||||
App::uses('Model', 'Model');
|
||||
App::uses('AppModel', 'Model');
|
||||
App::uses($plugin . 'AppModel', $pluginPath . $type);
|
||||
App::uses($class, $pluginPath . $type);
|
||||
App::uses($plugin . 'AppModel', $pluginPath . 'Model');
|
||||
App::uses($class, $pluginPath . 'Model');
|
||||
|
||||
if (class_exists($class)) {
|
||||
${$class} = new $class($settings);
|
||||
} elseif ($type === 'Model') {
|
||||
if ($plugin && class_exists($plugin . 'AppModel')) {
|
||||
${$class} = (${$class} instanceof Model) ? ${$class} : null;
|
||||
}
|
||||
if (!isset(${$class})) {
|
||||
if ($strict) {
|
||||
return false;
|
||||
} elseif ($plugin && class_exists($plugin . 'AppModel')) {
|
||||
$appModel = $plugin . 'AppModel';
|
||||
} else {
|
||||
$appModel = 'AppModel';
|
||||
}
|
||||
if (!empty($appModel)) {
|
||||
$settings['name'] = $class;
|
||||
${$class} = new $appModel($settings);
|
||||
}
|
||||
|
@ -153,12 +156,8 @@ class ClassRegistry {
|
|||
trigger_error(__d('cake_dev', '(ClassRegistry::init() could not create instance of %1$s class %2$s ', $class, $type), E_USER_WARNING);
|
||||
return $false;
|
||||
}
|
||||
|
||||
if ($type !== 'Model') {
|
||||
$_this->addObject($alias, ${$class});
|
||||
} else {
|
||||
$_this->map($alias, $class);
|
||||
}
|
||||
$_this->map($alias, $class);
|
||||
} elseif (is_numeric($settings)) {
|
||||
trigger_error(__d('cake_dev', '(ClassRegistry::init() Attempted to create instance of a class with a numeric name'), E_USER_WARNING);
|
||||
return $false;
|
||||
|
|
|
@ -64,14 +64,6 @@ class Helper extends Object {
|
|||
*/
|
||||
public $plugin = null;
|
||||
|
||||
/**
|
||||
* Contains model validation errors of form post-backs
|
||||
*
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $validationErrors = null;
|
||||
|
||||
/**
|
||||
* Holds tag templates.
|
||||
*
|
||||
|
@ -508,23 +500,6 @@ class Helper extends Object {
|
|||
return $last;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false if given FORM field has no errors. Otherwise it returns the constant set in
|
||||
* the array Model->validationErrors.
|
||||
*
|
||||
* @param string $model Model name as a string
|
||||
* @param string $field Fieldname as a string
|
||||
* @param integer $modelID Unique index identifying this record within the form
|
||||
* @return boolean True on errors.
|
||||
*/
|
||||
public function tagIsInvalid($model = null, $field = null, $modelID = null) {
|
||||
$errors = $this->validationErrors;
|
||||
$entity = $this->entity();
|
||||
if (!empty($entity)) {
|
||||
return Set::extract($errors, join('.', $entity));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a DOM ID for the selected element, if one is not set.
|
||||
* Uses the current View::entity() settings to generate a CamelCased id attribute.
|
||||
|
|
|
@ -96,6 +96,7 @@ class FormHelper extends AppHelper {
|
|||
* @access protected
|
||||
*/
|
||||
protected $_inputDefaults = array();
|
||||
|
||||
/**
|
||||
* An array of fieldnames that have been excluded from
|
||||
* the Token hash used by SecurityComponent's validatePost method
|
||||
|
@ -107,49 +108,144 @@ class FormHelper extends AppHelper {
|
|||
protected $_unlockedFields = array();
|
||||
|
||||
/**
|
||||
* Introspects model information and extracts information related
|
||||
* to validation, field length and field type. Appends information into
|
||||
* $this->fieldset.
|
||||
* Holds the model references already loaded by this helper
|
||||
* product of trying to inspect them out of field names
|
||||
*
|
||||
* @return Model Returns a model instance
|
||||
* @var array
|
||||
*/
|
||||
protected function &_introspectModel($model) {
|
||||
protected $_models = array();
|
||||
|
||||
/**
|
||||
* Holds all the validation errors for models loaded and inspected
|
||||
* it can also be set manually to be able to display custom error messages
|
||||
* in the any of the input fields generated by this helper
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $validationErrors = array();
|
||||
|
||||
/**
|
||||
* Copies the validationErrors variable from the View object into this instance
|
||||
*
|
||||
* @param View $View The View this helper is being attached to.
|
||||
* @param array $settings Configuration settings for the helper.
|
||||
*/
|
||||
public function __construct(View $View, $settings = array()) {
|
||||
parent::__construct($View, $settings);
|
||||
$this->validationErrors =& $View->validationErrors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Guess the location for a model based on its name and tries to create a new instance
|
||||
* or get an already created instance of the model
|
||||
*
|
||||
* @return Model model instance
|
||||
*/
|
||||
protected function _getModel($model) {
|
||||
$object = null;
|
||||
if (is_string($model) && strpos($model, '.') !== false) {
|
||||
$path = explode('.', $model);
|
||||
$model = end($path);
|
||||
if (!$model || $model === 'Model') {
|
||||
return $object;
|
||||
}
|
||||
|
||||
if (array_key_exists($model, $this->_models)) {
|
||||
return $this->_models[$model];
|
||||
}
|
||||
|
||||
if (ClassRegistry::isKeySet($model)) {
|
||||
$object = ClassRegistry::getObject($model);
|
||||
} elseif (isset($this->request->params['models'][$model])) {
|
||||
$plugin = $this->request->params['models'][$model]['plugin'];
|
||||
$plugin .= ($plugin) ? '.' : null;
|
||||
$object = ClassRegistry::init(array(
|
||||
'class' => $plugin . $this->request->params['models'][$model]['className'],
|
||||
'alias' => $model
|
||||
));
|
||||
} else {
|
||||
$object = ClassRegistry::init($model, true);
|
||||
}
|
||||
|
||||
if (!empty($object)) {
|
||||
$fields = $object->schema();
|
||||
foreach ($fields as $key => $value) {
|
||||
unset($fields[$key]);
|
||||
$fields[$key] = $value;
|
||||
$this->_models[$model] = $object;
|
||||
if (!$object) {;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!empty($object->hasAndBelongsToMany)) {
|
||||
$this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null);
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inspects the model properties to extract information from them.
|
||||
* Currently it can extract information from the the fields, the primary key and required fields
|
||||
*
|
||||
* The $key parameter accepts the following list of values:
|
||||
*
|
||||
* - key: Returns the name of the primary key for the model
|
||||
* - fields: Returns the model schema
|
||||
* - validates: returns the list of fields that are required
|
||||
* - errors: returns the list of validation errors
|
||||
*
|
||||
* If the $field parameter is passed if will return the information for that sole field.
|
||||
*
|
||||
* `$this->_introspectModel('Post', 'fields', 'title');` will return the schema information for title column
|
||||
*
|
||||
* @param string $model name of the model to extract information from
|
||||
* @param string $key name of the special information key to obtain (key, fields, validates, errors)
|
||||
* @param string $field name of the model field to get information from
|
||||
* @return mixed information extracted for the special key and field in a model
|
||||
*/
|
||||
protected function _introspectModel($model, $key, $field = null) {
|
||||
$object = $this->_getModel($model);
|
||||
if (!$object) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($key === 'key') {
|
||||
return $this->fieldset[$model]['key'];
|
||||
}
|
||||
|
||||
if ($key === 'fields') {
|
||||
if (!isset($this->fieldset[$model]['fields'])) {
|
||||
$fields = $this->fieldset[$model]['fields'] = $object->schema();
|
||||
}
|
||||
if (empty($field)) {
|
||||
foreach ($object->hasAndBelongsToMany as $alias => $assocData) {
|
||||
$fields[$alias] = array('type' => 'multiple');
|
||||
$this->fieldset[$object->alias]['fields'][$alias] = array('type' => 'multiple');
|
||||
}
|
||||
return $this->fieldset[$model]['fields'];
|
||||
} elseif (isset($this->fieldset[$model]['fields'][$field])) {
|
||||
return $this->fieldset[$model]['fields'][$field];
|
||||
} else {
|
||||
return isset($object->hasAndBelongsToMany[$field]) ? array('type' => 'multiple') : null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($key === 'errors' && !isset($this->validationErrors[$model])) {
|
||||
$this->validationErrors[$model] =& $object->validationErrors;
|
||||
return $this->validationErrors[$model];
|
||||
} elseif ($key === 'errors' && isset($this->validationErrors[$model])) {
|
||||
return $this->validationErrors[$model];
|
||||
}
|
||||
|
||||
if ($key === 'validates' && !isset($this->fieldset[$model]['validates'])) {
|
||||
$validates = array();
|
||||
if (!empty($object->validate)) {
|
||||
foreach ($object->validate as $validateField => $validateProperties) {
|
||||
if ($this->_isRequiredField($validateProperties)) {
|
||||
$validates[] = $validateField;
|
||||
$validates[$validateField] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
$defaults = array('fields' => array(), 'key' => 'id', 'validates' => array());
|
||||
$key = $object->primaryKey;
|
||||
$this->fieldset[$model] = array_merge($defaults, compact('fields', 'key', 'validates'));
|
||||
$this->fieldset[$model]['validates'] = $validates;
|
||||
}
|
||||
|
||||
return $object;
|
||||
if ($key === 'validates') {
|
||||
if (empty($field)) {
|
||||
return $this->fieldset[$model]['validates'];
|
||||
} else {
|
||||
return isset($this->fieldset[$model]['validates'][$field]) ?
|
||||
$this->fieldset[$model]['validates'] : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,6 +276,26 @@ class FormHelper extends AppHelper {
|
|||
return $required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false if given FORM field has no errors. Otherwise it returns the validation message
|
||||
*
|
||||
* @param string $model Model name as a string
|
||||
* @param string $field Fieldname as a string
|
||||
* @param integer $modelID Unique index identifying this record within the form
|
||||
* @return boolean True on errors.
|
||||
*/
|
||||
public function tagIsInvalid($model = null, $field = null, $modelID = null) {
|
||||
$entity = $this->entity();
|
||||
$model = array_shift($entity);
|
||||
if (!empty($entity) && isset($this->validationErrors[$model])) {
|
||||
return Set::classicExtract($this->validationErrors[$model], join('.', $entity));
|
||||
}
|
||||
if (!empty($entity)) {
|
||||
$errors = $this->_introspectModel($model, 'errors');
|
||||
return ($errors) ? Set::classicExtract($errors, join('.', $entity)) : false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an HTML FORM element.
|
||||
*
|
||||
|
@ -210,39 +326,30 @@ class FormHelper extends AppHelper {
|
|||
$options = $model;
|
||||
$model = null;
|
||||
}
|
||||
if (empty($model) && $model !== false && !empty($this->request['models'])) {
|
||||
$model = $this->request['models'][0];
|
||||
$this->defaultModel = $this->request['models'][0];
|
||||
} elseif (empty($model) && empty($this->request['models'])) {
|
||||
if (empty($model) && $model !== false && !empty($this->request->params['models'])) {
|
||||
$model = key($this->request->params['models']);
|
||||
$this->defaultModel = $model;
|
||||
} elseif (empty($model) && empty($this->request->params['models'])) {
|
||||
$model = false;
|
||||
}
|
||||
|
||||
$models = ClassRegistry::keys();
|
||||
foreach ($models as $currentModel) {
|
||||
if (ClassRegistry::isKeySet($currentModel)) {
|
||||
$currentObject = ClassRegistry::getObject($currentModel);
|
||||
if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
|
||||
$this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$key = null;
|
||||
if ($model !== false) {
|
||||
$object = $this->_introspectModel($model);
|
||||
$object = $this->_getModel($model);
|
||||
$key = $this->_introspectModel($model, 'key');
|
||||
}
|
||||
$this->setEntity($model, true);
|
||||
|
||||
if ($model !== false && isset($this->fieldset[$model]['key'])) {
|
||||
$data = $this->fieldset[$model];
|
||||
if ($model !== false && $key) {
|
||||
$recordExists = (
|
||||
isset($this->request->data[$model]) &&
|
||||
!empty($this->request->data[$model][$data['key']]) &&
|
||||
!is_array($this->request->data[$model][$data['key']])
|
||||
!empty($this->request->data[$model][$key]) &&
|
||||
!is_array($this->request->data[$model][$key])
|
||||
);
|
||||
|
||||
if ($recordExists) {
|
||||
$created = true;
|
||||
$id = $this->request->data[$model][$data['key']];
|
||||
$id = $this->request->data[$model][$key];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -378,9 +485,6 @@ class FormHelper extends AppHelper {
|
|||
* @link http://book.cakephp.org/view/1389/Closing-the-Form
|
||||
*/
|
||||
public function end($options = null) {
|
||||
if (!empty($this->request['models'])) {
|
||||
$models = $this->request['models'][0];
|
||||
}
|
||||
$out = null;
|
||||
$submit = null;
|
||||
|
||||
|
@ -544,8 +648,9 @@ class FormHelper extends AppHelper {
|
|||
$defaults = array('wrap' => true, 'class' => 'error-message', 'escape' => true);
|
||||
$options = array_merge($defaults, $options);
|
||||
$this->setEntity($field);
|
||||
|
||||
if ($error = $this->tagIsInvalid()) {
|
||||
if (!$error = $this->tagIsInvalid()) {
|
||||
return null;
|
||||
}
|
||||
if (is_array($text)) {
|
||||
if (isset($text['attributes']) && is_array($text['attributes'])) {
|
||||
$options = array_merge($options, $text['attributes']);
|
||||
|
@ -610,9 +715,6 @@ class FormHelper extends AppHelper {
|
|||
} else {
|
||||
return $error;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -705,7 +807,7 @@ class FormHelper extends AppHelper {
|
|||
}
|
||||
|
||||
if (empty($fields)) {
|
||||
$fields = array_keys($this->fieldset[$model]['fields']);
|
||||
$fields = array_keys($this->_introspectModel($model, 'fields'));
|
||||
}
|
||||
|
||||
if ($legend === true) {
|
||||
|
@ -796,9 +898,6 @@ class FormHelper extends AppHelper {
|
|||
|
||||
$modelKey = $this->model();
|
||||
$fieldKey = $this->field();
|
||||
if (!isset($this->fieldset[$modelKey])) {
|
||||
$this->_introspectModel($modelKey);
|
||||
}
|
||||
|
||||
if (!isset($options['type'])) {
|
||||
$magicType = true;
|
||||
|
@ -809,8 +908,7 @@ class FormHelper extends AppHelper {
|
|||
$options['type'] = 'password';
|
||||
} elseif (isset($options['checked'])) {
|
||||
$options['type'] = 'checkbox';
|
||||
} elseif (isset($this->fieldset[$modelKey]['fields'][$fieldKey])) {
|
||||
$fieldDef = $this->fieldset[$modelKey]['fields'][$fieldKey];
|
||||
} elseif ($fieldDef = $this->_introspectModel($modelKey, 'fields', $fieldKey)) {
|
||||
$type = $fieldDef['type'];
|
||||
$primaryKey = $this->fieldset[$modelKey]['key'];
|
||||
}
|
||||
|
@ -881,10 +979,7 @@ class FormHelper extends AppHelper {
|
|||
} elseif (is_array($div)) {
|
||||
$divOptions = array_merge($divOptions, $div);
|
||||
}
|
||||
if (
|
||||
isset($this->fieldset[$modelKey]) &&
|
||||
in_array($fieldKey, $this->fieldset[$modelKey]['validates'])
|
||||
) {
|
||||
if ($this->_introspectModel($modelKey, 'validates', $fieldKey)) {
|
||||
$divOptions = $this->addClass($divOptions, 'required');
|
||||
}
|
||||
if (!isset($divOptions['tag'])) {
|
||||
|
@ -2110,10 +2205,8 @@ class FormHelper extends AppHelper {
|
|||
function setEntity($entity, $setScope = false) {
|
||||
parent::setEntity($entity, $setScope);
|
||||
$parts = explode('.', $entity);
|
||||
if (
|
||||
isset($this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type']) &&
|
||||
$this->fieldset[$this->_modelScope]['fields'][$parts[0]]['type'] === 'multiple'
|
||||
) {
|
||||
$field = $this->_introspectModel($this->_modelScope, 'fields', $parts[0]);
|
||||
if (!empty($field) && $field['type'] === 'multiple') {
|
||||
$this->_entityPath = $parts[0] . '.' . $parts[0];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue