I'm trying to capture all input fields in a form (i.e. sign_up.html) & validating them - if one of them is null or empty then it should show a validation message. I've done it but I'm looking for an elegant way to do it.
sign_up.html
{% extends "layout.html" %}
{% block main %}
<form action="/sign_up.html" method="POST" autocomplete="off" name="sign_up">
<h2>Sign Up</h2>
<label for="username">User name:
<input type="text" placeholder="Username" name="username" required="required" maxlength="20">
</label>
<br>
<sup>* Password must be 8-12 Digit ,Numbers & letters & punctuations @, !, $, ETC..</sup>
<label for="password">Password:
<input type="password" placeholder="password" name="password" required="required" minlength="8" maxlength="12">
</label>
<label for="check">Check Password:
<input type="password" placeholder="Check Password" name="check" required="required" minlength="8" maxlength="12">
</label>
<br>
<label for="security_question">Security question :
<select name="security_question">
<option value="where were you born?" name="q1" >where were you born?</option>
<option value="what is your favorite nickname?" name="q2" >what is your favorite nickname?</option>
<option value="what is the name of your first pet?" name="q3" >what is the name of your first pet?</option>
<option value="what is the name of your first best friend?" name="q4" >what is the name of your first best friend?</option>
</select>
</label>
<label for="answer">Security answer:
<input type="text" placeholder="Answer" name="answer" required="required">
</label>
<br>
<input type="submit" value="Sign Up">
</form>
{% endblock %}
this is my spaghetti validation python code:
from flask import Flask, flash, redirect, render_template, Request, request, session
from werkzeug.datastructures import ImmutableOrderedMultiDict
from werkzeug.utils import HTMLBuilder
#Something in between
class OrderedRequest(Request):
parameter_storage_class = ImmutableOrderedMultiDict
app = Flask(__name__)
app.request_class = OrderedRequest
#Something in between
@app.route("/sign_up.html", methods=["GET", "POST"])
def sign_up():
if request.method == "POST":
#assigning returning page
page = "sign_up.html"
#Null fields checking
if not request.form.get("username"):
return render_template("errors/general_error.html", error = "Username field is Empty", page = page)
elif not request.form.get("password"):
return render_template("errors/general_error.html", error = "Password field is Empty", page = page)
elif not request.form.get("check"):
return render_template("errors/general_error.html", error = "Check Password field is Empty", page = page)
elif not request.form.get("answer"):
return render_template("errors/general_error.html", error = "answer field is Empty", page = page)
#something at the end
this will return a page with the validation error massage.
I need to loop through an element gives me names of all the inputs inside the form & I'll return the error massage due to it..
& code will be like:
for i in needed_element:
if not i:
return render_template("errors/general_error.html", error = f"{i} field is Empty", page = page)
or something similar or any more elegant way because I'm repeating this step many times in the project :'D