Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form list Validation changes #151

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions lib/components/FormList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const async = require('async');
const FORMLIST_CHANGE_MESSAGE = 'mlformlistchange';

const MLFormList = module.exports = milo.createComponentClass({
Expand Down Expand Up @@ -27,7 +27,9 @@ const MLFormList = module.exports = milo.createComponentClass({
init: MLFormList$init,
moveItem: MLFormList$moveItem,
setItemSchema: MLFormList$setItemSchema,
destroy: MLFormList$destroy
destroy: MLFormList$destroy,
validateModel: MLFormList$validateModel,
clearSubSchemaValidation: MLFormList$clearSubSchemaValidation
}
});

Expand All @@ -52,6 +54,7 @@ function handleClick (type, event) {
function MLFormList$init () {
MLFormList.super.init.apply(this, arguments);
this.once('childrenbound', onChildrenBound);
this._invalidFormControls = {};
}

function MLFormList$setItemSchema (schema) {
Expand Down Expand Up @@ -126,3 +129,91 @@ function _triggerExternalPropagation () {
this.data.dispatchSourceMessage(FORMLIST_CHANGE_MESSAGE);
showHidePrepend.call(this);
}

function MLFormList$clearSubSchemaValidation () {
this._invalidFormControls = {};
}

function MLFormList$validateModel (callback, invalidControls) {
const validations = [];
const self = this;
this._dataValidations = { fromModel: {} };
(this.model.m().get() || []).forEach((data, index) => {
this._subFormSchema.items.forEach((item) => {
if (item.validate && item.validate.fromModel && item.validate.fromModel[0] === 'required') {
this._dataValidations.fromModel[`${index}${item.modelPath}`] = [validateRequired];
}
});
});

_.eachKey(this._dataValidations.fromModel, function (validators, modelPath) {
const [index, path] = modelPath.split('.');
const data = (this.model.m().get() || [])[index][path];
validators = Array.isArray(validators) ? validators : [validators];

if (validators && validators.length) {
validations.push({
modelPath: modelPath,
data: data,
validators: validators
});
}
}, this);


let allValid = true;
async.each(validations,
function (validation, nextValidation) {
let lastResponse;
async.every(validation.validators,
function (validator, next) {
validator(validation.data, function (err, response) {
lastResponse = response || {};
next(err, lastResponse.valid);
});
},
function (err, valid) {
lastResponse.path = validation.modelPath;
lastResponse.valid = valid;
handleValidatedComponents.call(self, lastResponse, invalidControls);
if (!valid) allValid = false;
nextValidation(null);
}
);
},
function (err) {
invalidControls = Object.assign({}, invalidControls, self._invalidFormControls);
callback && callback({allValid, invalidControls});
}
);
}

function validateRequired(data, callback) {
const valid = typeof data != 'undefined'
&& (typeof data != 'string' || data.trim() != '');
const response = MLForm$$validatorResponse(valid, 'please enter a value', 'REQUIRED');
callback(null, response);
}

function MLForm$$validatorResponse(valid, reason, reasonCode) {
return valid
? { valid: true }
: { valid: false, reason: reason, reasonCode: reasonCode };
}

function handleValidatedComponents(response) {
if (response.valid) {
delete this._invalidFormControls[response.path];
} else {
const [index, modelPath] = response.path.split('.');
let reason = {
label: `List Item ${Number(index)+1}. ${modelPath}`,
reason: response.reason,
reasonCode: response.reasonCode
};
this._invalidFormControls[response.path] = {
reason: reason
};
}
}

16 changes: 16 additions & 0 deletions lib/forms/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ _.extendProto(MLForm, {
viewPathSchema: MLForm$viewPathSchema,
getModelPath: MLForm$getModelPath,
getViewPath: MLForm$getViewPath,
getSubSchemas: MLForm$getSubSchemas,
destroy: MLForm$destroy,
});

Expand Down Expand Up @@ -542,6 +543,21 @@ function MLForm$viewPathComponent(viewPath) {
return viewPathObj && viewPathObj.component;
}

/**
* Returns subSchemas of type formList
*
* @return {Schemas}
*/
function MLForm$getSubSchemas() {
let subSchemas = [];
for(const value in this._formViewPaths) {
if(Object.hasOwn(this._formViewPaths, value) && this._formViewPaths[value].schema && this._formViewPaths[value].schema.type === "formlist") {
subSchemas.push(this._formViewPaths[value]);
}
}
return subSchemas;
}


/**
* Returns form schema for a given view path item (path as defined in Data facet)
Expand Down