WTForms具有可变数量的textareas(WTForms with a variable amount of textareas)

编程入门 行业动态 更新时间:2024-10-27 05:23:23
WTForms具有可变数量的textareas(WTForms with a variable amount of textareas)

我有一个带有我的Flask应用程序的HTML表单,其中包含一个textarea但是,用户可以按下一个按钮,在表单中添加另一个textarea以创建无限量的textareas。

因为我没有使用WTForms,验证要困难得多,所以我在客户端使用Javascript完成了验证,但是任何有编程技能的人都可以编辑这个不安全的代码。 我更愿意用WTForms来验证表单。

这甚至可以与WTForms和Flask一起使用,还是我必须使用Flask和闪烁消息创建我自己的方法来验证表单? 谢谢。

编辑:

HTML:

<form id="myForm"> <input type="text"> <textarea></textarea> <textarea></textarea> </form> <button onclick="add()">Add textarea</button> <script> function add() { var textarea = document.createElement('textarea'); document.getElementById('myForm').appendChild(textarea); } </script>

所以在我的情况下,我在开头也有一个输入,我想验证。 我还要验证所有textareas,以便它们不被允许为空。

我想用WTForms来验证这个,但是要用WTForms创建一个表单,它必须用Python创建,然后用HTML呈现。 由于textareas被添加到表单中,因此无法验证它们,因为它们未在python代码中定义以创建WTForms。

问题是,我希望能够将表单元素添加到使用WTForms创建的表单中。

I have a HTML form with my Flask application which includes a textarea however, the user can press a button which adds another textarea to the form to create an infinite amount of textareas.

As I'm not using WTForms, validation is much harder and so I've done it on the client side with Javascript however anyone with programming skills can edit this code which is unsafe. I would prefer to validate the form with WTForms.

Is this even possible to do with WTForms and Flask or would I have to create my own method with Flask and flashing messages to validate the form? Thanks.

Edit:

HTML:

<form id="myForm"> <input type="text"> <textarea></textarea> <textarea></textarea> </form> <button onclick="add()">Add textarea</button> <script> function add() { var textarea = document.createElement('textarea'); document.getElementById('myForm').appendChild(textarea); } </script>

So in my case, I also have an input at the beginning which I would like to validate. I would also like to validate that all the textareas so that they are not allowed to be empty.

I would like to validate this with WTForms though to create a form with WTForms, it must be created in Python and then rendered in HTML. As textareas are added to the form, they cannot be validated as they are not defined in the python code to create the WTForms.

The issue is, I would like to be able to add form elements to a form created with WTForms.

最满意答案

我解决这个问题的方法是使用WTForms验证<input type=text> ,然后使用简单的flash()消息为textareas(如果它们为空)。

@app.route('/form') def form(): form = some_form() if request.method == 'POST': newQuestions = [] i = 1 alreadyBlank = False while True: try: question = request.form['question' + str(i)] if question.strip(' \t\n\r') == '' and not alreadyBlank flash('Please don\'t leave any questions blank.') alreadyBlank = True newQuestions.append(question) i += 1 except: break if form.validate_on_submit(): input_content = form.input.data

The way I solved this was by using WTForms to validate the <input type=text> and then using simple flash() messages for the textareas if they were empty.

@app.route('/form') def form(): form = some_form() if request.method == 'POST': newQuestions = [] i = 1 alreadyBlank = False while True: try: question = request.form['question' + str(i)] if question.strip(' \t\n\r') == '' and not alreadyBlank flash('Please don\'t leave any questions blank.') alreadyBlank = True newQuestions.append(question) i += 1 except: break if form.validate_on_submit(): input_content = form.input.data

更多推荐

本文发布于:2023-08-01 09:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1356718.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数量   WTForms   textareas   amount   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!