in English

Awọn fọọmu

Awọn apẹẹrẹ ati awọn itọnisọna lilo fun awọn ara iṣakoso fọọmu, awọn aṣayan akọkọ, ati awọn paati aṣa fun ṣiṣẹda ọpọlọpọ awọn fọọmu.

Akopọ

Awọn iṣakoso fọọmu Bootstrap faagun lori awọn aza fọọmu Atunbere wa pẹlu awọn kilasi. Lo awọn kilasi wọnyi lati jade sinu awọn ifihan adani wọn fun ṣiṣe deede diẹ sii kọja awọn aṣawakiri ati awọn ẹrọ.

Rii daju pe o lo abuda ti o yẹ typelori gbogbo awọn igbewọle (fun apẹẹrẹ, emailfun adirẹsi imeeli tabi numberfun alaye nọmba) lati lo anfani awọn iṣakoso igbewọle tuntun bii ijẹrisi imeeli, yiyan nọmba, ati diẹ sii.

Eyi ni apẹẹrẹ iyara lati ṣafihan awọn aza fọọmu Bootstrap. Jeki kika fun iwe lori awọn kilasi ti a beere, iṣeto fọọmu, ati diẹ sii.

A kii yoo pin imeeli rẹ pẹlu ẹnikẹni miiran.
<form>
  <div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp">
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Password</label>
    <input type="password" class="form-control" id="exampleInputPassword1">
  </div>
  <div class="form-group form-check">
    <input type="checkbox" class="form-check-input" id="exampleCheck1">
    <label class="form-check-label" for="exampleCheck1">Check me out</label>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Awọn iṣakoso fọọmu

Awọn iṣakoso fọọmu ọrọ-bii <input>s, <select>s, ati <textarea>s — jẹ aṣa pẹlu .form-controlkilasi naa. To wa pẹlu awọn aza fun irisi gbogbogbo, ipo idojukọ, iwọn, ati diẹ sii.

Rii daju lati ṣawari awọn fọọmu aṣa wa si ara <select>s siwaju sii.

<form>
  <div class="form-group">
    <label for="exampleFormControlInput1">Email address</label>
    <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]">
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect1">Example select</label>
    <select class="form-control" id="exampleFormControlSelect1">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect2">Example multiple select</label>
    <select multiple class="form-control" id="exampleFormControlSelect2">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>
  </div>
  <div class="form-group">
    <label for="exampleFormControlTextarea1">Example textarea</label>
    <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
  </div>
</form>

Fun awọn igbewọle faili, paarọ rẹ .form-controlfun .form-control-file.

<form>
  <div class="form-group">
    <label for="exampleFormControlFile1">Example file input</label>
    <input type="file" class="form-control-file" id="exampleFormControlFile1">
  </div>
</form>

Titobi

Ṣeto awọn giga ni lilo awọn kilasi bii .form-control-lgati .form-control-sm.

<input class="form-control form-control-lg" type="text" placeholder=".form-control-lg">
<input class="form-control" type="text" placeholder="Default input">
<input class="form-control form-control-sm" type="text" placeholder=".form-control-sm">
<select class="form-control form-control-lg">
  <option>Large select</option>
</select>
<select class="form-control">
  <option>Default select</option>
</select>
<select class="form-control form-control-sm">
  <option>Small select</option>
</select>

Ka nikan

Ṣafikun readonlyabuda Boolean lori titẹ sii lati yago fun iyipada iye titẹ sii. Awọn igbewọle kika-nikan han fẹẹrẹfẹ (gẹgẹbi awọn igbewọle alaabo), ṣugbọn mu kọsọ boṣewa duro.

<input class="form-control" type="text" placeholder="Readonly input here..." readonly>

Ọrọ itele nikan

Ti o ba fẹ lati ni <input readonly>awọn eroja ninu fọọmu rẹ ti a ṣe gẹgẹ bi ọrọ itele, lo .form-control-plaintextkilasi naa lati yọkuro iselo aaye fọọmu aiyipada ki o tọju ala to pe ati padding.

<form>
  <div class="form-group row">
    <label for="staticEmail" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="text" readonly class="form-control-plaintext" id="staticEmail" value="[email protected]">
    </div>
  </div>
  <div class="form-group row">
    <label for="inputPassword" class="col-sm-2 col-form-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword">
    </div>
  </div>
</form>
<form class="form-inline">
  <div class="form-group mb-2">
    <label for="staticEmail2" class="sr-only">Email</label>
    <input type="text" readonly class="form-control-plaintext" id="staticEmail2" value="[email protected]">
  </div>
  <div class="form-group mx-sm-3 mb-2">
    <label for="inputPassword2" class="sr-only">Password</label>
    <input type="password" class="form-control" id="inputPassword2" placeholder="Password">
  </div>
  <button type="submit" class="btn btn-primary mb-2">Confirm identity</button>
</form>

Awọn igbewọle sakani

Ṣeto awọn igbewọle ibiti o ti yi lọ ni ita ni lilo .form-control-range.

<form>
  <div class="form-group">
    <label for="formControlRange">Example Range input</label>
    <input type="range" class="form-control-range" id="formControlRange">
  </div>
</form>

Awọn apoti ayẹwo ati awọn redio

Awọn apoti ayẹwo aiyipada ati awọn redio ti ni ilọsiwaju lori pẹlu iranlọwọ ti .form-check, kilasi ẹyọkan fun awọn iru igbewọle mejeeji ti o ṣe ilọsiwaju iṣeto ati ihuwasi awọn eroja HTML wọn . Awọn apoti ayẹwo jẹ fun yiyan ọkan tabi pupọ awọn aṣayan ninu atokọ kan, lakoko ti awọn redio wa fun yiyan aṣayan kan lati ọpọlọpọ.

Awọn apoti ayẹwo alaabo ati awọn redio ni atilẹyin. Ẹya naa disabledyoo lo awọ fẹẹrẹfẹ lati ṣe iranlọwọ tọka ipo igbewọle naa.

Awọn apoti ayẹwo ati awọn bọtini redio ṣe atilẹyin afọwọsi fọọmu orisun HTML ati pese awọn aami ṣoki, wiwọle. Bi iru bẹẹ, awọn <input>s ati <label>awọn ara wa jẹ awọn eroja arakunrin ni idakeji si <input>laarin kan <label>. Eyi jẹ ọrọ-ọrọ diẹ sii bi o ṣe gbọdọ pato idati forawọn abuda lati ṣe ibatan <input>ati <label>.

Aiyipada (tolera)

Nipa aiyipada, nọmba eyikeyi ti awọn apoti ayẹwo ati awọn redio ti o jẹ arakunrin lẹsẹkẹsẹ yoo wa ni tolera ati ni aye ni deede pẹlu .form-check.

