简体   繁体   English

Flask WTForms SelectMultipleField限制最大选择数

[英]Flask WTForms SelectMultipleField limit maximum number of selections

Right now I am building a webapp with Flask, including some WTForms. 现在,我正在使用Flask构建Web应用程序,其中包括一些WTForms。 One of them is a checklist where people can select from multiple options (hence a SelectMultipleField). 其中之一是清单,人们可以从中选择多个选项(因此有一个SelectMultipleField)。 Now I want to make sure that people can only select a maximum number of options (say 2 out of 3) and if they want to select more first have to de-select one of the other options. 现在,我要确保人们只能选择最大数量的选项(例如3个选项中的2个),如果他们要选择更多选项,则必须取消选择其他选项之一。 I think it might have to do something with validation, but I am really not sure how to do this...Any help is much appreciated! 我认为可能需要进行验证,但是我真的不确定如何执行此操作……非常感谢您的帮助! This is my code from forms.py 这是来自forms.py的代码

class MultiCheckboxField(SelectMultipleField):
    option_widget = widgets.CheckboxInput()
    widget = widgets.ListWidget(prefix_label = False)

class ChecklisteForm(FlaskForm):
    list_of_files = ['politiek', 'economie', 'sport']
    files = [(x, x) for x in list_of_files]
    example = MultiCheckboxField('Label', choices=files)
    submit = SubmitField('Wijzigen')

This is the code from my html template 这是我的html模板中的代码

<form action="" method = "post">
    {{ form.hidden_tag() }}
<div class="container-fluid">
  <div class="row">
     <div class="col-lg-12">
     <div class="col-lg-4">
     <div class="button-group">
        <span class="kiezen_informatie">
        <button type="button" class="btn btn-info btn-large dropdown-toggle" data-toggle="dropdown">Kies kategorie </span> <span class="caret"></span></button>
        <ul class="dropdown-menu text-center">
        <li><a href = "#">{{form.example(class = "list-unstyled")}}</a></li>
        <li><a href = "#"> {{form.submit(class = "btn btn-info")}}</a></li>
        </ul>
      </div>    
     </div>
    </div>
   </div>
  </div>

I thought this might also be related to including some javascript? 我认为这可能还与包含一些JavaScript有关? But if there is any way I could avoid that I'd be happy :) 但是如果有什么办法可以避免的话,我会很高兴的:)

You can certainly validate this on the client side with some Javascript, but you'd also want to validate this on the server side to ensure that users don't avoid the Javascript validation and submit data they shouldn't. 您当然可以在客户端使用某些Javascript对此进行验证,但是您还希望在服务器端对其进行验证,以确保用户不要避免Javascript验证并提交不应接受的数据。

You can define a custom validate method in your Form: 您可以在表单中定义自定义validate方法:

class ChecklisteForm(FlaskForm):                                                
    list_of_files = ['politiek', 'economie', 'sport']                           
    files = [(x, x) for x in list_of_files]                                     
    example = MultiCheckboxField('Label', choices=files)                        
    submit = SubmitField('Wijzigen')                                            

    def validate(self):                                                         

        rv = FlaskForm.validate(self)                                           

        if not rv:                                                              
            return False                                                        

        print(self.example.data)                                                

        if len(self.example.data) > 2:                                          
            self.example.errors.append('Please select no more than 2 items')    
            return False                                                        

        return True 

and in your HTML template: 在您的HTML模板中:

    {% for key in form.errors %}                                            
        {% for error in form.errors[key] %}                                 
            <span style="color: red;">[{{ error }}]</span>                  
        {% endfor %}                                                        
    {% endfor %}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM