I don't know what language you're using (maybe C#?) but you should be referring to the fields posted form data as a collection rather than listing them individually. I think you should do this in an OOP approach using a Form object which contains field objects.
This is a very crude example of what I mean. You could actually have a Field interface implemented by concrete field classes like int, text, file etc.
class Field {
public bool isValid = true;
public string errorMessage;
public string value;
public Field(string name, string label, string type, bool required) {
}
}
class Form {
public List<Field> fields;
public bool isValid = false;
public void validate() {
foreach(Field field in fields) {
if(field.required && field.value = "") {
this.isValid = false;
field.isValid = false;
field.errorMessage = field.name+" field is required";
}
}
}
public void loadData(Array request) {
//stuff the request data into the field objects
foreach(KeyValuePair<string, string> kvp in request) {
fields.Where(x => x.name = kvp.Key).first().value = kvp.value;
}
}
}
On your post request you could do this
Form form = new Form();
//name, type, required
form.fields(new Field('field1','text',true);
if(Page.IsPostBack) {
form.loadData(Request);
form.Validate();
if(!form.isValid) {
//print some errors or whatever
throw new Exception(...);
}
}
Most web frameworks have Form and field classes, you could look at some framework APIs for ideas. You can also state exactly which field is left empty rather than just displaying a generic one or more fields are blank
message. Even if this is only for a web service it can be helpful to state which of the 40 or so fields are not valid.
Your other option is to define required fields in a collection and use a loop to check if each Request[fieldName]
is null.