<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
  <label class="form-check-label" for="defaultCheck1">
    Default checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="defaultCheck2" disabled>
  <label class="form-check-label" for="defaultCheck2">
    Disabled checkbox
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
  <label class="form-check-label" for="exampleRadios1">
    Default radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios2" value="option2">
  <label class="form-check-label" for="exampleRadios2">
    Second default radio
  </label>
</div>
<div class="form-check">
  <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios3" value="option3" disabled>
  <label class="form-check-label" for="exampleRadios3">
    Disabled radio
  </label>
</div>

Ni tito

Awọn apoti ayẹwo ẹgbẹ tabi awọn redio lori ila petele kanna nipa fifi kun .form-check-inlinesi eyikeyi .form-check.

<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="option1">
  <label class="form-check-label" for="inlineCheckbox1">1</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox2" value="option2">
  <label class="form-check-label" for="inlineCheckbox2">2</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="checkbox" id="inlineCheckbox3" value="option3" disabled>
  <label class="form-check-label" for="inlineCheckbox3">3 (disabled)</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">
  <label class="form-check-label" for="inlineRadio1">1</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
  <label class="form-check-label" for="inlineRadio2">2</label>
</div>
<div class="form-check form-check-inline">
  <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio3" value="option3" disabled>
  <label class="form-check-label" for="inlineRadio3">3 (disabled)</label>
</div>

Laisi awọn akole

Ṣafikun .position-staticsi awọn igbewọle laarin .form-checkiyẹn ko ni ọrọ aami eyikeyi. Ranti lati tun pese diẹ ninu awọn orukọ wiwọle fun awọn imọ-ẹrọ iranlọwọ (fun apẹẹrẹ, lilo aria-label).

<div class="form-check">
  <input class="form-check-input position-static" type="checkbox" id="blankCheckbox" value="option1" aria-label="...">
</div>
<div class="form-check">
  <input class="form-check-input position-static" type="radio" name="blankRadio" id="blankRadio1" value="option1" aria-label="...">
</div>

Ìfilélẹ

Niwọn igba ti Bootstrap ti kan display: blockati width: 100%si gbogbo awọn iṣakoso fọọmu wa, awọn fọọmu yoo jẹ akopọ aiyipada ni inaro. Awọn afikun awọn kilasi le ṣee lo lati yatọ si ipilẹ yii lori ipilẹ-fọọmu kan.

Awọn ẹgbẹ fọọmu

Kilasi naa .form-groupjẹ ọna ti o rọrun julọ lati ṣafikun eto diẹ si awọn fọọmu. O pese kilasi rọ ti o ṣe iwuri fun kikojọpọ awọn aami to dara, awọn idari, ọrọ iranlọwọ yiyan, ati fifiranṣẹ afọwọsi fọọmu. Nipa aiyipada o kan nikan margin-bottom, ṣugbọn o mu awọn aza ni afikun .form-inlinebi o ṣe nilo. Lo pẹlu <fieldset>s, <div>s, tabi fere eyikeyi eroja miiran.

<form>
  <div class="form-group">
    <label for="formGroupExampleInput">Example label</label>
    <input type="text" class="form-control" id="formGroupExampleInput" placeholder="Example input placeholder">
  </div>
  <div class="form-group">
    <label for="formGroupExampleInput2">Another label</label>
    <input type="text" class="form-control" id="formGroupExampleInput2" placeholder="Another input placeholder">
  </div>
</form>

Akoj fọọmu

Awọn fọọmu eka diẹ sii ni a le kọ nipa lilo awọn kilasi akoj wa. Lo iwọnyi fun awọn ipilẹ fọọmu ti o nilo awọn ọwọn pupọ, awọn iwọn ti o yatọ, ati awọn aṣayan titete afikun.

<form>
  <div class="row">
    <div class="col">
      <input type="text" class="form-control" placeholder="First name">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Last name">
    </div>
  </div>
</form>

Fọọmu kana

O tun le paarọ .rowfun .form-row, iyatọ ti ọna kika grid boṣewa wa ti o dojukọ awọn gọta ọwọn aiyipada fun wiwọ ati awọn ipilẹ iwapọ diẹ sii.

<form>
  <div class="form-row">
    <div class="col">
      <input type="text" class="form-control" placeholder="First name">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Last name">
    </div>
  </div>
</form>

Awọn ipalemo eka diẹ sii tun le ṣẹda pẹlu eto akoj.

<form>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputEmail4">Email</label>
      <input type="email" class="form-control" id="inputEmail4">
    </div>
    <div class="form-group col-md-6">
      <label for="inputPassword4">Password</label>
      <input type="password" class="form-control" id="inputPassword4">
    </div>
  </div>
  <div class="form-group">
    <label for="inputAddress">Address</label>
    <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St">
  </div>
  <div class="form-group">
    <label for="inputAddress2">Address 2</label>
    <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment, studio, or floor">
  </div>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputCity">City</label>
      <input type="text" class="form-control" id="inputCity">
    </div>
    <div class="form-group col-md-4">
      <label for="inputState">State</label>
      <select id="inputState" class="form-control">
        <option selected>Choose...</option>
        <option>...</option>
      </select>
    </div>
    <div class="form-group col-md-2">
      <label for="inputZip">Zip</label>
      <input type="text" class="form-control" id="inputZip">
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" id="gridCheck">
      <label class="form-check-label" for="gridCheck">
        Check me out
      </label>
    </div>
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

Fọọmu petele

Ṣẹda awọn fọọmu petele pẹlu akoj nipa fifi .rowkilasi kun lati ṣẹda awọn ẹgbẹ ati lilo awọn .col-*-*kilasi lati pato iwọn awọn aami ati awọn idari rẹ. Rii daju lati ṣafikun .col-form-labelsi awọn <label>s rẹ daradara nitorina wọn wa ni inaro pẹlu awọn idari fọọmu ti o somọ.

Ni awọn igba miiran, o le nilo lati lo ala tabi awọn ohun elo padding lati ṣẹda titete pipe ti o nilo. Fun apẹẹrẹ, a ti yọkuro padding-toplori aami awọn igbewọle redio tolera wa lati ṣe deede ipilẹ ọrọ dara julọ.

