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: alasd...@memset.com
 web: http://www.memset.com/

Memset Ltd., registration number 4504980. 25 Frederick Sanger Road, Guildford, 
Surrey, GU2 7YD, UK.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to