使用WTForms自定义验证器编辑现有数据库字段时检查重复 [英] Check duplication when edit an exist database field with WTForms custom validator

查看:44
本文介绍了使用WTForms自定义验证器编辑现有数据库字段时检查重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与此验证程序一起使用此表单,以实现唯一性:

I'm having this form with this validator for uniqueness:

class CreateTypeForm(FlaskForm):
    type_name = StringField("Type", validators=[DataRequired()])
    description = TextAreaField("Description")
    submit = SubmitField("Create")

    def validate_type_name(self, type_name):
        typeName = mongo.db.types.find_one({"type_name": type_name.data})
        if typeName:
            raise ValidationError("The type already exists.")

但是当我编辑 type 时,需要更改我具有的表单的验证器:

But need to alter the validator for the form I have when I edit my type:

class EditTypeForm(FlaskForm):
    type_name = StringField("Type", validators=[DataRequired()])
    description = TextAreaField("Description")
    submit = SubmitField("Update")

因此它检查重复项,但是如果我仅编辑描述而不是名称,则允许我保留当前的 type_name .这是怎么做的?

So it checks for duplicates but allowing me to keep my current type_name if I were to edit only the description and not the name. What's the way to do this?

简而言之,如何检查类型名称的重复性,但又允许保存原始类型名称?

In short, how to check the duplication of the type name but also allow saving the original type name?

更新:

根据以下答案的温和建议,我现在的状态是:

Following the gentle advice from the answer below, I have now in my form:

class EditTypeForm(FlaskForm):
    origin_type_name = HiddenField()
    type_name = StringField("Type", validators=[DataRequired()])
    description = TextAreaField("Description")
    submit = SubmitField("Update")

    def validate_type_name(self, type_name):
        typeName = mongo.db.types.find_one({"type_name": type_name.data})
        if typeName and type_name.data != self.origin_type_name.data:
            raise ValidationError("The type already exists.")

在路线中:

@app.route("/type/edit/<id>", methods=["POST", "GET"])
@login_required
def edit_type(id):
    form = EditTypeForm()
    query = mongo.db.types.find_one(
        {"_id": ObjectId(id)}, {"_id": 0, "type_name": 1}
    )
    current_type_name = query['type_name']
    form.origin_type_name.data = current_type_name
    (etc...)

现在可以正常使用了!

推荐答案

也许有更好的解决方案,但是我得到了这个解决方案:

Maybe there are better solutions, but I got this one:

  1. 将原始类型名称存储在一个隐藏字段中,然后在自定义验证器中对其进行检查:

class EditTypeForm(FlaskForm):
    origin_type_name = HiddenField()  # import HiddenField first
    type_name = StringField("Type", validators=[DataRequired()])
    description = TextAreaField("Description")
    submit = SubmitField("Create")

    def validate_type_name(self, type_name):
        typeName = mongo.db.types.find_one({"type_name": type_name.data})
        if typeName and type_name.data != self.origin_type_name.data:
            raise ValidationError("The type already exists.")

  1. 在渲染模板之前,在视图函数中设置原始类型名称:

form = EditTypeForm()
form.origin_type_name.data = the_origin_data_get_from_database

这篇关于使用WTForms自定义验证器编辑现有数据库字段时检查重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