Question

I'm trying to make some of my fields in my crispy form readonly. I would have thought it would be simple, but it's not working. No errors, just not readonly.

class ApplicationForm(BaseItemForm):

    jira_bucket = forms.CharField(
        widget=autocomplete_light.ChoiceWidget('JiraAutocomplete'))

    def __init__(self, *args, **kwargs):
        ''' Change form based on permissions of the user '''
        super(ApplicationForm, self).__init__(*args, **kwargs)
        # app = self.instance

        if not self.user.has_perm('Assets.sarbox_fields'):

            disabled_widget = forms.CheckboxInput(attrs={'readonly': True})
            self.fields['sarbox'].widget = disabled_widget
            self.fields['pii'].widget = disabled_widget
            self.fields['pci'].widget = disabled_widget
            if 'sarbox_approver' in self.fields:
                self.fields['sarbox_approver'].widget.attrs['disabled'] = True
        # it's about to get crispy
        self.helper = FormHelper()
        # self.helper.form_class = 'application-form'  # done in template
        self.helper.form_tag = False
        self.helper.disable_csrf = True
        self.helper.layout = Layout(
            Div(
                Field('name'),
                Field('jira_bucket'),
            ),
            Row(Field('primary_tech',
                      wrapper_class='col-md-6'),
                Field('primary_biz',
                      wrapper_class='col-md-6')
                ),
            Row(Field('backup_tech',
                      wrapper_class='col-md-6'),
                Field('backup_biz',
                      wrapper_class='col-md-6'),
                ),
            Field('desc', rows=2),
            Div(
                Field('sarbox'),
                Field('pci'),
                Field('pii'),
                css_class='form-inline',
                ),
            Row(
                Field('sarbox_tech_approvers',
                      wrapper_class='col-md-6'),
                Field('sarbox_biz_approvers',
                      wrapper_class='col-md-6'),
                css_id='sarbox_approver'
                ),
            )

    def clean(self):
        cleaned_data = self.cleaned_data
        sarbox = cleaned_data.get('sarbox')
        if sarbox:
            if not (cleaned_data['sarbox_tech_approvers'] and
                    cleaned_data['sarbox_biz_approvers']):
                raise forms.ValidationError("Sarbox applications must have Sarbox Tech and Business approvers assigned.")
        return cleaned_data
Was it helpful?

Solution 2

Make the disabled/readonly widget: disabled_widget = forms.CheckboxInput(attrs={'disabled': True})

OTHER TIPS

I had to set the readonly argument to True in the Layout, like so:

class ContactForm(forms.Form)
  email = forms.EmailField(label='Email')
  api_token = forms.CharField(label='API Token')

  def __init__(self, *args, **kwargs):
    super(ContactForm, self.__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.layout = Layout(
      'email',
      Field('api_token', readonly=True),
    )

should this:

attrs={'readonly': True}

not be this?

attrs={'readonly': 'readonly'}

For Columns, I had to add a read-only class

Row(
    Column('created', css_class='col-sm-4 read-only'),
),

Then select the input element using JS

$('.read-only .form-control').prop("disabled", true);

and toggle on submitting to get the fields to save properly

$('.read-only .form-control').prop("disabled", false);

I had some trouble dynamically specifying read-only, as CSS seems not to care what value you set readonly (it's mere presence making it readonly). I used:

weeks_read_only = {}
if read_only_condition_required:
weeks_read_only['readonly'] = ''

Field('weeks', css_class='numberinput text-center', **weeks_read_only)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top