StopValidation() In route without raiseing wsgi debuger flask wtf
<p>I need to overwrite the Vlaidator in the form and apply my own validator this specific route.
When the  image uploaded is not a  jpg then it  will not pass the form.validate_on_submit() check and the template will render the errors in the html. When i try to use</p>

<pre><code> raise StopValidation("image1 jpg only")
</code></pre>

<p><br>
It raises it debuger and it prevents me from seeing the route. I just want it display as a field.error with the other errors.</p>

<pre><code> @app.route('/test',methods=['GET','POST'])
    def test():
        form.image1.validators=[]
            if request.method == "POST" and  str(request.files['image1'].filename) != "":
                 if request.files['image1'].filename.split('.',1)[1] != "jpg" :
                    raise StopValidation("image1 jpg only")
                    print "image 1 not jpg"


            if form.validate_on_submit():
                # do stuff

        return render_template('test.html')
</code></pre>

<p>If you want to validate only jpg file, then make a custom validation instead of using StopValidation and just define in your form.</p>

<pre><code>def customvalidatorForImage(form, field):
    allowed_file = ['jpg','jpeg','png','gif']  # you can modify your valid file list here
    if form.image.data:
        if form.image.data.split('.',1)[1] not in allowed_file:
            raise ValidationError("Please enter a valid image file")     


class MyForm(Form):
    image = FileField("Image" , [customvalidatorForImage, validators.DataRequired("Image is required")])
</code></pre>

python wtforms flask-wtforms