6

I am trying to change the required of a form field to 'False' depending on the input of another field.

form:

class MyNewForm(forms.Form):

def __init__(self, *args, **kwargs):
    self.users = kwargs.pop('users', None)
    super(MyNewForm, self).__init__(*args, **kwargs)


    self.fields['name'] = forms.CharField(
        max_length=60,
        required=True,
        label="Name *:",
        widget=forms.TextInput(
            attrs={'class' : 'form-control', 'placeholder': 'Name'})
    )

def clean_this_other_field(self):

    # change required field
    name = self.fields['name']
    print name.required
    name.required=False
    print name.required

results of print:

True

False

So the field required is changing to 'False' but when the form page reloads it reverts back to 'True'

Any suggestions?

EDIT - Problem solved

For anyone landing on this with a similar issue:

def clean_remove(self):
    cleaned_data = super(MyNewForm, self).clean()
    remove = cleaned_data.get('remove', None)
    if remove:
        self.fields['name'].required=False

Remove is the first field in the form so adding this clean on the remove field resolves the issue.

drew
  • 771
  • 1
  • 4
  • 11
  • You probably need to id both fields in your template and update the `required` attribute for the dependent using Javascript. The dependent field can be set to be non-required or a validation enforced server-side for it. – Oluwafemi Sule Sep 07 '17 at 11:13
  • I can't understand why you think this wouldn't happen. Of course a reload causes the field to revert to True; your `__init__` sets it to True, so it *must* always do that. What else did you expect? – Daniel Roseman Sep 07 '17 at 17:43
  • I didn't say that I didn't expect that to happen, I asked if there were any suggestions to stop this happening. – drew Sep 08 '17 at 10:53
  • Use **form = GroupForm(use_required_attribute=False)** when you initialize your form in your views.py. – Lord-shiv Jul 19 '21 at 16:12

1 Answers1

11

Can you give more of your code when this problem appers?

If u use ModelForm you can do something like:

class YourForm(forms.ModelForm):    
    def __init__(self, *args, **kwargs):
        super(YourForm, self).__init__(*args, **kwargs)
        self.fields['name'].required = True

    class Meta:
        model = YouModel
        fields = (...)

Django Model Forms - Setting a required field

U can add required using widget too:

email = forms.EmailField(
    max_length=100,
    required=True,
    widget=forms.TextInput(attrs={ 'required': 'true' }),
)
Thaian
  • 1,215
  • 2
  • 20
  • 30
  • I've added some more of my form – drew Sep 07 '17 at 11:15
  • I am using the form's own required attribute but the problem is changing it after submission and depending on the other field, keeping it that way – drew Sep 07 '17 at 11:21
  • Try add required to widget. I added it to my answer. – Thaian Sep 07 '17 at 11:22
  • 1
    I have just tried using a widget as you suggest but this does not allow the form to submit therefore it can't get to the clean function to remove the required attribute. – drew Sep 07 '17 at 11:26