1

First of all, this question is not duplicated, because I looked everywhere on Google, GitHub and here and no one answered this.

I'm creating a CRUD for users of an app and part of the project is to give admins the power of altering users. Users have roles like Common Users or Admins.

When an admin clicks on the button to edit a user's info, a modal appears on the screen with the form.

What I want is to be able to mark an option as selected based on the user's info. If the user is an admin, the selected option that appears on modal opening will be by default admin. And if the user is a common user, I want to the role "Common User" to be selected instead, but for some reason, only Admin role appears selected.

Template:

<form action="" method="post" novalidate>
  {{ form.hidden_tag() }}
  <input type="hidden" name="user_id" value="{{ user.user_id }}">
  {{ form.username(placeholder=user.username) }}
  {{ form.full_name(placeholder=user.full_name) }}
  {{ form.e_mail(placeholder=user.e_mail) }}
  {{ form.role() }}
  {{ form.new_password }}
  {{ form.repeated_new_password }}
...

Form:

class AlterUserDataForm(FlaskForm):                                              

    user_id = IntegerField(                                                      
        'User ID')                                                         
    username = StringField(                                                      
        'Username',                                                               
        validators=[Optional()])                                                 
    full_name = StringField(                                                     
        'Full name',                                                         
        validators=[Optional()])                                                 
    email = StringField(                                                        
        'Email',                                                                
        validators=[Optional()])                                                 
    role = SelectField(                                                          
        'Role',                                                       
        choices=[                                                                
            (1, 'Admin'),                                                        
            (2, 'User')],                                                     
        validators=[Optional()]                                                  
        )                                                                        
    new_password = StringField(                                                  
        'New password',                                                            
        render_kw={'placeholder': 'Type the new password'},                        
        validators=[Optional()])                                                 
    repeated_new_password = StringField(                                         
        'Repeat new password',                                                   
        render_kw={'placeholder': 'Repeat new password'},                        
        validators=[Optional(), EqualTo('new_password')])                        
    submit = SubmitField('Change')

I tried putting default value on form.role, but it didn't work as I expected.

Can anyone help?

Versions:

  • Flask: 1.0.3
  • Flask-WTForms: 0.14.2
sergiomafra
  • 1,174
  • 2
  • 13
  • 21
  • This may be useful: https://stackoverflow.com/a/17019796/4379026 – mattsap Jul 29 '19 at 19:41
  • Hey, matt, thanks for your comment, but I've already seen this post. What I need is put the selected property on an option. I don't know if default does this, because I tried and nothing happened... – sergiomafra Jul 29 '19 at 19:54
  • I mean, the idea would be to, when knowing whether the user is admin or not, set the data attribute to user/admin after populating the form. So, you don't want to set the default value, but rather, have some event that triggers to set the actual value of the form. – mattsap Jul 29 '19 at 20:30
  • That's it! On the template, I cycle through all users generating its row on the user's table and generating a form on a modal for everyone. I know that this sounds dumb, but this is just a POC and I'm not a Javascript developer to solve it other way. It supposed to work.. I guess... Haha – sergiomafra Jul 29 '19 at 20:36

0 Answers0