I have an form with multiple fields and I want to send data using OO-Programming, using the controller to get the data and setting/getting data in the model, I´m OK with doing this, but I thinking in case I have huge multiple form, how can I send data to model.
I created a form with few fields and used the controller and jQuery to get the form data and send it to the model.
Controller:
class FormController {
constructor() {
let $ = document.querySelector.bind(document);
this.inputFname = $('#fname');
this.inputLname = $('#lname');
this.inputAge = $('#age');
}
add(event) {
event.preventDefault();
new Form(
this.inputFname.value,
this.inputLname.value,
this.inputAge.value
);
}
}
Model:
class Form {
constructor(fname, lname, age) {
this.fname = fname;
this.lname = lname;
this.age = age;
}
get fullname() {
return this.fname * this.lname;
}
get fname() {
return this.fname;
}
get lname() {
return this.lname;
}
get age() {
return this.age;
}
}
class form {
constructor(fname, lname, age) {
this.fname = fname;
this.lname = lname;
this.age = age;
}
In form class constructor, in case I have multiple form fields, how can I send data to the constructor? If I have 20 fields in my form for example, do I necessarily need to write field by field and also create this.fieldname
for each of the 20 fields?