def init_helper(self): # Put required HTML attribute on required fields so they are managed by Abide (if enabled) if "data_abide" in self.attrs: for field in self.fields.values(): if field.required: field.widget.attrs["required"] = "" field.abide_msg = "This field is required." if not self.layout: # Start with an empty layout self.helper = FormHelper(self) else: # Start from the given layout self.helper = FormHelper() self.helper.layout = deepcopy(self.layout) # Try to reverse form_action url, else fallback to use it as a simple string try: self.helper.form_action = reverse(self.action) except NoReverseMatch: self.helper.form_action = self.action if self.title: self.helper.layout.insert( 0, HTML(self.title_templatestring.format(self.title))) if self.form_id is not None: self.helper.form_id = self.form_id self.helper.form_class = self.classes self.helper.form_method = self.method self.helper.form_error_title = self.error_title self.helper.attrs = self.attrs if self.switches: # Get a list of all fields with their location within the layout layout_field_names = self.helper.layout.get_field_names() # Transform checkbox fields to switches element for pointer in layout_field_names: if isinstance(self.fields[pointer[1]].widget, forms.CheckboxInput): self.replace_layout_object( pointer[0], InlineSwitchField(pointer[1], switch_class="inline")) if self.submit: if isinstance(self.submit, Submit): self.helper.add_input(self.submit) elif isinstance(self.submit, str): self.helper.add_input(Submit('submit', self.submit)) else: self.helper.add_input(Submit('submit', "Submit"))
def part_2_crispies(): return [ Row(Column('select_input', css_class='large-12'), ), Row( Column('radio_input', css_class='large-4'), Column('checkbox_input', css_class='large-4'), Column(Row( Column(SwitchField('checkbox_switch_input_1', switch_class="round tiny"), css_class='small-3'), Column(HTML('<label>Checkbox with a switch field</label>'), css_class='small-9'), ), Row(Column( InlineSwitchField('checkbox_switch_input_2'), ), ), css_class='large-4'), ), ]
def test_inlineswitchfield(output_test_path, render_output, rendered_template, helper, client): form = BoolInputForm() pack = helper.template_pack helper.layout = Layout( InlineSwitchField('opt_in', label_column='large-8', input_column='large-4', label_class='foobar', switch_class='inline') ) rendered = rendered_template(form, helper=helper) attempted = render_output(os.path.join(output_test_path, pack, 'test_inlineswitchfield.html')) #write_output(output_test_path, pack, "test_inlineswitchfield.html", rendered) assert parse_html(attempted) == parse_html(rendered)
def test_inlineswitchfield(output_test_path, rendered_template, helper, client): form = BoolInputForm() pack = helper.template_pack helper.layout = Layout( InlineSwitchField('opt_in', label_column='large-8', input_column='large-4', label_class='foobar', switch_class="inline")) rendered = rendered_template(form, helper=helper) attempted = read_output(output_test_path, pack, "test_inlineswitchfield.html") #write_output(output_test_path, pack, "test_inlineswitchfield.html", rendered) assert rendered == attempted