View on GitLab

Fields

A x-larastrap::field usually wraps an input within a x-larastrap::form , and is responsible for the layout of the input itself and his label. Each input field provides to init his own wrapping x-larastrap::field (until the squeeze parameter is not set to true ), and there is no need to manually place it, but it is possible to create your own fields to wrap custom elements.

All of the following parameters can be passed to an Input element, to be applied to his implicit wrapping Field.

label

The base parameter for a field is his label , used as a description for the child content (which usually is an input box, but can be anything else).

<x-larastrap::field label="Name">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-b0737bf337ea4e463d811f675f401491" class="col-4 col-form-label">Name</label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

label_class

The label can be customized assigning one or more CSS classes, using the label_class parameter.

<x-larastrap::field label="Name" label_class="text-danger">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-3aebdf7b6c19a598603432dde434fc77" class="text-danger col-4 col-form-label">Name</label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

label_html

If you want to use a completely custom HTML label, you have to use the label_html parameter instead of label . Entities in plain label are escaped; when using label_html , those are not.

<x-larastrap::field label_html="<strong>Name</strong>">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-b7416c9cb9434e8c2034b3ef9a0527eb" class="col-4 col-form-label"><strong>Name</strong></label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

label_width / input_width

With the label_width and input_width parameters it is possible to define the width of each column, within the twelve columns system of Bootstrap. You can specify both an absolute value, or an array of breakpoints to obtain a responsive layout.



Here, the width of label's and input's columns vary accordingly to the responsive breakpoint.

<x-larastrap::field label="Name" label_width="6" input_width="6">
    <img src="https://picsum.photos/100">
</x-larastrap::field>

<br>

<x-larastrap::field label="Name" label_width="2" input_width="10">
    <img src="https://picsum.photos/100">
</x-larastrap::field>

<br>

<x-larastrap::field label="Name" :label_width="['md' => 4, 'lg' => 3]" :input_width="['md' => 8, 'lg' => 9]">
    <img src="https://picsum.photos/100">

    <p>
        Here, the width of label's and input's columns vary accordingly to the responsive breakpoint.
    </p>
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-1a76f2853c512373d39dd7540f33aec8" class="col-6 col-form-label">Name</label>
  <div class="col-6">
    <img src="https://picsum.photos/100">
  </div>
</div><br>
<div class="row mb-3">
  <label for="field-8cb7146cd9521bee38fe87ea99ea9ff3" class="col-2 col-form-label">Name</label>
  <div class="col-10">
    <img src="https://picsum.photos/100">
  </div>
</div><br>
<div class="row mb-3">
  <label for="field-7a1ff13cc08f63c5d36c660995f7b170" class="col-md-4 col-lg-3 col-form-label">Name</label>
  <div class="col-md-8 col-lg-9">
    <img src="https://picsum.photos/100">
    <p>
      Here, the width of label's and input's columns vary accordingly to the responsive breakpoint.
    </p>
  </div>
</div>

Those parameter are inherited from the parent Container, to avoid to specify for each field. Even better: it is recommended to define those value in the Larastrap's global configuration, once and for all the application, and override them only when required for specific and occasional edge cases.

<x-larastrap::form label_width="6" input_width="6">
    <x-larastrap::text name="name" label="Name" />
    <x-larastrap::email name="email" label="EMail" />
</x-larastrap::form>
<form id="form-6364d3f0f495b6ab9dcf8d3b5c6e0b01" class="" method="post" name="form-6364d3f0f495b6ab9dcf8d3b5c6e0b01">
  <input type="hidden" name="_token" value="K8LO2qAnt9vxMXRo8KdgnyajlLee29odLlmG0Z0s" autocomplete="off">
  <div class="row mb-3">
    <label for="input-cdbb8704d9a3835a915c56ed35b34dd2" class="col-6 col-form-label">Name</label>
    <div class="col-6">
      <input id="input-cdbb8704d9a3835a915c56ed35b34dd2" type="text" class="form-control" name="name" value="">
    </div>
  </div>
  <div class="row mb-3">
    <label for="input-1379cb82655152d9008b40f952fe602c" class="col-6 col-form-label">EMail</label>
    <div class="col-6">
      <input id="input-1379cb82655152d9008b40f952fe602c" type="email" class="form-control" name="email" value="">
    </div>
  </div>
  <div class="col-12 text-end">
    <button id="button-40945fdaf1a3654ca107b87635c7a75e" class="btn btn-primary" type="submit">Save</button>
  </div>
