title: Flask WTForms subforms and independent validating inside one common form
body: <p>I'm writing profile page and got some trouble with subforms in Flask.
  For example - I have one page with 2 submit buttons and fields like - password and some notes.</p>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@auth.route('/profile/&lt;user&gt;', methods=['GET','POST'])
  @login_required
  def profile(user):
      form=ProfileForm()
      if request.method('POST'):
          if form.profile_password.sub_pass.data and form.profile_password.validate(form):
              db_rw("update users set password=? where username=?",(generate_password_hash(form.profile_password.password1.data), user))
              flash('Password has been updated')
          elif form.profile_notes.sub_info.data and form.profile_avatar.validate(form):
              db_rw("update users set notes=? where username=?",(form.profile_notes.notes.data, user))
              flash('Notes has been updatedd')
      return render_template('auth/profile.html', user=user, form=form)
  </code></pre>
  
  <p><strong>forms.py</strong></p>
  
  <pre><code>class ProfilePasswordForm(FlaskForm):
      password1 = PasswordField('Password', validators=[DataRequired(),Length(min=3,max=20,message='The password should be at least 8 characters long')]) #!!!!
      password2 = PasswordField('Repeat Password', validators=[EqualTo('password1',message='The passwords are mismatched')])
      sub_pass = SubmitField('Change')
  
  class ProfileNotesForm(FlaskForm):
      notes = TextAreaField('Notes', validators=[Length(min=0, max=150)])
      sub_info = SubmitField('Save')
  
  class ProfileAvatarForm(FlaskForm):
      ava = FileField('Avatar')
      sub_pic = SubmitField('Upload')
  
  class ProfileForm(FlaskForm):
      profile_password=FormField(ProfilePasswordForm)
      profile_notes=FormField(ProfileNotesForm)
      profile_avatar=FormField(ProfileAvatarForm)
  </code></pre>
  
  <p><strong>profile.html</strong></p>
  
  <pre><code>{% block content %}
  
    &lt;h3&gt;Hello {{ user }} &lt;br&gt;
  
  
      &lt;form action="" method="post" enctype="multipart/form-data" novalidate&gt;
      {{ form.hidden_tag() }}
  
    &lt;div&gt;
      Change password: &lt;br&gt;
  
      {{ form.profile_password.password1 }}&lt;br&gt;
           {% for error in form.profile_password.password1.errors  %}
              &lt;span style="color: red;"&gt;[{{ error }}]&lt;/span&gt;
           {% endfor %}           
      {{ form.profile_password.password2 }}&lt;br&gt;
           {% for error in form.profile_password.password2.errors  %}
          &lt;span style="color: red;"&gt;[{{ error }}]&lt;/span&gt;
           {% endfor %}
      {{ form.profile_password.sub_pass() }}&lt;/p&gt;
    &lt;/div&gt;
  
    &lt;div&gt; 
      Notes: &lt;br&gt;     
      {{ form.profile_notes.notes }}
      {{ form.profile_notes.sub_info }}
    &lt;/div&gt;
  
      &lt;/form&gt;
  {% endblock %}
  </code></pre>
  
  <p>So the problem is that I want to have separate validators for each part of that big form. </p>
  
  <p>But this code </p>
  
  <pre><code>if form.profile_password.sub_pass.data and form.profile_password.validate(form)
  </code></pre>
  
  <p>doesn't return True, works only one part of this condition.</p>
  
  <p>If "Save" button is submitted only validators for Notes should be run and vise versa for button 'Change'.</p>
  
  <p>What I'm doing wrong?</p>
  
  <p>jfyi:</p>
  
  <p>I know that I can use multiply views to manage multiply forms, but I'm interested in subforms. </p>