# Accessing \`widget\` attribute within validators.

**URL:** <https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729>\
**Category:** Using Django\
**Created:** [July 11, 2021, 5:39am UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729 "2021-07-11T05:39:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ultra-Instinct-05](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ultra-instinct-05/32/3267_2.png) [@Ultra-Instinct-05](https://forum.djangoproject.com/u/Ultra-Instinct-05)\
**Post date:** [July 11, 2021, 5:39am UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729/1 "2021-07-11T05:39:08Z")

</div>

I have some example pseudo code for a form here:

```python
class ExampleForm(forms.Form):

    field_1 = forms.FloatField(
        label = "Field 1",
        widget = forms.NumberInput(attrs = {
            "class": "form-control" # This is just a bootstrap class.
        }),
        validators = [] # Some lambda function to a validator. 
    )

```

I populate the `validators` with a lambda function that validates the form input.  
What I would like to know is, Is there a way I can access the `widget` property of the field in the validator function ? What my main goal is if the validation fails, I can add an `is-invalid` class (again a bootstrap class) to the `widget.attrs` to show which inputs have failed.

I can of course do this inside each fields `clean_` method `django` provides but in case of a lot of fields, that just reeks of DRY violation.

Any help would be appreciated ! 🙂  
Have a good day !

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 11, 2021, 1:15pm UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729/2 "2021-07-11T13:15:02Z")

</div>

> [@Ultra-Instinct-05](#):
>
> I can of course do this inside each fields `clean_` method `django` provides but in case of a lot of fields, that just reeks of DRY violation.

You can do it within the `clean()` method (the global clean, not the per-field clean) by iterating over the ValidationErrors and adjusting the fields accordingly.

---

<div class="post-metadata">

**Author:** ![Ultra-Instinct-05](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ultra-instinct-05/32/3267_2.png) [@Ultra-Instinct-05](https://forum.djangoproject.com/u/Ultra-Instinct-05)\
**Post date:** [July 11, 2021, 1:41pm UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729/3 "2021-07-11T13:41:41Z")

</div>

> [@KenWhitesell](#):
>
> iterating over the ValidationErrors

Could you provide a sample example ? I have looked in [Form and field validation | Django documentation | Django](https://docs.djangoproject.com/en/3.2/ref/forms/validation/) but I don’t seem to find anything similar.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 11, 2021, 2:03pm UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729/4 "2021-07-11T14:03:48Z")

</div>

See the docs in the Forms API page for [Form.errors](https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.errors) along with the [`.as_data`](https://docs.djangoproject.com/en/3.2/ref/forms/api/#django.forms.Form.errors.as_data) method.

---

<div class="post-metadata">

**Author:** ![Ultra-Instinct-05](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ultra-instinct-05/32/3267_2.png) [@Ultra-Instinct-05](https://forum.djangoproject.com/u/Ultra-Instinct-05)\
**Post date:** [July 11, 2021, 2:08pm UTC](https://forum.djangoproject.com/t/accessing-widget-attribute-within-validators/8729/5 "2021-07-11T14:08:49Z")

</div>

Thanks for the help. Got it. The fields are available in `self.error.items()` and I can use that.