Redio
<form>
  <div class="form-group row">
    <label for="inputEmail3" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="inputEmail3">
    </div>
  </div>
  <div class="form-group row">
    <label for="inputPassword3" class="col-sm-2 col-form-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="inputPassword3">
    </div>
  </div>
  <fieldset class="form-group row">
    <legend class="col-form-label col-sm-2 float-sm-left pt-0">Radios</legend>
    <div class="col-sm-10">
      <div class="form-check">
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios1" value="option1" checked>
        <label class="form-check-label" for="gridRadios1">
          First radio
        </label>
      </div>
      <div class="form-check">
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios2" value="option2">
        <label class="form-check-label" for="gridRadios2">
          Second radio
        </label>
      </div>
      <div class="form-check disabled">
        <input class="form-check-input" type="radio" name="gridRadios" id="gridRadios3" value="option3" disabled>
        <label class="form-check-label" for="gridRadios3">
          Third disabled radio
        </label>
      </div>
    </div>
  </fieldset>
  <div class="form-group row">
    <div class="col-sm-10 offset-sm-2">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="gridCheck1">
        <label class="form-check-label" for="gridCheck1">
          Example checkbox
        </label>
      </div>
    </div>
  </div>
  <div class="form-group row">
    <div class="col-sm-10">
      <button type="submit" class="btn btn-primary">Sign in</button>
    </div>
  </div>
</form>
Petele fọọmu aami iwọn

Rii daju lati lo .col-form-label-smtabi .col-form-label-lgsi awọn <label>s tabi <legend>s rẹ lati tẹle iwọn .form-control-lgati deede .form-control-sm.

<form>
  <div class="form-group row">
    <label for="colFormLabelSm" class="col-sm-2 col-form-label col-form-label-sm">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control form-control-sm" id="colFormLabelSm" placeholder="col-form-label-sm">
    </div>
  </div>
  <div class="form-group row">
    <label for="colFormLabel" class="col-sm-2 col-form-label">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="colFormLabel" placeholder="col-form-label">
    </div>
  </div>
  <div class="form-group row">
    <label for="colFormLabelLg" class="col-sm-2 col-form-label col-form-label-lg">Email</label>
    <div class="col-sm-10">
      <input type="email" class="form-control form-control-lg" id="colFormLabelLg" placeholder="col-form-label-lg">
    </div>
  </div>
</form>

Iwọn ti ọwọn

Gẹgẹbi a ti han ninu awọn apẹẹrẹ ti tẹlẹ, eto akoj wa gba ọ laaye lati gbe nọmba eyikeyi ti .cols laarin kan .rowtabi .form-row. Wọn yoo pin iwọn to wa ni dọgbadọgba laarin wọn. O tun le mu ipin kan ti awọn ọwọn rẹ lati gba aaye diẹ sii tabi kere si, lakoko ti awọn ti o ku ni o .colpin bakanna pẹlu awọn kilasi iwe kan pato bi .col-7.

<form>
  <div class="form-row">
    <div class="col-7">
      <input type="text" class="form-control" placeholder="City">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="State">
    </div>
    <div class="col">
      <input type="text" class="form-control" placeholder="Zip">
    </div>
  </div>
</form>

Titobi aifọwọyi

Apẹẹrẹ ti o wa ni isalẹ nlo ohun elo flexbox lati aarin awọn akoonu inu inaro ati awọn iyipada .colsi .col-autoki awọn ọwọn rẹ nikan gba aaye pupọ bi o ṣe nilo. Fi ọna miiran, awọn iwọn ọwọn funrararẹ da lori awọn akoonu.

@
<form>
  <div class="form-row align-items-center">
    <div class="col-auto">
      <label class="sr-only" for="inlineFormInput">Name</label>
      <input type="text" class="form-control mb-2" id="inlineFormInput" placeholder="Jane Doe">
    </div>
    <div class="col-auto">
      <label class="sr-only" for="inlineFormInputGroup">Username</label>
      <div class="input-group mb-2">
        <div class="input-group-prepend">
          <div class="input-group-text">@</div>
        </div>
        <input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Username">
      </div>
    </div>
    <div class="col-auto">
      <div class="form-check mb-2">
        <input class="form-check-input" type="checkbox" id="autoSizingCheck">
        <label class="form-check-label" for="autoSizingCheck">
          Remember me
        </label>
      </div>
    </div>
    <div class="col-auto">
      <button type="submit" class="btn btn-primary mb-2">Submit</button>
    </div>
  </div>
</form>

Lẹhinna o le tun ṣe atunṣe lẹẹkan si pẹlu awọn kilasi iwe-iwọn kan pato.

@
<form>
  <div class="form-row align-items-center">
    <div class="col-sm-3 my-1">
      <label class="sr-only" for="inlineFormInputName">Name</label>
      <input type="text" class="form-control" id="inlineFormInputName" placeholder="Jane Doe">
    </div>
    <div class="col-sm-3 my-1">
      <label class="sr-only" for="inlineFormInputGroupUsername">Username</label>
      <div class="input-group">
        <div class="input-group-prepend">
          <div class="input-group-text">@</div>
        </div>
        <input type="text" class="form-control" id="inlineFormInputGroupUsername" placeholder="Username">
      </div>
    </div>
    <div class="col-auto my-1">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="autoSizingCheck2">
        <label class="form-check-label" for="autoSizingCheck2">
          Remember me
        </label>
      </div>
    </div>
    <div class="col-auto my-1">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Ati pe dajudaju awọn iṣakoso fọọmu aṣa ni atilẹyin.

<form>
  <div class="form-row align-items-center">
    <div class="col-auto my-1">
      <label class="mr-sm-2 sr-only" for="inlineFormCustomSelect">Preference</label>
      <select class="custom-select mr-sm-2" id="inlineFormCustomSelect">
        <option selected>Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
    </div>
    <div class="col-auto my-1">
      <div class="custom-control custom-checkbox mr-sm-2">
        <input type="checkbox" class="custom-control-input" id="customControlAutosizing">
        <label class="custom-control-label" for="customControlAutosizing">Remember my preference</label>
      </div>
    </div>
    <div class="col-auto my-1">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>

Awọn fọọmu inline

Lo .form-inlinekilasi naa lati ṣe afihan lẹsẹsẹ awọn aami, awọn idari fọọmu, ati awọn bọtini lori ila petele kan. Awọn idari fọọmu laarin awọn fọọmu laini yatọ diẹ si awọn ipinlẹ aifọwọṣe wọn.

  • Awọn iṣakoso jẹ display: flex, ṣubu eyikeyi aaye funfun HTML ati gbigba ọ laaye lati pese iṣakoso titete pẹlu aye ati awọn ohun elo flexbox .
  • Awọn iṣakoso ati awọn ẹgbẹ titẹ sii gba width: autolati yiyipada aiyipada Bootstrap kuro width: 100%.
  • Awọn idari nikan han laini ni awọn ibudo wiwo ti o kere ju 576px fife lati ṣe akọọlẹ fun awọn iwoye dín lori awọn ẹrọ alagbeka.

