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

fix(fields): append validator instead of pushing it with index error #136

Closed
wants to merge 2 commits into from

Conversation

alpden550
Copy link

Hello,
added compatible with Django > 4

@clhVEGAS
Copy link

clhVEGAS commented Jan 8, 2023

Hi, I also came to the same conclusion with this error. Don't see a reason why that value would need to be added exclusively at the zero index.

@humanscape-chan
Copy link

why this PR is not approved yet??

@alpden550
Copy link
Author

why this PR is not approved yet??

Hi, I will use for my projects fixed
https://github.com/alpden550/django-multiselectfield as temporary

@pe712
Copy link

pe712 commented Jul 21, 2023

Related to #134

I think your code does not cover completly the need. If max_length is set, then the MaxLengthValidator is added twice. One time by CharField init and one time by MultiSelectField init.

It is not too much of a worry to double check but not very useful either.

I would rather do this:

if self.max_length is None:
            self.max_length = get_max_length(self.choices, self.max_length)
            self.validators.append(MaxValueMultiFieldValidator(self.max_length))
else:
            self.validators[0] = MaxValueMultiFieldValidator(self.max_length)

@alpden550
Copy link
Author

Related to #134

I think your code does not cover completly the need. If max_length is set, then the MaxLengthValidator is added twice. One time by CharField init and one time by MultiSelectField init.

It is not too much of a worry to double check but not very useful either.

I would rather do this:

if self.max_length is None:
            self.max_length = get_max_length(self.choices, self.max_length)
            self.validators.append(MaxValueMultiFieldValidator(self.max_length))
else:
            self.validators[0] = MaxValueMultiFieldValidator(self.max_length)

Dajngo 4 all ok, nothing doubled:

[ <django.core.validators.MaxLengthValidator object at 0xffffb4d450f0>, <multiselectfield.validators.MaxValueMultiFieldValidator object at 0xffffb4d44f70> ]

@pe712
Copy link

pe712 commented Aug 24, 2023

Although, we can wonder why there is a max_length parameter at all. The max_length should be inferred from the choices as get_max_length does. Indeed if we set the max_length to 3 for example but our choices is MY_CHOICES2 = ((1, 'Item title 1'), (2, 'Item title 2'), (3, 'Item title 3')), the necessary length is 5. It means that we cannot take all the choices together.

Admitting that this feature makes sense, you are checking twice : MaxValueMultiFieldValidator is a MaxLengthValidator !!

class MaxValueMultiFieldValidator(validators.MaxLengthValidator):
    code = 'max_multifield_value'

    def clean(self, x):
        return len(','.join(x))

And you can run

for validator in my_field.validators:
    print(validator.limit_value)

To see that they are duplicates.

See other PR

@blag
Copy link
Collaborator

blag commented May 24, 2024

Fixed in #148.

@blag blag closed this May 24, 2024
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

Successfully merging this pull request may close these issues.

5 participants