Skip to content

Commit

Permalink
[new] checkbox validator
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Nov 27, 2016
1 parent 5d42bd1 commit eb689f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/components/answer/CheckboxAnswer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,25 @@ import React from 'react';

import Configuration from '../../model/Configuration';
import FormUtils from '../../util/FormUtils';
import Constants from "../../constants/Constants";
import assign from "object-assign";

class InputPropertiesResolver {

static resolveValidationProperties(question) {
var props = {};
if (question[Constants.HAS_VALID_ANSWER] === false) {
props['validation'] = 'error';
props['help'] = question[Constants.HAS_VALIDATION_MESSAGE];
}
return props;
}
}


const CheckboxAnswer = (props) => {
var question = props.question;
return React.createElement(Configuration.inputComponent, {
return React.createElement(Configuration.inputComponent, assign({}, InputPropertiesResolver.resolveValidationProperties(question), {
type: 'checkbox',
label: props.label,
title: props.title,
Expand All @@ -16,7 +31,7 @@ const CheckboxAnswer = (props) => {
props.onChange(e.target.checked);
},
disabled: FormUtils.isDisabled(question),
});
}));
};

CheckboxAnswer.propTypes = {
Expand Down
21 changes: 21 additions & 0 deletions src/model/ValidatorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import JsonLdUtils from 'jsonld-utils';

import Constants from "../constants/Constants";
import Configuration from "../model/Configuration";
import FormUtils from "../util/FormUtils";

export default class ValidatorFactory {

static createValidator(question) {
if (question[Constants.REQUIRES_ANSWER]) {
if (FormUtils.isCheckbox(question)) { //TODO revise
return ValidatorFactory._generateRequiresAnswerCheckBoxValidator(question);
}
return ValidatorFactory._generateRequiresAnswerValidator(question);
} else {
return () => {
Expand All @@ -37,4 +41,21 @@ export default class ValidatorFactory {
return result;
}
}

static _generateRequiresAnswerCheckBoxValidator(question) {
return (answer) => {
var val = null;
if (answer[Constants.HAS_DATA_VALUE]) {
val = JsonLdUtils.getJsonAttValue(answer, Constants.HAS_DATA_VALUE);
} else if (answer[Constants.HAS_OBJECT_VALUE]) {
val = JsonLdUtils.getJsonAttValue(answer, Constants.HAS_OBJECT_VALUE, "@id");
}
var isValid = val !== null && val !== undefined && val !== "" && val !== false,
result = {};
result[Constants.HAS_VALID_ANSWER] = isValid;
result[Constants.HAS_VALIDATION_MESSAGE] = isValid ? null : JsonLdUtils.getLocalized(question[JsonLdUtils.RDFS_LABEL], Configuration.intl) + ' must be checked.';
return result;
}
}

}

0 comments on commit eb689f2

Please sign in to comment.