Skip to content

Select Fields should support disabled option out of the box #928

@propstat-dev

Description

@propstat-dev

While implementing multiple log-in systems I noticed that there's no way to implement disabled select options without significant effort on routes and templating.

Would a solution inside flask-wtf be appreciated if I make a PR and what would be the desired approach?

Below the solution I took.

Frontend

Image

Required Code

# forms.py
# Admin Log-in form for admin interface
class AdminLoginForm(FlaskForm):
    admin_login_username = StringField('Admin User', validators=[DataRequired()])
    admin_login_password = PasswordField('Admin Password', validators=[DataRequired()])
    admin_login_rememberme = BooleanField('Remember Me')
    # Currently only PAM, for future use when we add LDAP/AD support can be enabled to selectable realms
    admin_login_realm_options = [
        ("pam", "Linux PAM Standard Authentication"),
        ("ldap", "LDAP"),
        ("ad", "Active Directory"),
    ]
    admin_login_realms_enabled = {"pam"}  # only PAM is allowed for now, but this can be easily extended in the future

    admin_login_realm = SelectField(
        "Authentication Realms",
        choices=admin_login_realm_options,
        validators=[DataRequired()]
    )

    admin_login_submit = SubmitField('Sign In')

    def validate_admin_realm(self, field):
        if field.data not in self.admin_login_realms_enabled:
            raise ValidationError("Selected authentication method is not enabled.")
# routes.py
@ssldeploy.route('/admin/', methods=['GET', 'POST'])
def admin():
    brand = {'brandname' : 'companyname'}
    form = AdminLoginForm()
    if form.validate_on_submit():
        flash('Login requested for user {}, remember_me={}'.format(
            form.admin_login_username.data, form.admin_login_rememberme.data))
        return redirect('/')
    return render_template('admin/adminlogin.html', title='SSL Deploy Admin Interface', brand=brand, form=form)
<!-- Select Form Field Start -->
                    <select id="{{ form.admin_login_realm.id }}" name="{{ form.admin_login_realm.name }}" class="loginformfieldrealm">
                    {% for value, label in form.admin_login_realm.choices %}
                    {% set disabled = value not in form.admin_login_realms_enabled %}
                        <option value="{{ value }}"
                        {% if form.admin_login_realm.data == value %}selected{% endif %}
                        {% if disabled %}disabled class="text-gray-400"{% endif %}>
                        {{ label }}{% if disabled %} (disabled){% endif %}
                        </option>
                    {% endfor %}
                    </select>
<!-- Select Form Field END -->

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions