Skip to content

Commit

Permalink
Start hidden and add array casts to fix issues with mocks.
Browse files Browse the repository at this point in the history
Mocks return null. Having an array cast smoothes over PHP warnings.
  • Loading branch information
markstory committed Feb 13, 2014
1 parent 5e234d4 commit e45cf77
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/View/Form/ArrayContext.php
Expand Up @@ -190,7 +190,7 @@ public function attributes($field) {
if (!is_array($this->_context['schema'])) {
return [];
}
$schema = Hash::get($this->_context['schema'], $field);
$schema = (array)Hash::get($this->_context['schema'], $field);
$whitelist = ['length' => null, 'precision' => null];
return array_intersect_key($schema, $whitelist);
}
Expand Down
2 changes: 1 addition & 1 deletion src/View/Form/EntityContext.php
Expand Up @@ -359,7 +359,7 @@ public function attributes($field) {
$parts = explode('.', $field);
list($entity, $prop) = $this->_getEntity($parts);
$table = $this->_getTable($prop);
$column = $table->schema()->column(array_pop($parts));
$column = (array)$table->schema()->column(array_pop($parts));
$whitelist = ['length' => null, 'precision' => null];
return array_intersect_key($column, $whitelist);
}
Expand Down
42 changes: 26 additions & 16 deletions src/View/Helper/FormHelper.php
Expand Up @@ -62,6 +62,15 @@ class FormHelper extends Helper {
'month' => array(), 'year' => array(), 'meridian' => array()
);

/**
* Settings for the helper.
*
* @var array
*/
public $settings = [
'errorClass' => 'form-error'
];

/**
* List of fields created, used with secure forms.
*
Expand Down Expand Up @@ -163,6 +172,7 @@ class FormHelper extends Helper {
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
];

/**
Expand Down Expand Up @@ -1588,7 +1598,8 @@ public function hidden($fieldName, $options = array()) {
$this->_secure(true, null, '' . $options['value']);
}

return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => null)));
$options['type'] = 'hidden';
return $this->widget('hidden', $options);
}

/**
Expand Down Expand Up @@ -2808,7 +2819,7 @@ protected function _generateOptions($name, $options = array()) {

/**
* Sets field defaults and adds field to form security input hash.
* Will also add a 'form-error' class if the field contains validation errors.
* Will also add the error class if the field contains validation errors.
*
* ### Options
*
Expand All @@ -2823,7 +2834,7 @@ protected function _generateOptions($name, $options = array()) {
* @param array $options Array of options to append options into.
* @return array Array of options for the input.
*/
protected function _initInputField($field, $options = array()) {
protected function _initInputField($field, $options = []) {
if (isset($options['secure'])) {
$secure = $options['secure'];
unset($options['secure']);
Expand All @@ -2837,26 +2848,26 @@ protected function _initInputField($field, $options = array()) {
$options['disabled'] = true;
}

$result = parent::_initInputField($field, $options);
if ($this->tagIsInvalid() !== false) {
$result = $this->addClass($result, 'form-error');
$options['val'] = $this->_context->val($field);
$options += $this->_context->attributes($field);

if ($this->_context->hasError($field)) {
$options = $this->addClass($options, $this->settings['errorClass']);
}
if (!empty($result['disabled']) || $secure === static::SECURE_SKIP) {
return $result;
if (!empty($options['disabled']) || $secure === static::SECURE_SKIP) {
return $options;
}

if (!isset($result['required']) &&
$this->_introspectModel($this->model(), 'validates', $this->field())
) {
$result['required'] = true;
if (!isset($options['required']) && $this->_context->isRequired($field)) {
$options['required'] = true;
}

if ($secure === self::SECURE_SKIP) {
return $result;
return $options;
}

$this->_secure($secure, $this->_secureFieldName($options));
return $result;
return $options;
}

/**
Expand Down Expand Up @@ -2981,8 +2992,7 @@ public function addWidget($name, $spec) {
* @return void
*/
public function widget($name, array $data = []) {
$widget = $this->_registry->get($name);
return $widget->render($data);
return $this->_registry->get($name)->render($data);
}

}

0 comments on commit e45cf77

Please sign in to comment.