def __init__(self, *fields, legend=None, legend_size=None, legend_tag=None, **kwargs): self.fields = list(fields) self.context = {} if legend: self.context["legend"] = legend if legend_size: self.context["legend_size"] = Size.for_legend(legend_size) if legend_tag: self.context["legend_tag"] = legend_tag if hasattr(self, "css_class") and "css_class" in kwargs: self.css_class += " %s" % kwargs.pop("css_class") if not hasattr(self, "css_class"): self.css_class = kwargs.pop("css_class", None) self.css_id = kwargs.pop("css_id", None) self.template = kwargs.pop("template", self.template) self.flat_attrs = flatatt(kwargs)
def checkboxes( cls, field, legend_size=None, legend_tag=None, small=False, **kwargs ): """ Create a field for displaying a set of checkboxes. Args: field (str): the name of the field. The field's label will be used as the title of the <legend>. legend_size (str): the size of the legend. The default is None in which case the legend will be rendered at the same size as regular text. legend_tag (str): Wrap the field legend with this HTML tag. Default is None. small (bool): Display small checkboxes. Default is False. **kwargs: Attributes to add to the <input> element when the field is rendered. """ context = {} if legend_size: context["legend_size"] = Size.for_legend(legend_size) if legend_tag: context["legend_tag"] = legend_tag context["checkboxes_small"] = small return Field(field, context=context, **kwargs)
def select(cls, field, legend_size=None, legend_tag=None, **kwargs): """ Create a field for displaying a select drop-down. Args: field (str): the name of the field. legend_size (str): the size of the legend. The default is None in which case the legend will be rendered at the same size as regular text. legend_tag (str): Wrap the field legend with this HTML tag. Default is None. **kwargs: Attributes to add to the <select> element when the field is rendered. """ context = {} if legend_size: context["legend_size"] = Size.for_legend(legend_size) if legend_tag: context["legend_tag"] = legend_tag return Field(field, context=context, **kwargs)
def test_change_legend_size(): """Verify size of the field legend can be changed from the default.""" form = CheckboxesForm() form.helper = FormHelper() form.helper.layout = Layout( Field("method", context={"legend_size": Size.for_legend("l")})) assert parse_form(form) == parse_contents(RESULT_DIR, "legend_size.html")
def render_layout(self, form, context, template_pack=TEMPLATE_PACK): """ Returns safe html of the rendering of the layout. :meta private: """ # Django will add a required attribute when a field is rendered, if is # required by the form. The browser validation interferes with the way # validation errors are reported so adding required attributes is # disabled at the form level. form.use_required_attribute = False if self.label_size: context["label_size"] = Size.for_label(self.label_size) if self.legend_size: context["legend_size"] = Size.for_legend(self.legend_size) return super().render_layout(form, context, template_pack=template_pack)
def radios( cls, field, legend_size=None, legend_tag=None, small=False, inline=False, **kwargs, ): """ Create a field for displaying radio buttons. Args: field (str): the name of the field. legend_size (str): the size of the legend. The default is None in which case the legend will be rendered at the same size as regular text. legend_tag (str): Wrap the field legend with this HTML tag. Default is None. small (bool): Display small radio buttons. Default is False. inline (bool): Display the radio buttons in a row. Default is False. **kwargs: Attributes to add to the <input> element when the field is rendered. """ context = {} if legend_size: context["legend_size"] = Size.for_legend(legend_size) if legend_tag: context["legend_tag"] = legend_tag context["radios_small"] = small context["radios_inline"] = inline return Field(field, context=context, **kwargs)