我可以用变量更改flask wtforms验证器



这是表单

class VideoUploadForm(FlaskForm):
username = StringField('username', validators=[DataRequired()])
title = StringField('title', validators=[DataRequired()]) #equalTo("필드네임")
video = StringField('video', validators=[DataRequired()])
start_h = IntegerField('start_h', validators=[NumberRange(min=0)],default=0)
start_m = IntegerField('start_m', validators=[NumberRange(min=0, max=59)],default=0)
start_s = IntegerField('start_s', validators=[NumberRange(min=0, max=59)],default=0)
time = IntegerField('time', validators=[DataRequired(), NumberRange(min=1, max=60)], default=20)
video_link = HiddenField('video_link', validators=[DataRequired()])

我试着

form=VideoUploadForm()
max_time = 120
form.time.validators=[DataRequired(), NumberRange(min=1, max=max_time)]
return render_template('index.html', form=form)

我参考了这个网页。但是没有成功将变量传入WTForms类

我使用init传递任何运行时上下文:

class VideoUploadForm(FlaskForm):
username = StringField('username', validators=[DataRequired()])
title = StringField('title', validators=[DataRequired()]) #equalTo("필드네임")
video = StringField('video', validators=[DataRequired()])
start_h = IntegerField('start_h', validators=[NumberRange(min=0)],default=0)
start_m = IntegerField('start_m', validators=[NumberRange(min=0, max=59)],default=0)
start_s = IntegerField('start_s', validators=[NumberRange(min=0, max=59)],default=0)
time = IntegerField('time', validators=[DataRequired(), NumberRange()], default=20)
video_link = HiddenField('video_link', validators=[DataRequired()])
def __init__(self, *args, **kwargs):
super(VideoUploadForm, self).__init__(*args, **kwargs)
self.time.validators[1] = NumberRange(min=1, max=kwargs['max_time'])

创建表单:

form = VideoUploadForm(max_time=120)

相关内容

  • 没有找到相关文章

最新更新