Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

French local : Use class Validators for SIRET and SIREN model fields, do not format output #473

Open
LLyaudet opened this issue Aug 22, 2022 · 2 comments

Comments

@LLyaudet
Copy link

LLyaudet commented Aug 22, 2022

Hello,

Right now the SIRET and SIREN model fields use SIRET and SIREN form fields that do the validation.
I prefer the architecture of the IBAN and BIC fields in generic that use class Validators,
since you can call the validators without using form fields.
Would you accept if I submit a PR with a new file validators.py in fr/
and add in it SIRENValidator and SIRETValidator classes ?
I would use them as is done for IBANValidator and BICValidator.

There is also an annoying thing :
SIRET is max length 14
but it is formatted with spaces.
Thus if in Django admin you modify a form with a valid SIRET in it you get this error :
"Ensure this value has at most 14 characters (it has 17)."
image
I don't know what is the nicest way to avoid this :

  • remove formatting in
    def prepare_value(self, value):
    if value is None:
    return value
    value = value.replace(' ', '').replace('-', '')
    return ' '.join((value[:3], value[3:6], value[6:9], value[9:]))
    ?
  • change maxlength from 14 to 17 ?
    Modifying a SIRET each time you submit a form because of this is an unvolontary "dark pattern" ;)

Thanks, best regards,
Laurent Lyaudet

@LLyaudet
Copy link
Author

For the annoying formatting, I have found a workaround :
https://stackoverflow.com/questions/61896109/changing-max-length-on-a-charfield-for-a-modelform

class MyModelForm(forms.ModelForm):
    siret = FRSIRETField(max_length=17)

@LLyaudet
Copy link
Author

LLyaudet commented Aug 23, 2022

If you want to keep some arguments from the model, the workaround is more complicated (and ugly) :

class MyModelForm(forms.ModelForm):
    # siret = FRSIRETField(max_length=17) will not work with blank=True

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["siret"].max_length = 17
        for i, validator in enumerate(self.fields["siret"].validators):
            if isinstance(validator, MaxLengthValidator):
                self.fields["siret"].validators[i] = MaxLengthValidator(17)
                break
        self.fields["siret"].widget.attrs["maxlength"] = 17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant