django activa y desactiva la validación a través de una lista desplegable

I am a newbie to django, and this issue has me stumped. I cannot see the error, looked for many hours now!

I am trying to turn off or turn on the validation of two text fields based on the value of a drop down list. If the user selects index values 8888 or 9999 the validation is turned off - this appear to be working (woo hoo!!).

When the user selects another value (not 8888 or 9999) the validation is turned on, but remains on even when I have filled the relevant text fields. The form will not submit even when the text fields are filled because the validation is still triggered.

Here is the code in my forms.py page:

    def clean(self):

    cd_wrtdf = super(WorkplaceRelatedTrainingDetailsForm, self).clean()

    if cd_wrtdf['workplace_training_display_type'] == 8888 or cd_wrtdf['workplace_training_display_type'] == 9999:

        self.cleaned_data['workplace_training_institute'] = ''
        self.cleaned_data['workplace_training_date_completed'] = ''
        self.cleaned_data['workplace_training_date_expiry'] = ''

    else:

        if cd_wrtdf['workplace_training_display_type'] == 0:

            self._errors['workplace_training_institute'] = self.error_class([_("This field is required.xxxxxxxxx")])
            del self.cleaned_data['workplace_training_institute']

            self._errors['workplace_training_date_completed'] = self.error_class([_("This field is required.yyyyyyyy")])
            del self.cleaned_data['workplace_training_date_completed']

    return cd_wrtdf

Cualquier sugerencia o muestra de código sería muy apreciada.

Gracias.

preguntado el 27 de noviembre de 13 a las 06:11

I've never heard "ddl" used as an abbreviation for "drop down list". Usually it means Data Definition Language, which makes the title of this post rather confusing. -

I did not know that. Have amended the title. -

1 Respuestas

Finally, got the validation working!

Here is the code (hope this helps someone):

    def clean(self):

    cd_wrtdf = super(WorkplaceRelatedTrainingDetailsForm, self).clean()

    if cd_wrtdf['workplace_training_display_type'] == 8888 or cd_wrtdf['workplace_training_display_type'] == 9999:

        self.cleaned_data['workplace_training_institute'] = ''
        self.cleaned_data['workplace_training_date_completed'] = ''
        self.cleaned_data['workplace_training_date_expiry'] = ''

    else:

        if 'workplace_training_institute' in cd_wrtdf and len(cd_wrtdf['workplace_training_institute'].strip()) == 0:

            self._errors['workplace_training_institute'] = self.error_class([_("This field is required.xxxxxxxxx")])
            del self.cleaned_data['workplace_training_institute']

        if 'workplace_training_date_completed' in cd_wrtdf and len(cd_wrtdf['workplace_training_date_completed'].strip()) == 0:

            self._errors['workplace_training_date_completed'] = self.error_class([_("This field is required.yyyyyyyy")])
            del self.cleaned_data['workplace_training_date_completed']

    return cd_wrtdf

respondido 27 nov., 13:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.