This commit is contained in:
yumoqing 2024-03-27 11:27:24 +08:00
parent 93b4ef64d8
commit e459801466
3 changed files with 48 additions and 9 deletions

View File

@ -1,5 +1,7 @@
var bricks = window.bricks || {}; var bricks = window.bricks || {};
bricks.need_formdata_fields = ['file', 'image', 'video', 'audio'];
bricks.FieldGroup = class extends bricks.VBox { bricks.FieldGroup = class extends bricks.VBox {
constructor(opts){ constructor(opts){
opts.leftMargin = '10px'; opts.leftMargin = '10px';
@ -27,6 +29,9 @@ bricks.FieldGroup = class extends bricks.VBox {
if (fields[i].uitype !== 'hide'){ if (fields[i].uitype !== 'hide'){
dc.add_widget(box); dc.add_widget(box);
} }
if(bricks.need_formdata_fields.includes(fields[i].uitype)){
form.need_formdata = true;
}
var txt = new bricks.Text({ var txt = new bricks.Text({
otext:fields[i].label||fields[i].name, otext:fields[i].label||fields[i].name,
dynsize:true, dynsize:true,
@ -71,7 +76,7 @@ bricks.FormBody = class extends bricks.VScrollPanel {
this.form = form; this.form = form;
this.name_inputs = {}; this.name_inputs = {};
this.fg = new bricks.FieldGroup({}); this.fg = new bricks.FieldGroup({});
this.fg.build_fields(this.form, this, form.opts.fields) this.fg.build_fields(form, this, form.opts.fields)
} }
} }
@ -96,6 +101,7 @@ bricks.Form = class extends bricks.VBox {
opts.overflow = "auto"; opts.overflow = "auto";
super(opts); super(opts);
this.name_inputs = {}; this.name_inputs = {};
this.need_formdata = false;
if (this.opts.title){ if (this.opts.title){
var t = new bricks.Title3({ var t = new bricks.Title3({
otext:this.opts.title, otext:this.opts.title,
@ -192,10 +198,41 @@ bricks.Form = class extends bricks.VBox {
} }
return data; return data;
} }
get_formdata(){
var data = new FormData();
for (var name in this.name_inputs){
if (! this.name_inputs.hasOwnProperty(name)){
continue;
}
var w = this.name_inputs[name];
var d = w.getValue();
if (w.required && ( d[name] == '' || d[name] === null)){
bricks.debug('data=', data, 'd=', d);
new bricks.Error({title:'Requirement', message:'required field must input"' + w.label + '"'})
w.focus();
return;
}
if (bricks.need_formdata_fields.includes(w.uitype)){
var files = w.get_files();
for (var i=0;i<files.length;i++){
// data.append(name, w.fileContents[i].body, w.fileContents[i].filename);
data.append(name, files[i]);
}
} else {
data.append(name, d[name]);
}
}
return data;
}
async validation(){ async validation(){
var running = new bricks.Running({target:this}); var running = new bricks.Running({target:this});
try { try {
var data = this.getValue(); var data;
if (this.need_formdata){
data = this.get_formdata();
} else {
data = this.getValue();
}
if (! data) { if (! data) {
running.dismiss(); running.dismiss();
return; return;
@ -211,7 +248,7 @@ bricks.Form = class extends bricks.VBox {
this.dispatch('submited', resp); this.dispatch('submited', resp);
} }
} catch (e){ } catch (e){
bricks.debug('form submit error', e); console.log('form submit error', e);
} }
running.dismiss(); running.dismiss();
} }

View File

@ -325,6 +325,13 @@ bricks.UiFile =class extends bricks.UiStr {
this.dom_element.capture = this.opts.capture; this.dom_element.capture = this.opts.capture;
if (this.opts.multiple) if (this.opts.multiple)
this.dom_element.multiple = true; this.dom_element.multiple = true;
this.fileContents = [];
}
get_files(){
return this.dom_element.files;
}
resultValue(){
return this.get_files();
} }
setValue(v){ setValue(v){
return; return;

View File

@ -120,18 +120,13 @@
}, },
{ {
"uitype":"checkbox", "uitype":"checkbox",
"multicheck":true,
"name":"checkbox", "name":"checkbox",
"label":"CheckBox", "label":"CheckBox",
"value":2, "value":2,
"dataurl":"test_code.json", "dataurl":"test_code.json",
'required':true 'required':true
}, },
{
"uitype":"audio",
"name":"audio",
"label":"Audio",
'required':true
},
{ {
"uitype":"code", "uitype":"code",
"name":"code", "name":"code",