From 17c83f8db9983c798f0e179019e6ae3dc36fa404 Mon Sep 17 00:00:00 2001 From: hide_d Date: Fri, 20 Aug 2021 01:30:34 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20jqValidateForm=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EB=B3=B5=ED=95=A9=20=ED=83=80=EC=9E=85(radio,=20checkbox)=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/ts/util/jqValidateForm.ts | 49 +++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/hwe/ts/util/jqValidateForm.ts b/hwe/ts/util/jqValidateForm.ts index f26a7fd8..4e12e7ac 100644 --- a/hwe/ts/util/jqValidateForm.ts +++ b/hwe/ts/util/jqValidateForm.ts @@ -5,7 +5,7 @@ import $ from 'jquery'; type Option = { preParse?: ($target?: JQuery)=>void, - postParse?: (values:Record, $target?: JQuery)=>Record + postParse?: (values:Record, $target?: JQuery)=>[Record, Map] } export class JQValidateForm { @@ -13,7 +13,7 @@ export class JQValidateForm { public readonly inputs: JQuery; constructor(public readonly target: JQuery, public readonly rule: Rules, public readonly option?:Option) { this.validator = new Schema(rule); - this.inputs = target.find('input'); + this.inputs = target.find(':input'); } public clearErrMsg():void { @@ -34,30 +34,57 @@ export class JQValidateForm { this.option.preParse(this.target); } let rawValues = mergeKVArray(this.inputs.serializeArray()); + let optMap: Map; if(this.option?.postParse !== undefined){ - rawValues = this.option.postParse(rawValues, this.target); + [rawValues, optMap] = this.option.postParse(rawValues, this.target); } + else{ + optMap = new Map(); + } + console.log(rawValues); + const validateResult = await this.validator.validate(rawValues).catch(({ fields }) => { this.clearErrMsg(); - for(const key of Object.keys(fields)){ + for(const rawKey of Object.keys(fields)){ let $item: JQuery; - const errMsg = fields[key][0].message; - if(isArray(rawValues[key])){ - $item = $(`#db_form input[name='${key}[]']`); + const key = rawKey.split('.')[0]; + console.log(`ErrorType: ${key}:${rawValues[key]}`); + const errMsg = fields[rawKey][0].message; + + if(optMap.has(key)){ + $item = this.target.find(optMap.get(key) as string); + } + else if(isArray(rawValues[key])){ + $item = this.target.find(`:input[name='${key}[]']`); } else{ - $item = $(`#db_form input[name='${key}']`); + $item = this.target.find(`:input[name='${key}']`); + } + + if($item.length == 0){ + continue; } - $item.addClass('is-invalid'); const $error = $(`${errMsg}`); $error.addClass( "invalid-feedback" ); - if ( $item.prop( "type" ) === "checkbox" ) { - $error.insertAfter( $item.parent( "label" ) ); + if ("radio" == $item.prop( "type" )) { + const $target = $item.parents( ".btn-group" ); + $error.insertAfter( $target ); + $target.addClass('is-invalid'); + } + else if ("checkbox" == $item.prop( "type" )) { + let $target = $item.parent( "label" ); + if($target.parent(".btn-group").length){ + $target = $target.parent('.btn-group'); + } + $error.insertAfter( $target ); + $target.addClass('is-invalid'); } else { $error.insertAfter( $item ); + $item.addClass('is-invalid'); + } }