def bootstrap_field(field: Field, form_group: bool = True, placeholder: Union[bool, str] = True, label: bool = True, errors: bool = True, horizontal: bool = False, inline: bool = False, btn_color: str = 'default', icon: Union[bool, str] = False, **kwargs) -> Markup: """Render a WTForms Field with Bootstrap markup. Args: field: The field to render. form_group: Whether or not to make it a form-group. placeholder: The string to display as a placeholder, True to display the default placeholder and something falsey to display nothing. label: Whether or not to display the field label. errors: Whether or not to display errors. horizontal: Whether to display the field horizontally. icon: inline: Whether to display the field inline. btn_color: The color name to pass as the button color. """ # TODO respect inline if isinstance(field, HiddenField): # Handles CSRFTokenFields return Markup(str(field)) is_text_field = field.type in TEXT_FIELD_TYPES field_classes = _space_join_cond({ 'form-control': is_text_field, 'btn btn-' + btn_color: field.type == 'SubmitField', }) if placeholder is True and field.type in TEXT_FIELD_TYPES: placeholder = 'Enter %s' % field.label.text html_str: Markup = field(class_=field_classes, placeholder=placeholder, **kwargs) html_str = _insert_icon(html_str, field, icon, is_text_field) horizontal_field_offset = 2 label_html = '' if horizontal and field.errors: html_str += ERROR_ICON if field.type == 'BooleanField': html_str = Markup( f'<div class="checkbox"><label>{html_str} {field.label.text}' '</label></div>') elif field.type != 'SubmitField' and label: label_html = field.label(class_=_space_join_cond({ 'control-label': True, 'col-sm-2': horizontal })) horizontal_field_offset = 0 if horizontal: html_str = label_html + _col( html_str, 10, offset=horizontal_field_offset) else: html_str = label_html + html_str if form_group and field.errors: html_str += ERROR_ICON if form_group: form_group_classes = _space_join_cond({ 'form-group': True, 'has-error has-feedback': field.errors, 'required': field.flags.required, }) html_str = Markup( f'<div class="{form_group_classes}">{html_str}</div>') html_str += _field_description(field, horizontal) html_str += _field_errors(field, errors, horizontal) return Markup(html_str)
def bootstrap_field( field: Field, form_group: bool = True, placeholder: Union[bool, str] = True, label: bool = True, errors: bool = True, horizontal: bool = False, inline: bool = False, btn_color: str = 'default', icon: Union[bool, str] = False, **kwargs ) -> Markup: """Render a WTForms Field with Bootstrap markup. Args: field: The field to render. form_group: Whether or not to make it a form-group. placeholder: The string to display as a placeholder, True to display the default placeholder and something falsey to display nothing. label: Whether or not to display the field label. errors: Whether or not to display errors. horizontal: Whether to display the field horizontally. icon: inline: Whether to display the field inline. btn_color: The color name to pass as the button color. """ # TODO respect inline if isinstance(field, HiddenField): # Handles CSRFTokenFields return Markup(str(field)) is_text_field = field.type in TEXT_FIELD_TYPES field_classes = _space_join_cond({ 'form-control': is_text_field, 'btn btn-' + btn_color: field.type == 'SubmitField', }) if placeholder is True and field.type in TEXT_FIELD_TYPES: placeholder = 'Enter %s' % field.label.text html_str = field( class_=field_classes, placeholder=placeholder, **kwargs ) html_str = _insert_icon(html_str, field, icon, is_text_field) horizontal_field_offset = 2 label_html = '' if horizontal and field.errors: html_str += ERROR_ICON if field.type == 'BooleanField': html_str = ( f'<div class="checkbox"><label>{html_str} {field.label.text}' '</label></div>' ) elif field.type != 'SubmitField' and label: label_html = field.label(class_=_space_join_cond({ 'control-label': True, 'col-sm-2': horizontal })) horizontal_field_offset = 0 if horizontal: html_str = label_html + _col(html_str, 10, offset=horizontal_field_offset) else: html_str = label_html + html_str if form_group and field.errors: html_str += ERROR_ICON if form_group: form_group_classes = _space_join_cond({ 'form-group': True, 'has-error has-feedback': field.errors, 'required': field.flags.required, }) html_str = f'<div class="{form_group_classes}">{html_str}</div>' html_str += _field_description(field, horizontal) html_str += _field_errors(field, errors, horizontal) return Markup(html_str)