O le nilo lati fi ọwọ koju iwọn ati titete awọn idari fọọmu kọọkan pẹlu awọn ohun elo aye aye (bii o han ni isalẹ). Nikẹhin, rii daju pe nigbagbogbo ni <label>pẹlu iṣakoso fọọmu kọọkan, paapaa ti o ba nilo lati tọju rẹ lati ọdọ awọn alejo ti kii ṣe oluka iboju pẹlu .sr-only.

@
<form class="form-inline">
  <label class="sr-only" for="inlineFormInputName2">Name</label>
  <input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Jane Doe">

  <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
  <div class="input-group mb-2 mr-sm-2">
    <div class="input-group-prepend">
      <div class="input-group-text">@</div>
    </div>
    <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Username">
  </div>

  <div class="form-check mb-2 mr-sm-2">
    <input class="form-check-input" type="checkbox" id="inlineFormCheck">
    <label class="form-check-label" for="inlineFormCheck">
      Remember me
    </label>
  </div>

  <button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>

Awọn iṣakoso fọọmu aṣa ati yiyan tun ni atilẹyin.

<form class="form-inline">
  <label class="my-1 mr-2" for="inlineFormCustomSelectPref">Preference</label>
  <select class="custom-select my-1 mr-sm-2" id="inlineFormCustomSelectPref">
    <option selected>Choose...</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>

  <div class="custom-control custom-checkbox my-1 mr-sm-2">
    <input type="checkbox" class="custom-control-input" id="customControlInline">
    <label class="custom-control-label" for="customControlInline">Remember my preference</label>
  </div>

  <button type="submit" class="btn btn-primary my-1">Submit</button>
</form>
Awọn yiyan si awọn aami ti o farapamọ

Awọn imọ-ẹrọ iranlọwọ gẹgẹbi awọn oluka iboju yoo ni wahala pẹlu awọn fọọmu rẹ ti o ko ba pẹlu aami kan fun gbogbo igbewọle. Fun awọn fọọmu inline, o le tọju awọn akole ni lilo .sr-onlykilasi naa. Awọn ọna omiiran siwaju wa ti ipese aami fun awọn imọ-ẹrọ iranlọwọ, gẹgẹbi awọn aria-label, aria-labelledbytabi titleeefa. Ti ko ba si ọkan ninu iwọnyi ti o wa, awọn imọ-ẹrọ iranlọwọ le lo abuda naa placeholder, ti o ba wa, ṣugbọn ṣe akiyesi lilo placeholderbi rirọpo fun awọn ọna isamisi miiran ko ni imọran.

Ọrọ iranlọwọ

Ọrọ iranlọwọ ipele-idina ni awọn fọọmu le ṣẹda ni lilo .form-text(eyiti a mọ tẹlẹ bi .help-blockni v3). Ọrọ iranlọwọ inu ila le jẹ imuse ni irọrun ni lilo eyikeyi inline HTML ano ati awọn kilasi iwulo bii .text-muted.

Associating ọrọ iranlọwọ pẹlu awọn idari fọọmu

Ọrọ iranlọwọ yẹ ki o ni nkan ṣe ni gbangba pẹlu iṣakoso fọọmu ti o nii ṣe pẹlu lilo abuda naa aria-describedby. Eyi yoo rii daju pe awọn imọ-ẹrọ iranlọwọ-gẹgẹbi awọn oluka iboju-yoo kede ọrọ iranlọwọ yii nigbati olumulo ba dojukọ tabi wọ inu iṣakoso naa.

Ọrọ iranlọwọ ni isalẹ awọn igbewọle le jẹ aṣa pẹlu .form-text. Kilasi yii pẹlu display: blockati ṣafikun diẹ ninu ala oke fun aye irọrun lati awọn igbewọle loke.

Ọrọigbaniwọle rẹ gbọdọ jẹ awọn lẹta 8-20 gigun, ni awọn lẹta ati awọn nọmba ninu, ko si gbọdọ ni awọn alafo, awọn ohun kikọ pataki, tabi emoji ninu.
<label for="inputPassword5">Password</label>
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
<small id="passwordHelpBlock" class="form-text text-muted">
  Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</small>

Ọrọ inline le lo eyikeyi opopo HTML ano (jẹ a <small>, <span>, tabi nkan miran) pẹlu ohunkohun siwaju sii ju a IwUlO kilasi.

Gbọdọ jẹ awọn ohun kikọ 8-20 gigun.
<form class="form-inline">
  <div class="form-group">
    <label for="inputPassword6">Password</label>
    <input type="password" id="inputPassword6" class="form-control mx-sm-3" aria-describedby="passwordHelpInline">
    <small id="passwordHelpInline" class="text-muted">
      Must be 8-20 characters long.
    </small>
  </div>
</form>

Awọn fọọmu alaabo

Ṣafikun disabledabuda Boolean lori titẹ sii lati ṣe idiwọ awọn ibaraenisepo olumulo ati jẹ ki o han pe o fẹẹrẹfẹ.

<input class="form-control" id="disabledInput" type="text" placeholder="Disabled input here..." disabled>

Fi awọn disabledikalara si kan <fieldset>lati mu gbogbo awọn idari laarin.

Apeere aaye alaabo
<form>
  <fieldset disabled>
    <legend>Disabled fieldset example</legend>
    <div class="form-group">
      <label for="disabledTextInput">Disabled input</label>
      <input type="text" id="disabledTextInput" class="form-control" placeholder="Disabled input">
    </div>
    <div class="form-group">
      <label for="disabledSelect">Disabled select menu</label>
      <select id="disabledSelect" class="form-control">
        <option>Disabled select</option>
      </select>
    </div>
    <div class="form-group">
      <div class="form-check">
        <input class="form-check-input" type="checkbox" id="disabledFieldsetCheck" disabled>
        <label class="form-check-label" for="disabledFieldsetCheck">
          Can't check this
        </label>
      </div>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </fieldset>
</form>
Caveat pẹlu ìdákọró

Awọn aṣawakiri ṣe itọju gbogbo awọn iṣakoso fọọmu abinibi ( <input>, <select>, ati <button>awọn eroja) inu <fieldset disabled>bi alaabo, idilọwọ mejeeji awọn ibaraẹnisọrọ keyboard ati Asin lori wọn.

