如何用deform 2制作水平表单?

1 投票
1 回答
669 浏览
提问于 2025-04-18 08:03

我正在使用deform 2和Bootstrap 3来制作一些表单。我想创建一个水平表单,像这里展示的那样,但在演示网站上看到的所有例子都不是水平表单(标签和输入框在同一行)。

我尝试使用form_class="form-horizontal"bootstrap_form_style="form-horizontal"deform_bootstrap)。使用这些设置确实给表单添加了form-horizontal这个类,但我不仅仅想给表单元素添加一个类。

我该如何将其他类名添加到表单的其余部分,以使其成为像Bootstrap网站上那样的水平表单呢?

1 个回答

3

我找到了办法,在我修改的模板里,我做了以下操作:

mapping_item.pt:

<div tal:define="error_class error_class|field.widget.error_class;
                 description description|field.description;
                 title title|field.title;
                 oid oid|field.oid;
                 hidden hidden|field.widget.hidden;
                 category category|field.widget.category;
                 structural hidden or category == 'structural';
                 required required|field.required;"
     class="form-group  ${field.error and 'has-error' or ''} ${field.widget.item_css_class or ''}"
     title="${description}"
     id="item-${oid}"
     tal:omit-tag="structural"
     i18n:domain="deform">
  <!-- <div class="col-sm-12"> -->


  <label for="${oid}"
         class="control-label col-sm-3 col-md-3 ${required and 'required' or ''}"
         tal:condition="not structural"
         id="req-${oid}"
         >
    ${title}
  </label>
<div class="col-sm-9 col-md-9">
  <div tal:define="input_prepend field.widget.input_prepend | None;
                   input_append field.widget.input_append  | None"
       tal:omit-tag="not (input_prepend or input_append)"
       class="input-group">
    <span class="input-group-addon"
          tal:condition="input_prepend">${input_prepend}</span
    ><span tal:replace="structure field.serialize(cstruct).strip()"
    /><span class="input-group-addon"
            tal:condition="input_append">${input_append}</span>
  </div>

  <p class="help-block"
     tal:define="errstr 'error-%s' % field.oid"
     tal:repeat="msg field.error.messages()"
     i18n:translate=""
     tal:attributes="id repeat.msg.index==0 and errstr or
     ('%s-%s' % (errstr, repeat.msg.index))"
     tal:condition="field.error and not field.widget.hidden and not field.typ.__class__.__name__=='Mapping'">
    ${msg}
  </p>

   <p tal:condition="field.description and not field.widget.hidden"
     class="help-block" >
    ${field.description}
  </p>

  </div>
</div>

注意到在label标签里,我加上了col-sm-3 col-md-3
另外,我还加了一个<div>,它的类是:class="col-sm-9 col-md-9"

这样做的效果是,让标签和输入框“横向排列”(也就是在同一行显示): 标签和输入框

form.pt文件中,我在表单标签里加了:

class="deform ${field.bootstrap_form_style | 'form-horizontal'}

我在定义表单布局时也这样做:

agent = colander.SchemaNode(
    colander.String(),
    validator=agent_validator,
    widget=widget.AutocompleteInputWidget(
        size=40,
        style="width: 300px",
        min_length=3,
        values=url,
        css_class="form-control"
        ),
    title="Agent*")

希望这些对你有帮助。

撰写回答