Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2455 from njam/form-required-fields
Browse files Browse the repository at this point in the history
Move requiredFields from form-action to form
  • Loading branch information
njam authored Jan 3, 2017
2 parents 07bd49c + 3b61853 commit f77aab7
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 100 deletions.
80 changes: 28 additions & 52 deletions library/CM/Form/Abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
/** @type String **/
autosave: null,

/** @type Object **/
_fields: {},
/** @ype String[] */
requiredFieldNames: null,

/** @type String[] */
actionNames: null,

/** @type Object **/
_actions: {},
_fields: {},

/** @type Array **/
_autosaveFields: [],
Expand All @@ -39,7 +42,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
CM_View_Abstract.prototype.initialize.call(this);

this._fields = {};
this._actions = {};
},

events: {
Expand All @@ -57,7 +59,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
_ready: function() {
var handler = this;

_.each(this._actions, function(action, name) {
_.each(this.getActionNames(), function(name) {
var $btn = $('#' + this.getAutoId() + '-' + name + '-button');
var event = $btn.data('event');
if (!event) {
Expand Down Expand Up @@ -104,14 +106,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
}, this);
},

/**
* @param {String} name
* @param {Object} presentation
*/
registerAction: function(name, presentation) {
this._actions[name] = presentation;
},

/**
* @return CM_FormField_Abstract
*/
Expand Down Expand Up @@ -154,6 +148,16 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
return _.values(this._fields);
},

/**
* @returns {String[]}
*/
getActionNames: function() {
if (null === this.actionNames) {
throw new CM_Exception('Missing `actionNames` on form', false, {'form': this.getClass()});
}
return this.actionNames;
},

/**
* @returns {{}}
*/
Expand All @@ -167,26 +171,6 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
return data;
},

/**
* @param {String} actionName
* @returns {{}}
*/
getActionData: function(actionName) {
var action = this._getAction(actionName);
var data = {};

_.each(action.fields, function(isRequired, fieldName) {
if (this.hasField(fieldName)) {
var field = this.getField(fieldName);
if (field.getEnabled()) {
data[field.getName()] = field.getValue();
}
}
}, this);

return data;
},

/**
* @param {String} actionName
* @return Promise
Expand All @@ -207,9 +191,8 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
* @return Promise
*/
_submitOnly: function(actionName, disableUI) {
var action = this._getAction(actionName);
var data = this.getActionData(action.name);
var errorListRequired = this._getErrorListRequired(action.name, data);
var data = this.getData();
var errorListRequired = this._getErrorListRequired(data);

this.resetErrors();
if (_.size(errorListRequired)) {
Expand All @@ -224,7 +207,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
this.disable();
}
this.trigger('submit', [data]);
return cm.ajax('form', {viewInfoList: this.getViewInfoList(), actionName: action.name, data: data})
return cm.ajax('form', {viewInfoList: this.getViewInfoList(), actionName: actionName, data: data})
})
.then(function(response) {
if (response.errors) {
Expand All @@ -245,7 +228,7 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
}
}

this.trigger('success success.' + action.name, response.data);
this.trigger('success success.' + actionName, response.data);
return response.data;
})
.finally(function() {
Expand Down Expand Up @@ -302,30 +285,23 @@ var CM_Form_Abstract = CM_View_Abstract.extend({
},

/**
* @param {String} actionName
* @returns {Object}
* @param {String} name
* @returns {Boolean}
*/
_getAction: function(actionName) {
var action = this._actions[actionName];
if (!action) {
throw new CM_Exception('Form `' + this.getClass() + '` has no action `' + actionName + '`.');
}
action.name = actionName;
return action;
_isRequiredField: function(name) {
return this.requiredFieldNames.includes(name);
},

/**
* @param {String} actionName
* @param {Object} data
* @returns {Array[]}
*/
_getErrorListRequired: function(actionName, data) {
var action = this._getAction(actionName);
_getErrorListRequired: function(data) {
var errorList = [];

_.each(action.fields, function(isRequired, fieldName) {
_.each(this._fields, function(field, fieldName) {
var isRequired = this._isRequiredField(fieldName);
if (isRequired) {
var field = this.getField(fieldName);
if (field.isEmpty(data[fieldName])) {
var label = this._getFieldLabel(field);
if (label) {
Expand Down
23 changes: 19 additions & 4 deletions library/CM/Form/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ abstract class CM_Form_Abstract extends CM_View_Abstract {

abstract protected function _initialize();

/**
* @return string[]
*/
abstract protected function _getRequiredFields();

public function __construct($params = null) {
parent::__construct($params);

Expand All @@ -31,6 +36,9 @@ public function __construct($params = null) {
}

public function prepare(CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) {
$viewResponse->getJs()->setProperty('requiredFieldNames', $this->_getRequiredFields());
$viewResponse->getJs()->setProperty('actionNames', array_keys($this->getActions()));

$autosave = $this->_params->has('autosave') ? $this->_params->getString('autosave') : null;
if (null !== $autosave) {
$action = $this->getAction($autosave);
Expand Down Expand Up @@ -116,6 +124,15 @@ public function hasField($name) {
return array_key_exists($name, $this->_fields);
}

/**
* @param string $name
* @return bool
*/
public function isRequiredField($name) {
$name = (string) $name;
return in_array($name, $this->_getRequiredFields(), true);
}

/**
* @return bool
*/
Expand All @@ -140,9 +157,7 @@ public function process(array $data, $actionName, CM_Http_Response_View_Form $re
$action = $this->getAction($actionName);

$formData = array();
foreach ($action->getFieldList() as $fieldName => $required) {
$field = $this->getField($fieldName);

foreach ($this->getFields() as $fieldName => $field) {
$isEmpty = true;
if (array_key_exists($fieldName, $data)) {
$fieldValue = $field->filterInput($data[$fieldName]);
Expand All @@ -159,7 +174,7 @@ public function process(array $data, $actionName, CM_Http_Response_View_Form $re
}

if ($isEmpty) {
if ($required) {
if ($this->isRequiredField($fieldName)) {
$response->addError($response->getRender()->getTranslation('Required'), $fieldName);
} else {
$formData[$fieldName] = null;
Expand Down
4 changes: 4 additions & 0 deletions library/CM/Form/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ protected function _initialize() {
$this->registerAction(new CM_FormAction_Example_Submit($this));
}

protected function _getRequiredFields() {
return ['text', 'money'];
}

public function ajax_validate(CM_Params $params, CM_Frontend_JavascriptContainer_View $handler, CM_Http_Response_View_Ajax $response) {
$data = $params->getArray('data');
$result = [];
Expand Down
4 changes: 4 additions & 0 deletions library/CM/Form/ExampleAutosave.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ protected function _initialize() {
$this->registerAction(new CM_FormAction_Example_Submit($this));
}

protected function _getRequiredFields() {
return [];
}

}
6 changes: 6 additions & 0 deletions library/CM/Form/ExampleIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ protected function _initialize() {
$this->registerField(new CM_FormField_Integer(['name' => 'shadowBlur', 'min' => 0, 'max' => 20]));
}

protected function _getRequiredFields() {
return [];
}

public function prepare(CM_Frontend_Environment $environment, CM_Frontend_ViewResponse $viewResponse) {
parent::prepare($environment, $viewResponse);

$this->getField('sizeSlider')->setValue(18);
$this->getField('shadowColor')->setValue(CM_Color_RGB::fromHexString('333333'));
$this->getField('colorBackground')->setValue(CM_Color_RGB::fromHexString('fafafa'));
Expand Down
33 changes: 0 additions & 33 deletions library/CM/FormAction/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ abstract class CM_FormAction_Abstract {
/** @var CM_Form_Abstract */
private $_form;

/** @var CM_FormField_Abstract[]|null */
private $_fieldList = null;

/**
* @param CM_Form_Abstract $form
* @throws CM_Exception
Expand All @@ -30,36 +27,13 @@ public function getName() {
return $this->_name;
}

/**
* @return array [string => bool]
*/
public function getFieldList() {
if (null === $this->_fieldList) {
$this->_fieldList = array();
foreach ($this->_form->getFields() as $fieldName => $field) {
$this->_fieldList[$fieldName] = in_array($fieldName, $this->_getRequiredFields());
}
}
return $this->_fieldList;
}

/**
* @return CM_Form_Abstract
*/
public function getForm() {
return $this->_form;
}

/**
* @return string
*/
final public function js_presentation() {
$data = array();
$data['fields'] = (object) $this->getFieldList();

return json_encode($data);
}

/**
* @param array $data
* @param CM_Http_Response_View_Form $response
Expand All @@ -79,13 +53,6 @@ final public function process(array $data, CM_Http_Response_View_Form $response,
return $this->_process(CM_Params::factory($data, false), $response, $form);
}

/**
* @return string[]
*/
protected function _getRequiredFields() {
return array();
}

/**
* @param CM_Params $params
* @param CM_Http_Response_View_Form $response
Expand Down
6 changes: 1 addition & 5 deletions library/CM/FormAction/Example/Submit.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<?php

class CM_FormAction_Example_Submit extends CM_FormAction_Abstract{

protected function _getRequiredFields() {
return [];
}
class CM_FormAction_Example_Submit extends CM_FormAction_Abstract {

protected function _checkData(CM_Params $params, CM_Http_Response_View_Form $response, CM_Form_Abstract $form) {
}
Expand Down
3 changes: 0 additions & 3 deletions library/CM/SmartyPlugins/block.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ function smarty_block_form($params, $content, Smarty_Internal_Template $template
$html .= $renderAdapter->fetch(CM_Params::factory());
}
}
foreach ($form->getActions() as $actionName => $action) {
$viewResponse->getJs()->append("this.registerAction('{$actionName}', {$action->js_presentation()});");
}
$html .= '</form>';

$frontend->treeCollapse();
Expand Down
6 changes: 3 additions & 3 deletions tests/library/CM/Form/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ protected function _initialize() {
$this->registerField(new CM_FormField_Date(['name' => 'date']));
$this->registerAction(new CM_FormAction_MockForm_TestExampleAction($this));
}
}

class CM_FormAction_MockForm_TestExampleAction extends CM_FormAction_Abstract {

protected function _getRequiredFields() {
return array('must_check', 'text');
}
}

class CM_FormAction_MockForm_TestExampleAction extends CM_FormAction_Abstract {

protected function _process(CM_Params $params, CM_Http_Response_View_Form $response, CM_Form_Abstract $form) {
CM_Form_AbstractTest::$formActionProcessCount++;
Expand Down

0 comments on commit f77aab7

Please sign in to comment.