Terminology
Element/Form Element -> Fields such as text list, combo_boxes, push buttons, etc.
Problem
I have a program that is basically a fat client that interfaces into a system that keeps track of traffic lights.
In this program exists a form. The form contains several subforms. As it is right now, some of the subforms have their own classes, and some of the subforms are just controlled completely from the class of the main form.
The main form class has a validate method which is called when the form is submitted and calls validation methods within the subforms, as well as performing validation on some of the form elements that don't have their own classes.
I'm not sold on this architecture, as I feel that it should be consistent (every subform should have its own class, or no subform should have it's own class.
My problem lies in the implementation of handling warnings. Right now, when an element is validated against, the validation results in an error or a warning. If it is an error, the user must correct the box to meet the forms validation requirements. If it is a warning, the user must be notified of that warning, and then submit the form a second time.
If a warning occurs, I have created a flag that will be set. Before the information within the form elements can be accepted, the form first checks if any errors were thrown. In the event there are no errors, it checks for warnings. If the warning flag is set, it displays the warning. The next time this method is called, the warning flag is toggled within the validation method.
I don't really like this implementation, as it basically means you have this rogue flag that should be cleared based on the requirements. I've thought about implementing it as a state machine, however you run into the same problem where the state of the form would be set in various spots within the class based on the requirements (One of the fields changed, reset to the starting state, etc).
The other problem that I ran into is that the warning flag is owned by the main forms class. I can create a pointer to the parent form from the subform, and then set the flag from there.
This all feels very clunky, and I feel like it could create a lot of confusion. To me there has to be a better way to architect this.
EDIT: Adding some code for context
bool MainForm::callback_pushbutton_submit_clicked( const FormEvent&)
{
if ( validate_all_forms() )
{
//display error
}
else if ( get_warning_displayed_flag() )
{
//display warning
}
else
{
//submit form
}
}
bool MainForm::validate_all_forms()
{
bool error = false;
bool display_warning = false;
error |= subform1.validate();
error |= subform2.validate();
//...
//perform some validation on subforms that are controlled within the main form
if ( !(my_date_field.validate() )
{
display_warning = true;
}
//....
if( error )
{
//format error message
//clear warning flag
}
else if ( display_warning )
if ( !get_display_warning_flag() )
{
//get warning message
}
invert_warning_display_flag()
}
return error;