Skip to content

Commit

Permalink
Refactor how FormHelper manages its context.
Browse files Browse the repository at this point in the history
Having to call create() in every test is going to get lame. Instead,
the context can be lazily built making testing easier.
  • Loading branch information
markstory committed Feb 14, 2014
1 parent 6ba26b0 commit 095be56
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/View/Helper/FormHelper.php
Expand Up @@ -306,10 +306,10 @@ public function create($model = null, $options = []) {
$options['context'] = [];
}
$options['context']['entity'] = $model;
$this->_context = $this->_buildContext($options['context']);
$context = $this->_getContext($options['context']);
unset($options['context']);

$isCreate = $this->_context->isCreate();
$isCreate = $context->isCreate();

$options = $options + [
'type' => $isCreate ? 'post' : 'put',
Expand All @@ -319,7 +319,7 @@ public function create($model = null, $options = []) {
'encoding' => strtolower(Configure::read('App.encoding')),
];

$options['action'] = $this->_formUrl($options);
$options['action'] = $this->_formUrl($context, $options);
unset($options['url']);

switch (strtolower($options['type'])) {
Expand Down Expand Up @@ -376,10 +376,11 @@ public function create($model = null, $options = []) {
/**
* Create the URL for a form based on the options.
*
* @param Cake\View\Form\ContextInterface $context The context object to use.
* @param array $options An array of options from create()
* @return string The action attribute for the form.
*/
protected function _formUrl($options) {
protected function _formUrl($context, $options) {
if ($options['action'] === null && $options['url'] === null) {
return $this->request->here(false);
}
Expand All @@ -397,9 +398,9 @@ protected function _formUrl($options) {

$action = (array)$options['url'] + $actionDefaults;

$pk = $this->_context->primaryKey();
$pk = $context->primaryKey();
if (count($pk)) {
$id = $this->_context->val($pk[0]);
$id = $context->val($pk[0]);
}
if (empty($action[0]) && isset($id)) {
$action[0] = $id;
Expand Down Expand Up @@ -2842,6 +2843,7 @@ protected function _initInputField($field, $options = []) {
} else {
$secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
}
$context = $this->_getContext();

$disabledIndex = array_search('disabled', $options, true);
if (is_int($disabledIndex)) {
Expand All @@ -2857,18 +2859,18 @@ protected function _initInputField($field, $options = []) {
unset($options['value']);
}
if (!isset($options['val'])) {
$options['val'] = $this->_context->val($field);
$options['val'] = $context->val($field);
}
$options += (array)$this->_context->attributes($field);
$options += (array)$context->attributes($field);

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

if (!isset($options['required']) && $this->_context->isRequired($field)) {
if (!isset($options['required']) && $context->isRequired($field)) {
$options['required'] = true;
}

Expand Down Expand Up @@ -2944,7 +2946,7 @@ public function addContextProvider($name, callable $check) {
* @return null|Cake\View\Form\ContextInterface The context for the form.
*/
public function context() {
return $this->_context;
return $this->_getContext();
}

/**
Expand All @@ -2957,7 +2959,12 @@ public function context() {
* @throws RuntimeException when the context class does not implement the
* ContextInterface.
*/
protected function _buildContext($data) {
protected function _getContext($data = []) {
if (isset($this->_context) && empty($data)) {
return $this->_context;
}
$data += ['entity' => null];

foreach ($this->_contextProviders as $key => $check) {
$context = $check($this->request, $data);
if ($context) {
Expand All @@ -2972,7 +2979,7 @@ protected function _buildContext($data) {
'Context objects must implement Cake\View\Form\ContextInterface'
);
}
return $context;
return $this->_context = $context;
}

/**
Expand Down

0 comments on commit 095be56

Please sign in to comment.