title: How to make a field conditionally optional in WTForms?
body: <p>My form validation is working nearly complete, I just have 2 cases I don't know exactly how to solve: 1) The password field should be required of course but I also provide the possibility to log in with google or facebook account via OAuth and then name gets prefilled but I remove the password field completely from the form is there is a user (google) or a facebook user object:</p>
  
  <pre><code>&lt;tr&gt;&lt;td&gt;
    &lt;br /&gt;        {% if user or current_user %}    {% else %} 
  
    &lt;div class="labelform"&gt;
       {% filter capitalize %}{% trans %}password{% endtrans %}{% endfilter %}:
    &lt;/div&gt;
        &lt;/td&gt;&lt;td&gt;  &lt;div class="adinput"&gt;{{ form.password|safe }}{% trans %}Choose a password{% endtrans %}&lt;/div&gt;{% endif %}
  
    &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>So for these users who already are logged in and the password field has no meaning, I need some logic to make that field conditionally optional. I was thinking that I could have a variable for logged_in + a method in my form class such as this:</p>
  
  <pre><code>class AdForm(Form):
      logged_in = False
      my_choices = [('1', _('VEHICLES')), ('2', _('Cars')), ('3', _('Bicycles'))]
      name = TextField(_('Name'), [validators.Required(message=_('Name is required'))], widget=MyTextInput())
      title = TextField(_('title'), [validators.Required(message=_('Subject is required'))], widget=MyTextInput())
      text = TextAreaField(_('Text'),[validators.Required(message=_('Text is required'))], widget=MyTextArea())
      phonenumber = TextField(_('Phone number'))
      phoneview = BooleanField(_('Display phone number on site'))
      price = TextField(_('Price'),[validators.Regexp('\d', message=_('This is not an integer number, please see the example and try again')),validators.Optional()] )
      password = PasswordField(_('Password'),[validators.Optional()], widget=PasswordInput())
      email = TextField(_('Email'), [validators.Required(message=_('Email is required')), validators.Email(message=_('Your email is invalid'))], widget=MyTextInput())
      category = SelectField(choices = my_choices, default = '1')
  
      def validate_name(form, field):
          if len(field.data) &gt; 50:
              raise ValidationError(_('Name must be less than 50 characters'))
  
      def validate_email(form, field):
          if len(field.data) &gt; 60:
              raise ValidationError(_('Email must be less than 60 characters'))
  
      def validate_price(form, field):
          if len(field.data) &gt; 8:
              raise ValidationError(_('Price must be less than 9 integers'))
  
      def validate_password(form, field):
          if not logged_in and not field:
              raise ValidationError(_('Password is required'))
  </code></pre>
  
  <p>Will the above validate_password work to achieve the desired effect? Is there another better way? Another way I could think is to have 2 different form class and in http post I instanciate the form class it should be:</p>
  
  <pre><code>def post(self):
      if not current_user:
        form = AdForm(self.request.params)
      if current_user:
        form = AdUserForm(self.request.params)
  </code></pre>
  
  <p>I also need conditional validation for the category field, when a certain category is selected then more choices appear and these should have validation only for a certain base-category eg. user selects "Car" and then via Ajax can choose registration data and mileage for the car and these fields are required given that the category Car was selected.</p>
  
  <p>So it might be two questions but both cases relate to how I can make a field "conditionally optional" or "conditionally required".</p>
  
  <p>My form looks like this</p>
  
  <p><img src="https://i.stack.imgur.com/KPab2.png" alt="enter image description here"></p>
  
  <p>And for a logged in user I prefill the name and email address and the pasword field is simply not used, so the password field neither fits being "optional" nor "required", it would need something like "conditionally optional" or "conditionally required."</p>
  
  <p><img src="https://i.stack.imgur.com/u6g9u.png" alt="enter image description here"></p>
  
  <p>Thanks for any answer or comment</p>