Your Ad Here

Friday, January 13, 2012

Re: change min_length in form __init__?

On 13/01/12 10:47, bruno desthuilliers wrote:
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 = 4 
 Assuming primary_color is a CharField, this should theoretically JustWork AFAICT. What is the result ? ("doesn't work" is not really descriptive...)  
Hi Bruno,

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