</form>

help

The help parameter implements a classic Bootstrap's pattern: displays a muted text below the actual content, to be used as an explicit hint for the user.

This help line is always displayed
<x-larastrap::field label="Name" help="This help line is always displayed">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-07ac2867d2a76c51bbab798830e81a8e" class="col-4 col-form-label">Name</label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
    <div class="form-text">
      This help line is always displayed
    </div>
  </div>
</div>

pophelp

The pophelp parameter is an alternative to help , and provides a popover to be used for more detailed (or less invasive) descriptions of the purpose of each field.

Remember you have to init the Javascript function to handle Bootstrap's popovers!

<x-larastrap::field label="Name" pophelp="This is a hoverable help box">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-dc255dd58d62ff6479b50f6148020d9f" class="col-4 col-form-label">Name <span class="badge rounded-pill bg-primary" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-content="This is a hoverable help box">?</span></label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

It is possible to overwrite once the aspect of the pophelp element directly from the global configuration file, to change the symbol and the CSS classes.

<!--

In your config/larastrap.php file...

return [
    'pophelp' => [
        'symbol' => 'HELP!',
        'override_classes' => ['badge', 'bg-success', 'float-start', 'me-2'],
    ]
];

-->

<x-larastrap::field label="Name" pophelp="This is a hoverable help box">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-96b80f14763b26d7ca02840041a3fa17" class="col-4 col-form-label">Name <span class="badge bg-success float-start me-2" data-bs-toggle="popover" data-bs-trigger="hover" data-bs-content="This is a hoverable help box">HELP!</span></label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

Also symbol_html can be used, to specify a HTML string to be used as label (for example: a Bootstrap icon).

<!--

In your config/larastrap.php file...

return [
    'pophelp' => [
        'symbol_html' => '<i class="bi bi-info-circle-fill"></i>',
        'override_classes' => ['text-danger'],
    ]
];

-->

<x-larastrap::field label="Name" pophelp="This is a hoverable help box">
    <img src="https://picsum.photos/100">
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-8a638c1b15377b64e7d32f5735f22a28" class="col-4 col-form-label">Name</label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
  </div>
</div>

do_label

The do_label parameter can be valorized to

  • show - the default: the label is formatted accondingly the the parent's formview )
  • hide - the label is omitted from the final output, but the field's content keeps his size
  • empty - the label is appended to the HTML, but contains an empty string despite any other configuration
  • squeeze - both label and wrapping field are totally omitted

Normal behavior

Hidden label: in a horizontal form, the content is shifted to the left due the missing node

Empty label: in a horizontal form, the space allocation for the label is still preserved

No label nor wrapping field (.row and .col divs)

<x-larastrap::field label="Normal Label" do_label="show">
    <img src="https://picsum.photos/100">
    <p>Normal behavior</p>
</x-larastrap::field>

<x-larastrap::field label="Name" do_label="hide">
    <img src="https://picsum.photos/100">
    <p>Hidden label: in a horizontal form, the content is shifted to the left due the missing node</p>
</x-larastrap::field>

<x-larastrap::field label="Name" do_label="empty">
    <img src="https://picsum.photos/100">
    <p>Empty label: in a horizontal form, the space allocation for the label is still preserved</p>
</x-larastrap::field>

<x-larastrap::field label="Name" do_label="squeeze">
    <img src="https://picsum.photos/100">
    <p>No label nor wrapping field (.row and .col divs)</p>
</x-larastrap::field>
<div class="row mb-3">
  <label for="field-6d2148e5a9dfaf5159536a0e803ed077" class="col-4 col-form-label">Normal Label</label>
  <div class="col-8">
    <img src="https://picsum.photos/100">
    <p>
      Normal behavior
    </p>
  </div>