Sibẹsibẹ, ti fọọmu rẹ ba pẹlu awọn eroja ti aṣa bi bọtini bii <a ... class="btn btn-*">, iwọnyi yoo fun ni ara ti pointer-events: none. Gẹgẹbi a ti ṣe akiyesi ni apakan nipa ipo alaabo fun awọn bọtini (ati ni pataki ni apakan-apakan fun awọn eroja oran), ohun-ini CSS yii ko tii diwọn ati pe ko ni atilẹyin ni kikun ni Internet Explorer 10. Awọn iṣakoso ti o da lori oran yoo tun jẹ tun. fojusi ati operable lilo awọn keyboard. O gbọdọ ṣe atunṣe awọn iṣakoso wọnyi pẹlu ọwọ nipa fifi kun tabindex="-1"lati ṣe idiwọ wọn lati gba idojukọ ati aria-disabled="disabled"lati ṣe ifihan ipo wọn si awọn imọ-ẹrọ iranlọwọ.

Agbelebu-kiri ibamu

Lakoko ti Bootstrap yoo lo awọn aṣa wọnyi ni gbogbo awọn aṣawakiri, Internet Explorer 11 ati ni isalẹ ko ṣe atilẹyin disabledẹya ni kikun lori faili <fieldset>. Lo JavaScript ti aṣa lati mu eto aaye kuro ninu awọn aṣawakiri wọnyi.

Ifọwọsi

Pese awọn esi ti o niyelori, ti o ṣee ṣe si awọn olumulo rẹ pẹlu afọwọsi fọọmu HTML5- wa ni gbogbo awọn aṣawakiri atilẹyin wa . Yan lati awọn esi afọwọsi aiyipada aṣawakiri, tabi ṣe awọn ifiranṣẹ aṣa pẹlu awọn kilasi ti a ṣe sinu ati JavaScript ibẹrẹ.

A mọ pe lọwọlọwọ awọn aṣa afọwọsi aṣa ẹgbẹ alabara ati awọn imọran irinṣẹ ko wa, nitori wọn ko farahan si awọn imọ-ẹrọ iranlọwọ. Lakoko ti a n ṣiṣẹ lori ojutu kan, a yoo ṣeduro boya lilo aṣayan ẹgbẹ olupin tabi ọna afọwọsi aṣawakiri aiyipada.

Bawo ni o ṣe n ṣiṣẹ

Eyi ni bii afọwọsi fọọmu ṣiṣẹ pẹlu Bootstrap:

  • Afọwọsi fọọmu HTML jẹ lilo nipasẹ awọn kilasi afarape meji ti CSS, :invalidati :valid. O kan si <input>, <select>, ati <textarea>awọn eroja.
  • Bootstrap dopin :invalidati :validawọn aza si kilasi obi .was-validated, nigbagbogbo loo si <form>. Bibẹẹkọ, eyikeyi aaye ti a beere laisi iye kan fihan bi aifẹ lori fifuye oju-iwe. Ni ọna yii, o le yan igba lati mu wọn ṣiṣẹ (paapaa lẹhin igbidanwo ifakalẹ fọọmu).
  • Lati tun irisi fọọmu naa tunto (fun apẹẹrẹ, ninu ọran ti awọn ifisilẹ fọọmu ti o ni agbara nipa lilo AJAX), yọ .was-validatedkilasi kuro <form>lẹẹkansi lẹhin ifakalẹ.
  • Gẹgẹbi ipadasẹhin, .is-invalidati .is-validawọn kilasi le ṣee lo dipo awọn kilasi pseudo-fun afọwọsi ẹgbẹ olupin . Wọn ko nilo .was-validatedkilasi obi kan.
  • Nitori awọn idiwọ ni bii CSS ṣe n ṣiṣẹ, a ko le (ni lọwọlọwọ) lo awọn aṣa si <label>eyiti o wa ṣaaju iṣakoso fọọmu ni DOM laisi iranlọwọ ti JavaScript aṣa.
  • Gbogbo awọn aṣawakiri ode oni ṣe atilẹyin API afọwọsi idilọwọ , lẹsẹsẹ awọn ọna JavaScript fun imudari awọn iṣakoso fọọmu.
  • Awọn ifiranšẹ esi le lo awọn aṣiṣe aṣawakiri (yatọ fun ẹrọ aṣawakiri kọọkan, ati aiṣedeede nipasẹ CSS) tabi awọn aza esi ti aṣa wa pẹlu HTML afikun ati CSS.
  • O le pese awọn ifiranṣẹ ifọwọsi aṣa pẹlu setCustomValidityni JavaScript.

Pẹlu iyẹn ni lokan, ṣe akiyesi awọn demos wọnyi fun awọn aza afọwọsi fọọmu aṣa wa, awọn kilasi ẹgbẹ olupin iyan, ati awọn aṣiṣe aṣawakiri.

Awọn aṣa aṣa

Fun awọn ifiranse fọọmu Bootstrap aṣa, iwọ yoo nilo lati ṣafikun novalidateabuda boolean si faili rẹ <form>. Eyi mu awọn imọran irinṣẹ esi aiyipada ẹrọ aṣawakiri ṣiṣẹ, ṣugbọn tun pese iraye si awọn API afọwọsi fọọmu ni JavaScript. Gbiyanju lati fi awọn fọọmu ni isalẹ; JavaScript wa yoo ṣe idiwọ bọtini ifisilẹ ati yi awọn esi pada si ọ. Nigbati o ba n gbiyanju lati fi silẹ, iwọ yoo rii :invalidati :validawọn aṣa ti a lo si awọn iṣakoso fọọmu rẹ.

Awọn ara esi ti aṣa lo awọn awọ aṣa, awọn aala, awọn aza idojukọ, ati awọn aami abẹlẹ lati ṣe ibasọrọ dara si awọn esi. Awọn aami abẹlẹ fun <select>s wa pẹlu .custom-select, kii ṣe .form-control.

Wulẹ dara!
Wulẹ dara!
Jọwọ pese ilu to wulo.
Jọwọ yan ipo to wulo.
Jọwọ pese zip ti o wulo.
O gbọdọ gba ṣaaju ki o to fi silẹ.
<form class="needs-validation" novalidate>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationCustom01">First name</label>
      <input type="text" class="form-control" id="validationCustom01" value="Mark" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationCustom02">Last name</label>
      <input type="text" class="form-control" id="validationCustom02" value="Otto" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationCustom03">City</label>
      <input type="text" class="form-control" id="validationCustom03" required>
      <div class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom04">State</label>
      <select class="custom-select" id="validationCustom04" required>
        <option selected disabled value="">Choose...</option>
        <option>...</option>
      </select>
      <div class="invalid-feedback">
        Please select a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationCustom05">Zip</label>
      <input type="text" class="form-control" id="validationCustom05" required>
      <div class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
      <label class="form-check-label" for="invalidCheck">
        Agree to terms and conditions
      </label>
      <div class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

