Skip to content

Avoid validating dynamic field #3

Closed

Description

@mschoettle

Thanks for this package and especially for the HTMX and Unpoly examples. It helped me realize that Unpoly might be a better fit (in my case) given the up-validate mechanism without needing a separate view.

When this is done, though, selecting a different make in the example causes the form to be validated. I.e., switching from "Audi" to "Toyota" causes the validation error "Select a valid choice. a1 is not one of the available choices.".

Is there any way to avoid that?

One way I thought about it, seeing the example in the official docs for [up-validate], is to handle it differently when the X-Up-Validate header is present:

if 'X-Up-Validate' in request.headers:
    form = MakeAndModelForm(initial=request.POST)

But there might be a better way to achieve this.

Activity

  1. j4mie commented on Apr 20, 2023

    @j4mie
    Member

    Good catch, I've done something very similar in the past:

    def is_validating(request):
        return "X-Up-Validate" in request.headers

    Then in your form, something like this:

    class MakeAndModelForm(DynamicFormMixin, forms.Form):
         ...
        model = DynamicField(
            forms.ChoiceField,
            choices=lambda form: form.MODEL_CHOICES[form["make"].value()],
            required=lambda form: not is_validating(form.context["request"]),
        )
  2. mschoettle commented on Apr 21, 2023

    @mschoettle
    Author

    Thanks! So there are different ways to achieve it depending on the use case. I wasn't sure if going the initial route is the appropriate way.

  3. locked and limited conversation to collaborators on Jul 21, 2023
  4. converted this issue into a discussion #8 on Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions