Hi Bruno,On Jan 11, 12:15 pm, galgal <weglarek.rob...@gmail.com> wrote:Is there any way, to change the field's *min_length* argument inside form constructor? That doesn't work: def __init__(self, *args, **kwargs): super(CreateTeamForm, self).__init__(*args, **kwargs) self.fields['primary_color'].min_length = 4Assuming primary_color is a CharField, this should theoretically JustWork AFAICT. What is the result ? ("doesn't work" is not really descriptive...)
I answered this question on Stack Overflow earlier this week [1].
When a field with min_length is initialised, a MinLengthValidator is added to the its validators. So if you set the min_length attribute after calling parent classes __init__ method, it is 'too late' for it to have any effect.
The solution is to change the field's validators in the __init__ method instead,
Cheers,
Alasdair
[1]: http://stackoverflow.com/questions/8818261/django-change-min-length-in-form-init/
-- Alasdair Nicol Developer, MEMSET mail: alasdair@memset.com web: http://www.memset.com/ Memset Ltd., registration number 4504980. 25 Frederick Sanger Road, Guildford, Surrey, GU2 7YD, UK.
0 comments:
Post a Comment