<script>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function() {
  'use strict';
  window.addEventListener('load', function() {
    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.getElementsByClassName('needs-validation');
    // Loop over them and prevent submission
    var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) {
          event.preventDefault();
          event.stopPropagation();
        }
        form.classList.add('was-validated');
      }, false);
    });
  }, false);
})();
</script>

Awọn aṣiṣe aṣawakiri

Ko nifẹ si awọn ifiranṣẹ esi afọwọsi aṣa tabi kikọ JavaScript lati yi awọn ihuwasi fọọmu pada? O dara, o le lo awọn aṣiṣe aṣawakiri. Gbiyanju lati firanṣẹ fọọmu ni isalẹ. Da lori ẹrọ aṣawakiri rẹ ati OS, iwọ yoo rii ara esi ti o yatọ diẹ diẹ.

Lakoko ti awọn aza esi wọnyi ko le ṣe aṣa pẹlu CSS, o tun le ṣe akanṣe ọrọ esi nipasẹ JavaScript.

<form>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationDefault01">First name</label>
      <input type="text" class="form-control" id="validationDefault01" value="Mark" required>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationDefault02">Last name</label>
      <input type="text" class="form-control" id="validationDefault02" value="Otto" required>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationDefault03">City</label>
      <input type="text" class="form-control" id="validationDefault03" required>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationDefault04">State</label>
      <select class="custom-select" id="validationDefault04" required>
        <option selected disabled value="">Choose...</option>
        <option>...</option>
      </select>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationDefault05">Zip</label>
      <input type="text" class="form-control" id="validationDefault05" required>
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required>
      <label class="form-check-label" for="invalidCheck2">
        Agree to terms and conditions
      </label>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

Ẹgbẹ olupin

A ṣeduro lilo afọwọsi-ẹgbẹ alabara, ṣugbọn ti o ba nilo afọwọsi ẹgbẹ olupin, o le tọkasi invalid ati awọn aaye fọọmu to wulo pẹlu .is-invalidati .is-valid. Ṣe akiyesi pe .invalid-feedbacko tun ṣe atilẹyin pẹlu awọn kilasi wọnyi.

Fun awọn aaye aiṣedeede, rii daju pe esi ti ko tọ/ ifiranṣẹ aṣiṣe ni nkan ṣe pẹlu aaye fọọmu ti o yẹ nipa lilo aria-describedby. Iwa yii ngbanilaaye diẹ sii ju ọkan idlọ lati ṣe itọkasi, ti aaye naa ba ti tọka si ọrọ fọọmu ni afikun.

Wulẹ dara!
Wulẹ dara!
Jọwọ pese ilu to wulo.
Jọwọ yan ipo to wulo.
Jọwọ pese zip ti o wulo.
O gbọdọ gba ṣaaju ki o to fi silẹ.
<form>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationServer01">First name</label>
      <input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationServer02">Last name</label>
      <input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
      <div class="valid-feedback">
        Looks good!
      </div>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationServer03">City</label>
      <input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
      <div id="validationServer03Feedback" class="invalid-feedback">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer04">State</label>
      <select class="custom-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
        <option selected disabled value="">Choose...</option>
        <option>...</option>
      </select>
      <div id="validationServer04Feedback" class="invalid-feedback">
        Please select a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationServer05">Zip</label>
      <input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
      <div id="validationServer05Feedback" class="invalid-feedback">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
      <label class="form-check-label" for="invalidCheck3">
        Agree to terms and conditions
      </label>
      <div  id="invalidCheck3Feedback" class="invalid-feedback">
        You must agree before submitting.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

Awọn eroja atilẹyin

Awọn ara afọwọsi wa fun awọn iṣakoso fọọmu atẹle ati awọn paati:

  • <input>s ati <textarea>s pẹlu.form-control
  • <select>s pẹlu .form-controltabi.custom-select
  • .form-checks
  • .custom-checkboxs ati .custom-radios
  • .custom-file
Jọwọ tẹ ifiranṣẹ sii ni agbegbe ọrọ.
Apeere ọrọ esi ti ko tọ
Apeere diẹ sii ọrọ esi ti ko tọ
Apẹẹrẹ aiṣedeede aṣa yan esi
Apẹẹrẹ esi faili ti ko tọ
@
Apeere esi igbewọle aiṣedeede
Apeere esi igbewọle aiṣedeede
Apeere esi igbewọle aiṣedeede
<form class="was-validated">
  <div class="mb-3">
    <label for="validationTextarea">Textarea</label>
    <textarea class="form-control is-invalid" id="validationTextarea" placeholder="Required example textarea" required></textarea>
    <div class="invalid-feedback">
      Please enter a message in the textarea.
    </div>
  </div>

  <div class="custom-control custom-checkbox mb-3">
    <input type="checkbox" class="custom-control-input" id="customControlValidation1" required>
    <label class="custom-control-label" for="customControlValidation1">Check this custom checkbox</label>
    <div class="invalid-feedback">Example invalid feedback text</div>
  </div>

  <div class="custom-control custom-radio">
    <input type="radio" class="custom-control-input" id="customControlValidation2" name="radio-stacked" required>
    <label class="custom-control-label" for="customControlValidation2">Toggle this custom radio</label>
  </div>
  <div class="custom-control custom-radio mb-3">
    <input type="radio" class="custom-control-input" id="customControlValidation3" name="radio-stacked" required>
    <label class="custom-control-label" for="customControlValidation3">Or toggle this other custom radio</label>
    <div class="invalid-feedback">More example invalid feedback text</div>
  </div>

  <div class="mb-3">
    <select class="custom-select" required>
      <option value="">Choose...</option>
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
    <div class="invalid-feedback">Example invalid custom select feedback</div>
  </div>

  <div class="custom-file mb-3">
    <input type="file" class="custom-file-input" id="validatedCustomFile" required>
    <label class="custom-file-label" for="validatedCustomFile">Choose file...</label>
    <div class="invalid-feedback">Example invalid custom file feedback</div>
  </div>

  <div class="mb-3">
    <div class="input-group is-invalid">
      <div class="input-group-prepend">
        <span class="input-group-text" id="validatedInputGroupPrepend">@</span>
      </div>
      <input type="text" class="form-control is-invalid" aria-describedby="validatedInputGroupPrepend" required>
    </div>
    <div class="invalid-feedback">
      Example invalid input group feedback
    </div>
  </div>

  <div class="mb-3">
    <div class="input-group is-invalid">
      <div class="input-group-prepend">
        <label class="input-group-text" for="validatedInputGroupSelect">Options</label>
      </div>
      <select class="custom-select" id="validatedInputGroupSelect" required>
        <option value="">Choose...</option>
        <option value="1">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
      </select>
    </div>
    <div class="invalid-feedback">
      Example invalid input group feedback
    </div>
  </div>

  <div class="input-group is-invalid">
    <div class="custom-file">
      <input type="file" class="custom-file-input" id="validatedInputGroupCustomFile" required>
      <label class="custom-file-label" for="validatedInputGroupCustomFile">Choose file...</label>
    </div>
    <div class="input-group-append">
       <button class="btn btn-outline-secondary" type="button">Button</button>
    </div>
  </div>
  <div class="invalid-feedback">
    Example invalid input group feedback
  </div>
