From d664f4d2235d267eae9c02c4ed9ab2962a30e874 Mon Sep 17 00:00:00 2001 From: kujyp Date: Sat, 10 Mar 2018 17:16:01 +0900 Subject: [PATCH] Add EntityForm class for validation #17 --- src/components/Form.js | 137 ++++++++++++++++++++++++----------------- src/data/EntityForm.js | 11 ++++ 2 files changed, 90 insertions(+), 58 deletions(-) create mode 100644 src/data/EntityForm.js diff --git a/src/components/Form.js b/src/components/Form.js index e0c5583..b8496ec 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -6,6 +6,7 @@ import LinkedText from "./LinkedText"; import "./Form.css"; import { RESTAPI_HOST } from "../consts/config"; import {isValid, notEmptyString} from "../utils/ValidationUtils"; +import {EntityForm, TYPE_BOOLEAN, TYPE_ONELINETEXT} from "../data/EntityForm"; const libraryName = "libraryName"; @@ -28,41 +29,55 @@ const otherFacilities = "otherFacilities"; class Form extends Component { - validator = { - [libraryName]: [notEmptyString], - [libraryLocation]: [notEmptyString], - [managerName]: [notEmptyString], - [managerEmail]: [notEmptyString], - [managerPhonenumber]: [notEmptyString], - [capacityOfAudiences]: [notEmptyString], - // [facilities]: [], - // [requirementsForSpeaker]: [], - // [personalInfoAgreement]: [], - [noVolunteerAgreement]: [], - [otherFacilities]: [], - }; - state = { - [libraryName]: null, - [libraryLocation]: null, - [managerName]: null, - [managerEmail]: null, - [managerPhonenumber]: null, - [capacityOfAudiences]: null, - [facilities]: { + [libraryName]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [libraryLocation]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [managerName]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [managerEmail]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [managerPhonenumber]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [capacityOfAudiences]: new EntityForm("", TYPE_ONELINETEXT, true, [notEmptyString]), + [facilities]: new EntityForm({ [facilitiesBeamOrScreen]: false, [facilitiesSound]: false, [facilitiesRecord]: false, [facilitiesPlacard]: false, [facilitiesSelfPromotion]: false, - }, - [requirementsForSpeaker]: null, - [personalInfoAgreement]: null, - [noVolunteerAgreement]: null, - [otherFacilities]: null, + }, TYPE_ONELINETEXT, true, []), + [requirementsForSpeaker]: new EntityForm("", TYPE_ONELINETEXT, false), + [otherFacilities]: new EntityForm("", TYPE_ONELINETEXT, false), + [personalInfoAgreement]: new EntityForm("", TYPE_BOOLEAN, true, [notEmptyString]), + [noVolunteerAgreement]: new EntityForm("", TYPE_BOOLEAN, true, [notEmptyString]), }; + componentWillMount() { + // this.syncFormsToState(); + } + + // syncFormsToState() { + // let stateBuffer = {}; + // for (let key in this.forms) { + // stateBuffer[key] = this.forms[key].value; + // // console.log(`stateBuffer[${key}]=`, this.state[key]); + // } + // + // this.setState(stateBuffer); + // } + + everyFormsAreValid() { + for (let key in this.forms) { + let eachForm = this.forms[key]; + if (!isValid(eachForm.value, eachForm.validators)) { + return false; + } + } + + return true; + } + onSubmit = (event) => { + if (this.everyFormsAreValid()) { + + } request .post({ url: RESTAPI_HOST + 'api/v1/forms', @@ -83,31 +98,37 @@ class Form extends Component { handleChangeWithName = (propertyName) => { return (event) => { - this.setState({ - [propertyName]: event.target.value, - }); - console.log("propertyName=", propertyName, "this.state[propertyName]=", this.state[propertyName]); + this.setValue(propertyName, event.target.value); + console.log("propertyName=", propertyName, "this.state[propertyName].value=", this.state[propertyName].value); }; }; + setValue = (propertyName, value) => { + this.setState({ + ...this.state, + [propertyName]: { + ...this.state[propertyName], + value: value, + } + }); + }; + handleRadioChangeWithName = (propertyName) => { return (unused_event, unused_value) => { - this.setState({ - [propertyName]: true, - }); - console.log("propertyName=", propertyName, "this.state[propertyName]=", this.state[propertyName]); + this.setValue(propertyName, true); + console.log("propertyName=", propertyName, "this.state[propertyName].value=", this.state[propertyName].value); }; }; handleCheckedChangeWithName(propertyName, checkedPropertyName) { return (event, isInputChecked) => { - this.setState({ + this.setValue(propertyName, { [propertyName]: { - ...this.state[propertyName], + ...this.state[propertyName].value, [checkedPropertyName]: isInputChecked } }); - console.log("propertyName=", propertyName, "this.state[propertyName]=", this.state[propertyName]); + console.log("propertyName=", propertyName, "this.state[propertyName].value=", this.state[propertyName].value); }; } @@ -134,66 +155,66 @@ class Form extends Component { @@ -205,31 +226,31 @@ class Form extends Component { @@ -239,7 +260,7 @@ class Form extends Component { required={false}> @@ -249,14 +270,14 @@ class Form extends Component { required={false}>