Let me start with usual - I'm new to both Python & Flask. Before posting this question (my first ever here) I have spent number of hours searching and experimenting, unfortunately with no luck so far.
I am building web form where user can define firewall rules, which subsequently get recorded in the database. I am at the validation stage and I came up to a wall here... hopefully someone will be able to help me out.
My (simplified here) form has 2 fields - src_ip and dst_ip:
class FirewallRule(Form)
src_ip = StringField('Source IP')
dst_ip = StringField('Destination IP')
and my validation requirements are:
- At least 1 field must be filled (both are also fine)
- Fields that are filled must contain valid IP address
wtforms.validators.IPAddress() and custom validation function seems to be my friend, but I am struggling to find a way to plug them together.
Essentially I am trying to build conditional validation:
- If not (src_ip OR dst_ip); return False
- If src_ip AND src_ip not valid IP address; return False
- If dst_ip AND dst_ip not valid IP address; return False
- Return True
Obviously I would like to re-use IPAddress() [or generally any of the built-in validators] rather then write my own.
I am sure someone must have done it before.. unfortunately I could not find any pointers in the right direction.
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…