</form>

Awọn imọran irinṣẹ

Ti iṣeto fọọmu rẹ ba gba laaye, o le paarọ awọn .{valid|invalid}-feedbackkilasi fun .{valid|invalid}-tooltipawọn kilasi lati ṣe afihan awọn esi afọwọsi ni ilana irinṣẹ ara. Rii daju pe o ni obi pẹlu position: relativelori rẹ fun ipo ohun elo. Ni apẹẹrẹ ni isalẹ, awọn kilasi ọwọn wa ti ni eyi tẹlẹ, ṣugbọn iṣẹ akanṣe rẹ le nilo iṣeto yiyan.

Wulẹ dara!
Wulẹ dara!
Jọwọ pese ilu to wulo.
Jọwọ yan ipo to wulo.
Jọwọ pese zip ti o wulo.
<form class="needs-validation" novalidate>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationTooltip01">First name</label>
      <input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
      <div class="valid-tooltip">
        Looks good!
      </div>
    </div>
    <div class="col-md-6 mb-3">
      <label for="validationTooltip02">Last name</label>
      <input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
      <div class="valid-tooltip">
        Looks good!
      </div>
    </div>
  </div>
  <div class="form-row">
    <div class="col-md-6 mb-3">
      <label for="validationTooltip03">City</label>
      <input type="text" class="form-control" id="validationTooltip03" required>
      <div class="invalid-tooltip">
        Please provide a valid city.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationTooltip04">State</label>
      <select class="custom-select" id="validationTooltip04" required>
        <option selected disabled value="">Choose...</option>
        <option>...</option>
      </select>
      <div class="invalid-tooltip">
        Please select a valid state.
      </div>
    </div>
    <div class="col-md-3 mb-3">
      <label for="validationTooltip05">Zip</label>
      <input type="text" class="form-control" id="validationTooltip05" required>
      <div class="invalid-tooltip">
        Please provide a valid zip.
      </div>
    </div>
  </div>
  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

Isọdi ara ẹni

Awọn ipinlẹ afọwọsi le jẹ adani nipasẹ Sass pẹlu $form-validation-statesmaapu naa. Ti o wa ninu _variables.scssfaili wa, maapu Sass yii ti wa ni yipo lati ṣe ipilẹṣẹ aiyipada valid/ invalidawọn ipinlẹ afọwọsi. To wa ni maapu oni ite kan fun isọdi awọ ati aami ipinlẹ kọọkan. Lakoko ti ko si awọn ipinlẹ miiran ti o ṣe atilẹyin nipasẹ awọn aṣawakiri, awọn ti nlo awọn aṣa aṣa le ṣafikun awọn abajade fọọmu eka diẹ sii ni irọrun.

Jọwọ ṣe akiyesi pe a ko ṣeduro isọdi awọn iye wọnyi laisi tun ṣe atunṣe form-validation-statemixin naa.

// Sass map from `_variables.scss`
// Override this and recompile your Sass to generate different states
$form-validation-states: map-merge(
  (
    "valid": (
      "color": $form-feedback-valid-color,
      "icon": $form-feedback-icon-valid
    ),
    "invalid": (
      "color": $form-feedback-invalid-color,
      "icon": $form-feedback-icon-invalid
    )
  ),
  $form-validation-states
);

// Loop from `_forms.scss`
// Any modifications to the above Sass map will be reflected in your compiled
// CSS via this loop.
@each $state, $data in $form-validation-states {
  @include form-validation-state($state, map-get($data, color), map-get($data, icon));
}

Afọwọsi ẹgbẹ igbewọle

Lati ṣawari kini awọn eroja nilo awọn igun yika inu ẹgbẹ titẹ sii pẹlu afọwọsi, ẹgbẹ titẹ sii nilo .has-validationkilasi afikun.

<div class="input-group has-validation">
  <div class="input-group-prepend">
    <span class="input-group-text">@</span>
  </div>
  <input type="text" class="form-control" required>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>
@
Jọwọ yan orukọ olumulo kan.

Awọn fọọmu aṣa

Fun paapaa isọdi diẹ sii ati aitasera aṣawakiri aṣawakiri, lo awọn eroja fọọmu aṣa wa patapata lati rọpo awọn aṣiṣe aṣawakiri. Wọn ti kọ si oke ti atunmọ ati isamisi iraye si, nitorinaa wọn jẹ awọn rirọpo to lagbara fun iṣakoso fọọmu aiyipada eyikeyi.

Awọn apoti ayẹwo ati awọn redio

Apoti ayẹwo kọọkan ati redio <input>ati <label>sisopọ pọ ni a <div>lati ṣẹda iṣakoso aṣa wa. Ni igbekalẹ, eyi jẹ ọna kanna bi aiyipada wa .form-check.

A lo oluyan arakunrin ( ~) fun gbogbo awọn <input>ipinlẹ wa—bii :checked— lati ṣe aṣa atọka fọọmu aṣa wa daradara. Nigbati a ba ni idapo pẹlu .custom-control-labelkilasi, a tun le ṣe ara ọrọ fun ohun kọọkan ti o da lori ipo <input>'s.

A tọju aiyipada <input>pẹlu opacityati lo .custom-control-labellati kọ atọka fọọmu aṣa tuntun ni aaye rẹ pẹlu ::beforeati ::after. Laanu a ko le kọ aṣa kan <input>nitori pe CSS contentko ṣiṣẹ lori nkan yẹn.

Ni awọn ipinlẹ ti a ṣayẹwo, a lo awọn aami SVG ti ipilẹ64 lati Ṣii Aami . Eyi n pese wa iṣakoso ti o dara julọ fun iselona ati ipo kọja awọn aṣawakiri ati awọn ẹrọ.

Awọn apoti ayẹwo

<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="customCheck1">
  <label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>

Awọn apoti ayẹwo aṣa tun le lo :indeterminatekilasi pseudo nigbati a ṣeto pẹlu ọwọ nipasẹ JavaScript (ko si ẹya HTML ti o wa fun sisọ rẹ).

Ti o ba nlo jQuery, iru eyi yẹ ki o to:

$('.your-checkbox').prop('indeterminate', true)

Redio

