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 || {};
bricks.need_formdata_fields = ['file', 'image', 'video', 'audio'];
bricks.FieldGroup = class extends bricks.VBox {
constructor(opts){
opts.leftMargin = '10px';
@ -27,6 +29,9 @@ bricks.FieldGroup = class extends bricks.VBox {
if (fields[i].uitype !== 'hide'){
dc.add_widget(box);
}
if(bricks.need_formdata_fields.includes(fields[i].uitype)){
form.need_formdata = true;
}
var txt = new bricks.Text({
otext:fields[i].label||fields[i].name,
dynsize:true,
@ -71,7 +76,7 @@ bricks.FormBody = class extends bricks.VScrollPanel {
this.form = form;
this.name_inputs = {};
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";
super(opts);
this.name_inputs = {};
this.need_formdata = false;
if (this.opts.title){
var t = new bricks.Title3({
otext:this.opts.title,
@ -192,10 +198,41 @@ bricks.Form = class extends bricks.VBox {
}
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(){
var running = new bricks.Running({target:this});
try {
var data = this.getValue();
var data;
if (this.need_formdata){
data = this.get_formdata();
} else {
data = this.getValue();
}
if (! data) {
running.dismiss();
return;
@ -211,7 +248,7 @@ bricks.Form = class extends bricks.VBox {
this.dispatch('submited', resp);
}
} catch (e){
bricks.debug('form submit error', e);
console.log('form submit error', e);
}
running.dismiss();
}

View File

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

View File

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