pythoncssformstwitter-bootstrapdeform

How to make a Horizontal form with deform 2?


I am using deform 2 together with Bootstrap 3, to render some forms, and I want to create a Horizontal form as shown here, but all of the examples on the demo-site, is not examples of horizontal forms( with the label next to the input element).

I have tried using form_class="form-horizontal", and bootstrap_form_style="form-horizontal"(deform_bootstrap). When using the above, it does add the class form-horizontal, however I dont just want to add a class to the form element.

How do I add the rest of the class names to the rest of the form to make it a horizontal form as is on the the bootstrap site?


Solution

  • I figured it out, in the deform templates(I over write them) I did:

    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>
    

    Notice in the label I added col-sm-3 col-md-3
    Also I added a <div> with a class: class="col-sm-9 col-md-9"

    This makes the item i.e. label + input 'horizontal'(displays in the same line): label and  input tag

    In form.pt, in the form tag I added:

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

    I also do when define the form layout:

    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*")
    

    I hope this is helpful.