<div class="custom-control custom-radio">
  <input type="radio" id="customRadio1" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio1">Toggle this custom radio</label>
</div>
<div class="custom-control custom-radio">
  <input type="radio" id="customRadio2" name="customRadio" class="custom-control-input">
  <label class="custom-control-label" for="customRadio2">Or toggle this other custom radio</label>
</div>

Ni tito

<div class="custom-control custom-radio custom-control-inline">
  <input type="radio" id="customRadioInline1" name="customRadioInline" class="custom-control-input">
  <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
  <input type="radio" id="customRadioInline2" name="customRadioInline" class="custom-control-input">
  <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
</div>

Alaabo

Awọn apoti ayẹwo aṣa ati awọn redio tun le jẹ alaabo. Ṣafikun disabledabuda Boolean si <input>ati atọka aṣa ati apejuwe aami yoo jẹ aṣa laifọwọyi.

<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="customCheckDisabled1" disabled>
  <label class="custom-control-label" for="customCheckDisabled1">Check this custom checkbox</label>
</div>

<div class="custom-control custom-radio">
  <input type="radio" name="radioDisabled" id="customRadioDisabled2" class="custom-control-input" disabled>
  <label class="custom-control-label" for="customRadioDisabled2">Toggle this custom radio</label>
</div>

Yipada

Iyipada kan ni isamisi ti apoti ayẹwo aṣa ṣugbọn o nlo .custom-switchkilasi lati ṣe iyipada iyipada kan. Awọn iyipada tun ṣe atilẹyin abuda naa disabled.

<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" id="customSwitch1">
  <label class="custom-control-label" for="customSwitch1">Toggle this switch element</label>
</div>
<div class="custom-control custom-switch">
  <input type="checkbox" class="custom-control-input" disabled id="customSwitch2">
  <label class="custom-control-label" for="customSwitch2">Disabled switch element</label>
</div>

Yan akojọ aṣayan

<select>Awọn akojọ aṣayan aṣa nilo kilasi aṣa nikan, .custom-selectlati ṣe okunfa awọn aṣa aṣa. Aṣa ara wa ni opin si awọn <select>'s ibẹrẹ hihan ati ki o ko ba le yi awọn <option>s nitori browser idiwọn.

<select class="custom-select">
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

O tun le yan lati awọn yiyan aṣa kekere ati nla lati baamu awọn igbewọle ọrọ ti o ni iwọn kanna.

<select class="custom-select custom-select-lg mb-3">
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

<select class="custom-select custom-select-sm">
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

Ẹya multiplenaa tun ni atilẹyin:

<select class="custom-select" multiple>
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

Gẹgẹ bi sizeabuda naa:

<select class="custom-select" size="3">
  <option selected>Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

Ibiti o

Ṣẹda awọn <input type="range">iṣakoso aṣa pẹlu .custom-range. Orin naa (lẹhin) ati atanpako (iye) jẹ aṣa mejeeji lati han kanna kọja awọn aṣawakiri. Bii IE ati Firefox nikan ṣe atilẹyin “kikun” orin wọn lati apa osi tabi ọtun ti atanpako bi ọna lati ṣe afihan ilọsiwaju ni wiwo, a ko ṣe atilẹyin lọwọlọwọ.

<label for="customRange1">Example range</label>
<input type="range" class="custom-range" id="customRange1">

Awọn igbewọle ibiti o ni awọn iye ti ko ṣoki fun minati max- 0ati 100, lẹsẹsẹ. O le pato awọn iye titun fun awọn ti nlo minati awọn maxabuda.

<label for="customRange2">Example range</label>
<input type="range" class="custom-range" min="0" max="5" id="customRange2">

Nipa aiyipada, awọn igbewọle ibiti “fifẹ” si awọn iye odidi. Lati yi eyi pada, o le pato stepiye kan. Ni apẹẹrẹ ni isalẹ, a ṣe ilọpo meji nọmba awọn igbesẹ nipasẹ lilo step="0.5".

<label for="customRange3">Example range</label>
<input type="range" class="custom-range" min="0" max="5" step="0.5" id="customRange3">

Aṣàwákiri faili

Ohun itanna ti a ṣeduro lati ṣe agbewọle igbewọle aṣa aṣa: bs-custom-file-input , iyẹn ni ohun ti a nlo lọwọlọwọ nibi ninu awọn docs wa.

Iṣagbewọle faili jẹ gnarly pupọ julọ ti opo naa ati nilo afikun JavaScript ti o ba fẹ lati so wọn pọ pẹlu iṣẹ-ṣiṣe Yan faili… ati ọrọ orukọ faili ti o yan.

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFile">
  <label class="custom-file-label" for="customFile">Choose file</label>
</div>

A tọju faili aiyipada <input>nipasẹ opacityati dipo ara <label>. Bọtini naa wa ni ipilẹṣẹ ati ipo pẹlu ::after. Nikẹhin, a kede a widthati heightlori <input>fun aye to dara fun akoonu agbegbe.

Itumọ tabi ṣe akanṣe awọn okun pẹlu SCSS

:lang()Kilasi pseudo ni a lo lati gba laaye fun itumọ ọrọ “Ṣawakiri” si awọn ede miiran. Paarẹ tabi ṣafikun awọn titẹ sii si $custom-file-textoniyipada Sass pẹlu aami ede ti o baamu ati awọn okun agbegbe. Awọn gbolohun ọrọ Gẹẹsi le ṣe adani ni ọna kanna. Fun apẹẹrẹ, eyi ni bii eniyan ṣe le ṣafikun itumọ ede Sipeeni kan (koodu ede Spani jẹ es):

$custom-file-text: (
  en: "Browse",
  es: "Elegir"
);

Eyi wa lang(es)ni iṣe lori iṣagbewọle faili aṣa fun itumọ ede Sipeeni kan:

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFileLang" lang="es">
  <label class="custom-file-label" for="customFileLang">Seleccionar Archivo</label>
</div>

Iwọ yoo nilo lati ṣeto ede ti iwe-ipamọ rẹ (tabi abẹlẹ rẹ) ni deede ki ọrọ to pe yoo han. Eyi le ṣee ṣe nipa lilo abudalang lori eroja<html> tabi Content-Languageakọsori HTTP , laarin awọn ọna miiran.

Itumọ tabi ṣe akanṣe awọn okun pẹlu HTML

Bootstrap tun pese ọna lati tumọ ọrọ “Ṣawari” ni HTML pẹlu data-browseabuda ti o le ṣafikun si aami titẹ sii (apẹẹrẹ ni Dutch):

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFileLangHTML">
  <label class="custom-file-label" for="customFileLangHTML" data-browse="Bestand kiezen">Voeg je document toe</label>
</div>