feincms / form-designer

A simple form designer for Django
https://form-designer.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
73 stars 32 forks source link

Select field accepts only english words #43

Open Warkinstar opened 4 days ago

Warkinstar commented 4 days ago

Hello everyone, I encountered a problem that the form is rendered without select values ​​that do not use English letters.

class TestPageView(View):

    def dispatch(self, request, *args, **kwargs):
        from form_designer.models import Form

        self.instance = Form.objects.get(pk=1)
        self.form = self.instance.form_class()

        return super().dispatch(request, *args, **kwargs)

    def get(self, request, *args, **kwargs):

        template_name = "pages/test.html"
        extra_context = {"form": self.form}
        return render(request, template_name, extra_context)
FormField.objects.get(pk=7).choices  
'Men,Woman,Мужчина,Женщина'

<select name="pol" class="select form-select is-invalid" aria-invalid="true" id="id_pol"> 
    <option value="men">Men</option> 
    <option value="woman">Woman</option> 
    <option value="" selected="">Мужчина</option> 
    <option value="">Женщина</option>
</select>
matthiask commented 4 days ago

Hey

yes, slugify removes all no-ascii characters it seems. Here's the relevant issue for that: https://code.djangoproject.com/ticket/8391

I'm not sure but maybe we could stop slugifying options altogether? @mjl Do you have any opinions on this?

Warkinstar commented 4 days ago

@matthiask Yes, I removed slugify and everything worked.

    def get_choices(self):
        def get_tuple(value):
            # return (slugify(value.strip()), value.strip())  # Original
            return (value.strip(), value.strip())
mjl commented 4 days ago

I'm not sure but maybe we could stop slugifying options altogether? @mjl Do you have any opinions on this?

No strong opinion. I personally like my option values ascii-able because I grew up in a pre-Unicode era :-), but nowadays and in an automated setting like with form-designer it does not really matter.

Do we need to be careful about quotes or other nasties?

I'm fine with removing the slugifying.

On a tangent: It would be nice to be able to somehow explicitely specify the option values in case some automated processing is run on the form entries later; I have several cases of having options that read "yes-i-have-read-the-terms-and-conditions-and-promise-to-be-a-good-boy-while-waiting-for-santa" which is 1) ridiculous, and 2) need to keep the option value in the external program in sync with the text.

Perhaps something like specifying "male=A Man,female=A Woman,droid=Some EVIL Robot" as option definition would do the trick? But again, that's an enhancement and not directly related with the problem at hand.

matthiask commented 3 days ago

Do we need to be careful about quotes or other nasties?

I don't think we have to. The HTML escaping which is automatically applied to values should be sufficient. TBH I don't really remember why I added the slugification (is that even a word?) in the first place.

On a tangent: It would be nice to be able to somehow explicitely specify the option values in case some automated processing is run on the form entries later; I have several cases of having options that read "yes-i-have-read-the-terms-and-conditions-and-promise-to-be-a-good-boy-while-waiting-for-santa" which is 1) ridiculous, and 2) need to keep the option value in the external program in sync with the text.

Yes, that's an excellent point. By the way, I recently changed the code to at least try to return the unslugified variant here https://github.com/feincms/form-designer/commit/f59ed76ec321df086a447c4d86bf51fd47737955 but storing the value as-is would have made that obsolete. At least if there weren't any concerns about preserving already submitted data.