django-multiselectfield icon indicating copy to clipboard operation
django-multiselectfield copied to clipboard

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

Open alpden550 opened this issue 2 years ago • 6 comments

Hello, added compatible with Django > 4

alpden550 avatar Dec 01 '22 16:12 alpden550

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.

clhVEGAS avatar Jan 08 '23 15:01 clhVEGAS

why this PR is not approved yet??

humanscape-chan avatar Jun 14 '23 12:06 humanscape-chan

why this PR is not approved yet??

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

alpden550 avatar Jun 14 '23 12:06 alpden550

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)

pe712 avatar Jul 21 '23 12:07 pe712

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> ]

alpden550 avatar Aug 10 '23 10:08 alpden550

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

pe712 avatar Aug 24 '23 21:08 pe712

Fixed in #148.

blag avatar May 24 '24 19:05 blag