Issue
I have one Parent form, and 2 Children forms, than inherit from it -
class ParentForm(FlaskForm):
number_a = StringField('A',
validators=[DataRequired()],
render_kw={"placeholder":"A", 'class_':'input', 'id':'number_a'})
number_b = StringField('B',
validators=[DataRequired()],
render_kw={"placeholder":"B", 'class_':'input', 'id':'number_b'})
class Child1Form(ParentForm):
number_c = StringField('C',
validators=[DataRequired()],
render_kw={"placeholder":"C", 'class_':'input', 'id':'number_c'})
class Child2Form(ParentForm):
number_d = StringField('D',
validators=[DataRequired()],
render_kw={"placeholder":"D", 'class_':'input', 'id':'number_d'})
For Child1Form I need number_a, number_b and number_c.
For Child2Form I need number_b and number_d, but I do not need number_a, so I don't submit it. This results in Validation error when I post the Child2Form, since in the ParentForm field is required.
How should I tackle this? Basically on certain forms I need to Validate the number_a field, on others I need to ignore it. But I don't want to type it multiple times, as I will potentially have a very large amount of forms.
Hopefully I got my point across, let me know if this is not the case.
Solution
This just sounds like the design problem in OOP of the "refused bequest". Something a child shouldn't do.
You should just arrange the inheritance hierarchy to cater for the needs of the child classes:
class ParentForm_B(FlaskForm):
number_b = ...
class ParentForm(ParentForm_B):
number_a = ...
class Child1Form(ParentForm):
number_c = ...
class Child2Form(ParentForm_B):
number_d = ...
Answered By - quamrana
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.