pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.73k stars 1.57k forks source link

Allowing custom choices in form_choices? #1519

Open vincentwhales opened 7 years ago

vincentwhales commented 7 years ago

I really like using form_choices to guide my user into inputting into the create form.

However, it's limiting in the sense that my users can't use anything else not defined in form_choices.

Is there a way to have form_choices with an extra option, i.e. "Enter your own" so that users can input custom choices?

(I know form_choices is intended to limit what a user can enter. However, another use case will be to allow some flexibility in allowing our users enter a custom option)

ljluestc commented 2 months ago

from flask import Flask, render_template, request

app = Flask(__name__)

# Sample choices
choices = ['Option 1', 'Option 2', 'Option 3']

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html', choices=choices)

@app.route('/process_form', methods=['POST'])
def process_form():
    selected_choice = request.form['choice']
    if selected_choice == 'custom':
        custom_input = request.form['customInput']
        # Handle custom input logic here
        return f"Custom input received: {custom_input}"
    else:
        # Handle selected choice logic here
        return f"Selected choice: {selected_choice}"

if __name__ == '__main__':
    app.run(debug=True)