</div>
<div class="row mb-3">
  <div class="col-8">
    <img src="https://picsum.photos/100">
    <p>
      Hidden label: in a horizontal form, the content is shifted to the left due the missing node
    </p>
  </div>
</div>
<div class="row mb-3">
  <div class="col-8">
    <img src="https://picsum.photos/100">
    <p>
      Empty label: in a horizontal form, the space allocation for the label is still preserved
    </p>
  </div>
</div><img src="https://picsum.photos/100">
<p>
  No label nor wrapping field (.row and .col divs)
</p>

Here a more exaustive example, using a x-larastrap::text as a reference.

<x-larastrap::text placeholder="Normal field, input + label" label="Test 1" />
<x-larastrap::text placeholder="Label is hidden, input has the same width" label="Test 2" do_label="hide" />
<x-larastrap::text placeholder="Label is blank, but has allocated width" label="Test 3" do_label="empty" />
<x-larastrap::text placeholder="No label at all, full width input" label="Test 4" do_label="squeeze" />
<div class="row mb-3">
  <label for="input-ceb54b7b816f577134501ca37379dafb" class="col-4 col-form-label">Test 1</label>
  <div class="col-8">
    <input id="input-ceb54b7b816f577134501ca37379dafb" type="text" class="form-control" name="hlJDLx7N5d" value="" placeholder="Normal field, input + label">
  </div>
</div>
<div class="row mb-3">
  <div class="col-8">
    <input id="input-dd5eceb7c9ac75712f9e2884671bdcba" type="text" class="form-control" name="7qryfmQrJH" value="" placeholder="Label is hidden, input has the same width">
  </div>
</div>
<div class="row mb-3">
  <div class="col-8">
    <input id="input-f5ac7ad2e1a2f57bb3ae8cc9a02b6ce0" type="text" class="form-control" name="PBptOjRgaU" value="" placeholder="Label is blank, but has allocated width">
  </div>
</div><input id="input-736fc2e335feeba0e79e045ab72f08c8" type="text" class="form-control" name="s5SkYL0bBZ" value="" placeholder="No label at all, full width input">

squeeze

squeeze parameter is just a convenient shorthand for do_label="squeeze" .

This is rendered without a wrapping field.

<x-larastrap::field squeeze>
    <p>
        This is rendered without a wrapping field.
    </p>
</x-larastrap::field>
<p>
  This is rendered without a wrapping field.
</p>

This is mostly intended for usage within an actual input, and setup it to omit the field when a label is not required (e.g. within a table).

<table class="table">
    <tbody>
        @foreach(App\Models\User::take(3)->get() as $user)
            <x-larastrap::enclose :obj="$user">
                <tr>
                    <td>
                        <x-larastrap::text name="name" squeeze />
                    </td>
                    <td>
                        <x-larastrap::text name="email" squeeze />
                    </td>
                </tr>
            </x-larastrap::enclose>
        @endforeach
    </tbody>
</table>
<table class="table">
  <tbody>
    <tr>
      <td>
        <input id="input-84fd8d23f8549338be8cfb6e5570c84b" type="text" class="form-control" name="name" value="Prof. Ethan Schmitt III">
      </td>
      <td>
        <input id="input-7af4c36f67af03ae6e1d035c44c8151c" type="text" class="form-control" name="email" value="ashly77@example.org">
      </td>
    </tr>
    <tr>
      <td>
        <input id="input-bb9325b1a14590f05042eb046489c618" type="text" class="form-control" name="name" value="Mr. Randall Hyatt">
      </td>
      <td>
        <input id="input-5d1e1233f959b8cc664fa656663b1cd6" type="text" class="form-control" name="email" value="cwalsh@example.com">
      </td>
    </tr>
    <tr>
      <td>
        <input id="input-199cb3b911015b4cc8155c204d704fd0" type="text" class="form-control" name="name" value="Dr. Darrin Hamill">
      </td>
      <td>
        <input id="input-07a7958d50a22dd74166f1968b2857b0" type="text" class="form-control" name="email" value="lincoln.davis@example.net">
      </td>
    </tr>
  </tbody>
</table>