0

Depending on the arguments I pass to the form I want to return different form fields.

class TestForm(FlaskForm):
    """Test Form."""

    if one:
        field1 = StringField('Field 1')
    if two:
        field2 = StringField("Field 2")
    if three:
        field3 = StringField("Field 3")
    submit = SubmitField("Add Service")

    def __init__(self, one=None, two=None, three=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.one = one
        self.two = two
        self.three = three

I am not able to see the arguments when doing the if statements.

I am aware of the option to have logic in html when rendering the form, however due the nature of the project have opted to use quick_form on the html side.

Here is the html code I am using.

{% import 'bootstrap/wtf.html' as wtf %}

<h3 >Add Service</h3>
<div class="row">
  <div class="col-md-4">
    {{ wtf.quick_form(form) }}
  </div>
</div>

2 Answers 2

0

Instead of creating this logic in your form class. I would recommend to create all the fields you need and then dynamically choose which to show the user using jinja2 in your html file. Here's an example.

{% for fields in fields_list %}
    {% if field == '1' %}
        {{ form.field1.label(class="form-control-label") }}
        {{ form.field1(class="form-control form-control-lg") }}
    {% endif %}
    {% if field == '2' %}
        {{ form.field2.label(class="form-control-label") }}
        {{ form.field2(class="form-control form-control-lg") }}
    {% endif %}
    {% if field == '3' %}
        {{ form.field3.label(class="form-control-label") }}
        {{ form.field3(class="form-control form-control-lg") }}
    {% endif %}
{% endfor %}

And then when you render or redirect to your .html from your routes code, don't forget to send the proper arguments, such as

# create your fields list, which do I want to show?
# Make it a list of integers

fields_list = [1, 2, 3]

return render_template('insert-name.html', fields_list , form=form)

If my answer helped you, please consider accepting my answer. I am new to this site and trying to build up some reputation :) Thank you! Happy Coding!

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Andrew, thank you for the suggestion. I am aware of this option however I am using quick form on the html side. I have multiple forms using a single html page so this would become too complex to manage as the project grows.
0

If someone ever comes accross the same question, here is a quick solution I came upon some times ago. It is in part inspired from the accepted answer here.

In your forms.py or wherever you declare your TestForm class, put the class declaration inside a function that takes your parameter as an argument and returns the class object as output. The argument will now be accessible within the class itself, allowing for any test you may want to perform.

Here's a working example based on your original question (I just added a default value to get at least one StringField in case the parameter is ommited):

def create_test_form(var='one'):
    class TestForm(FlaskForm):
        """Test Form."""

        if var == 'one':
            field1 = StringField('Field 1')
        if var == 'two':
            field2 = StringField("Field 2")
        if var == 'three':
            field3 = StringField("Field 3")

        submit = SubmitField("Add Service")

    return TestForm()

Then simply create the form in your routes like so:

form = create_test_form('two')

Finally pass it to your HTML to render the form with quick_form like you did.

This example will render a form with a single StringField named "Field 2" and a "Add Service" submit button.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.