You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

631 lines
20 KiB

  1. ---
  2. layout : 'default'
  3. css : 'form'
  4. title : 'Form Validation'
  5. description : 'A form validation behavior checks user input data against a set of criteria before passing along the data to the server'
  6. type : 'UI Behavior'
  7. ---
  8. <script src="/build/uncompressed/definitions/behaviors/form.js"></script>
  9. <script src="/javascript/validate-form.js"></script>
  10. <%- @partial('header', { tabs: { usage: 'Usage', examples: 'Examples', settings: 'Settings'} }) %>
  11. <div class="main container">
  12. <div class="peek">
  13. <div class="ui vertical pointing secondary menu">
  14. <a class="active item">Usage</a>
  15. <a class="item">Behavior</a>
  16. <a class="item">Settings</a>
  17. </div>
  18. </div>
  19. <div class="ui active tab" data-tab="usage">
  20. <h2 class="ui dividing header">Usage</h2>
  21. <div class="example">
  22. <h3 class="ui header">Validation Definitions</h3>
  23. <p>Form validation requires passing in a validation object with the rules required to validate your form.</p>
  24. <div class="ui info message">
  25. <i class="help icon"></i>A validation object includes a list of form elements, and rules to validate each against. Fields are matched by either the <code>id</code> tag, <code>name</code> tag, or the <code>data-validate</code> metadata matching the identifier provided in the settings object.
  26. </div>
  27. <div class="ignore code">
  28. $('.ui.form')
  29. .form({
  30. name: {
  31. identifier : 'name',
  32. rules: [
  33. {
  34. type : 'empty',
  35. prompt : 'Please enter your name'
  36. }
  37. ]
  38. },
  39. gender: {
  40. identifier : 'gender',
  41. rules: [
  42. {
  43. type : 'empty',
  44. prompt : 'Please select a gender'
  45. }
  46. ]
  47. },
  48. username: {
  49. identifier : 'username',
  50. rules: [
  51. {
  52. type : 'empty',
  53. prompt : 'Please enter a username'
  54. }
  55. ]
  56. },
  57. password: {
  58. identifier : 'password',
  59. rules: [
  60. {
  61. type : 'empty',
  62. prompt : 'Please enter a password'
  63. },
  64. {
  65. type : 'length[6]',
  66. prompt : 'Your password must be at least 6 characters'
  67. }
  68. ]
  69. }
  70. terms: {
  71. identifier : 'terms',
  72. rules: [
  73. {
  74. type : 'checked',
  75. prompt : 'You must agree to the terms and conditions'
  76. }
  77. ]
  78. }
  79. })
  80. ;
  81. </div>
  82. <div class="ui form segment">
  83. <p>Tell Us About Yourself</p>
  84. <div class="two fields">
  85. <div class="field">
  86. <label>Name</label>
  87. <input placeholder="First Name" name="name" type="text">
  88. </div>
  89. <div class="field">
  90. <label>Gender</label>
  91. <div class="ui selection dropdown">
  92. <input type="hidden" name="gender">
  93. <div class="default text">Gender</div>
  94. <i class="dropdown icon"></i>
  95. <div class="menu">
  96. <div class="item" data-value="male">Male</div>
  97. <div class="item" data-value="female">Female</div>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. <div class="field">
  103. <label>Username</label>
  104. <input placeholder="Username" name="username" type="text">
  105. </div>
  106. <div class="field">
  107. <label>Password</label>
  108. <input type="password" name="password">
  109. </div>
  110. <div class="inline field">
  111. <div class="ui checkbox">
  112. <input type="checkbox" name="terms" />
  113. <label>I agree to the terms and conditions</label>
  114. </div>
  115. </div>
  116. <div class="ui blue submit button">Submit</div>
  117. </div>
  118. </div>
  119. <h3 class="ui header">
  120. Types of Validation Rules
  121. <div class="sub header">Validation rules are a set of conditions required to validate a field</div>
  122. </h3>
  123. <div class="ui info message">Validation rules are found in <code>settings.rules</code>, to add new global validation rules, modify <code>$.fn.form.settings.rules</code> to include your function.</div>
  124. <div class="in red message">To pass parameters to a rule, use bracket notation in your settings object. For example <code>type: 'not[dog]'</code></div>
  125. <table class="ui teal celled sortable definition table">
  126. <thead>
  127. <th class="four wide">Name</th>
  128. <th>Arguments</th>
  129. <th>Description</th>
  130. </thead>
  131. <tbody>
  132. <tr>
  133. <td>empty</td>
  134. <td>value</td>
  135. <td>Checks whether a field is empty</td>
  136. </tr>
  137. <tr>
  138. <td>email</td>
  139. <td>value</td>
  140. <td>Checks whether a field is a valid email address</td>
  141. </tr>
  142. <tr>
  143. <td>length</td>
  144. <td>value</td>
  145. <td>Checks whether a field is longer than a length</td>
  146. </tr>
  147. <tr>
  148. <td>not</td>
  149. <td>value, notValue</td>
  150. <td>Checks whether a field is not a value</td>
  151. </tr>
  152. <tr>
  153. <td>contains</td>
  154. <td>value, text</td>
  155. <td>Checks whether a field contains text</td>
  156. </tr>
  157. <tr>
  158. <td>is</td>
  159. <td>value, text</td>
  160. <td>Checks whether a field matches a value</td>
  161. </tr>
  162. <tr>
  163. <td>maxLength</td>
  164. <td>value</td>
  165. <td>Checks whether a field is less than a max length</td>
  166. </tr>
  167. <tr>
  168. <td>match</td>
  169. <td>value, fieldIdentifier</td>
  170. <td>Checks whether a field matches another field</td>
  171. </tr>
  172. <tr>
  173. <td>url</td>
  174. <td>value</td>
  175. <td>Checks whether a field is a url</td>
  176. </tr>
  177. <tr>
  178. <td>checked</td>
  179. <td>-</td>
  180. <td>Checks whether a checkbox field is checked</td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. </div>
  185. <div class="ui tab" data-tab="examples">
  186. <div class="dropdown example">
  187. <h3 class="ui header">Validating Dropdowns</h3>
  188. <p><a href="/modules/dropdown.html">Dropdowns</a> can also be validated like other form fields. Simply match the validation rule to the hidden input associated with the dropdown</p>
  189. <div class="ignored code">
  190. $('.ui.dropdown')
  191. .dropdown()
  192. ;
  193. $('.ui.form')
  194. .form({
  195. gender: {
  196. identifier : 'gender',
  197. rules: [
  198. {
  199. type : 'empty',
  200. prompt : 'Please enter a gender'
  201. }
  202. ]
  203. },
  204. name: {
  205. identifier : 'name',
  206. rules: [
  207. {
  208. type : 'empty',
  209. prompt : 'Please enter your name'
  210. }
  211. ]
  212. },
  213. })
  214. ;
  215. </div>
  216. <div class="ui form">
  217. <div class="two fields">
  218. <div class="field">
  219. <label>Name</label>
  220. <input type="text" name="name">
  221. </div>
  222. <div class="field">
  223. <label>Gender</label>
  224. <div class="ui fluid selection dropdown">
  225. <input type="hidden" name="gender">
  226. <div class="default text">Gender</div>
  227. <i class="dropdown icon"></i>
  228. <div class="menu">
  229. <div class="item" data-value="1">Male</div>
  230. <div class="item" data-value="0">Female</div>
  231. </div>
  232. </div>
  233. </div>
  234. </div>
  235. <div class="ui blue submit button">Submit</div>
  236. </div>
  237. </div>
  238. <div class="example">
  239. <h3 class="ui header">Displaying Error Messages</h3>
  240. <p>Forms that contain a <a href="/elements/message.html">ui message</a> error block will automatically be filled in with form validation information.</p>
  241. <div class="info message">The template for error messages can be modified by adjusting settings.template.error</div>
  242. <div class="ui form segment">
  243. <p>Let's go ahead and get you signed up.</p>
  244. <div class="two fields">
  245. <div class="field">
  246. <label>First Name</label>
  247. <input placeholder="First Name" name="first-name" type="text">
  248. </div>
  249. <div class="field">
  250. <label>Last Name</label>
  251. <input placeholder="Last Name" name="last-name" type="text">
  252. </div>
  253. </div>
  254. <div class="field">
  255. <label>Username</label>
  256. <input placeholder="Username" name="username" type="text">
  257. </div>
  258. <div class="field">
  259. <label>Password</label>
  260. <input type="password" name="password">
  261. </div>
  262. <div class="inline field">
  263. <div class="ui checkbox">
  264. <input type="checkbox" name="terms" />
  265. <label>I agree to the Terms and Conditions</label>
  266. </div>
  267. </div>
  268. <div class="ui blue submit button">Submit</div>
  269. <div class="ui error message"></div>
  270. </div>
  271. </div>
  272. <div class="inline example">
  273. <h3 class="ui header">Validating on Blur and other Events</h3>
  274. <p>Validation messages can also appear inline. UI Forms automatically format <a href="/elements/label.html">labels</a> with the class name <code>prompt</code>. These validation prompts are also set to appear on input change instead of form submission.</p>
  275. <p>This example also uses a different validation event. Each element will be validated on input blur instead of the default form submit.</p>
  276. <div class="code" data-type="javascript">
  277. $('.ui.dropdown')
  278. .form(validationRules, {
  279. inline : true,
  280. on : 'blur'
  281. })
  282. ;
  283. </div>
  284. <div class="ui form segment">
  285. <p>Let's go ahead and get you signed up.</p>
  286. <div class="two fields">
  287. <div class="field">
  288. <label>First Name</label>
  289. <input placeholder="First Name" name="first-name" type="text">
  290. </div>
  291. <div class="field">
  292. <label>Last Name</label>
  293. <input placeholder="Last Name" name="last-name" type="text">
  294. </div>
  295. </div>
  296. <div class="field">
  297. <label>Username</label>
  298. <input placeholder="Username" name="username" type="text">
  299. </div>
  300. <div class="field">
  301. <label>Password</label>
  302. <input type="password" name="password">
  303. </div>
  304. <div class="inline field">
  305. <div class="ui checkbox">
  306. <input type="checkbox" name="terms" />
  307. <label>I agree to the Terms and Conditions</label>
  308. </div>
  309. </div>
  310. <div class="ui blue submit button">Submit</div>
  311. </div>
  312. </div>
  313. <div class="dog example">
  314. <h3 class="ui header">Creating Custom Validation</h3>
  315. <p>You can use multiple arbitrary rules to validate a form</p>
  316. <div class="ignore code">
  317. $('.ui.form')
  318. .form({
  319. dog: {
  320. identifier: 'dog',
  321. rules: [
  322. {
  323. type: 'empty',
  324. prompt: 'You must have a dog to add'
  325. },
  326. {
  327. type: 'contains[fluffy]',
  328. prompt: 'I only want you to add fluffy dogs!'
  329. },
  330. {
  331. type: 'not[mean]',
  332. prompt: 'Why would you add a mean dog to the list?'
  333. }
  334. ]
  335. }
  336. })
  337. ;
  338. </div>
  339. <div class="ui form segment">
  340. <p>Let's go ahead and get you signed up.</p>
  341. <div class="field">
  342. <label>Dog</label>
  343. <input placeholder="Dog" name="dog" type="text">
  344. </div>
  345. <div class="ui blue submit button">Add Dog <i class="add icon"></i></div>
  346. <div class="ui error message"></div>
  347. </div>
  348. </div>
  349. </div>
  350. <div class="ui tab" data-tab="settings">
  351. <h2 class="ui dividing header">Behavior</h2>
  352. All the following <a href="/module.html#/behavior">behaviors</a> can be called using the syntax <code>$('.foo').form('behavior name', argumentOne, argumentTwo)</code>
  353. <table class="ui definition celled table">
  354. <tr>
  355. <td>submit</td>
  356. <td>Submits selected form</td>
  357. </tr>
  358. <tr>
  359. <td>validate form</td>
  360. <td>Validates form and calls onSuccess or onFailure</td>
  361. </tr>
  362. <tr>
  363. <td>get change event</td>
  364. <td>gets browser property change event</td>
  365. </tr>
  366. <tr>
  367. <td>get field(id)</td>
  368. <td>Returns element with matching name, id, or data-validate metadata to ID</td>
  369. </tr>
  370. <tr>
  371. <td>get validation(element)</td>
  372. <td>Returns validation rules for a given field</td>
  373. </tr>
  374. <tr>
  375. <td>has field(identifier)</td>
  376. <td>Returns whether a field exists</td>
  377. </tr>
  378. <tr>
  379. <td>add errors(errors)</td>
  380. <td>Adds errors to form, given an array errors</td>
  381. </tr>
  382. </table>
  383. <h2 class="ui dividing header">Settings</h2>
  384. <h3 class="ui header">
  385. Form Settings
  386. <div class="sub header">Form settings modify the form validation behavior</div>
  387. </h3>
  388. <table class="ui celled sortable definition table">
  389. <thead>
  390. <th>Setting</th>
  391. <th class="four wide">Default</th>
  392. <th>Description</th>
  393. </thead>
  394. <tbody>
  395. <tr>
  396. <td>keyboardShortcuts</td>
  397. <td>true</td>
  398. <td>Adds keyboard shortcuts for enter and escape keys to submit form and blur fields respectively</td>
  399. </tr>
  400. <tr>
  401. <td>on</td>
  402. <td>submit</td>
  403. <td>Event used to trigger validation. Can be either <b>submit</b>, <b>blur</b> or <b>change</b>.</td>
  404. </tr>
  405. <tr>
  406. <td>revalidate</td>
  407. <td>true</td>
  408. <td>If set to true will revalidate fields with errors on input change</td>
  409. </tr>
  410. <tr>
  411. <td>delay</td>
  412. <td>true</td>
  413. <td>Delay from last typed letter to validate a field when using <code>on: change</code> or when revalidating a field.</td>
  414. </tr>
  415. <tr>
  416. <td>inline</td>
  417. <td>false</td>
  418. <td>Adds inline error on field validation error</td>
  419. </tr>
  420. <tr>
  421. <td>transition</td>
  422. <td>
  423. scale
  424. </td>
  425. <td>Named transition to use when animating validation errors. Fade and slide down are available without including <a href="/modules/transition.html">ui transitions</a></td>
  426. </tr>
  427. <tr>
  428. <td>duration</td>
  429. <td>150</td>
  430. <td>Animation speed for inline prompt</td>
  431. </tr>
  432. </tbody>
  433. </table>
  434. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  435. <h3 class="ui header">
  436. Callbacks
  437. <div class="sub header">Callbacks specify a function to occur after a specific behavior.</div>
  438. </h3>
  439. <table class="ui celled definition table">
  440. <thead>
  441. <th class="four wide">Setting</th>
  442. <th>Context</th>
  443. <th>Description</th>
  444. </thead>
  445. <tbody>
  446. <tr>
  447. <td>onValid</td>
  448. <td>field</td>
  449. <td>Callback on each valid field</td>
  450. </tr>
  451. <tr>
  452. <td>onInvalid</td>
  453. <td>field</td>
  454. <td>Callback on each invalid field</td>
  455. </tr>
  456. <tr>
  457. <td>onSuccess</td>
  458. <td>form</td>
  459. <td>Callback if a form is all valid</td>
  460. </tr>
  461. <tr>
  462. <td>onFailure</td>
  463. <td>form</td>
  464. <td>Callback if any form field is invalid</td>
  465. </tr>
  466. </tbody>
  467. </table>
  468. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  469. <h3 class="ui header">
  470. Templates
  471. <div class="sub header">Templates are used to construct elements</div>
  472. </h3>
  473. <div class="ui info message">Templates are found in <code>settings.template</code>, to modify templates across all forms, modify <code>$.fn.form.settings.templates</code> to include your function. They must return html.</div>
  474. <table class="ui celled definition table">
  475. <thead>
  476. <th class="four wide">Template</th>
  477. <th>Arguments</th>
  478. <th>Description</th>
  479. </thead>
  480. <tbody>
  481. <tr>
  482. <td>error</td>
  483. <td>Errors (Array)</td>
  484. <td>Constructs the contents of an error message</td>
  485. </tr>
  486. <tr>
  487. <td>prompt</td>
  488. <td>Errors (Array)</td>
  489. <td>Constructs an element to prompt the user to an invalid field</td>
  490. </tr>
  491. </tbody>
  492. </table>
  493. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  494. <h3 class="ui header">
  495. DOM Settings
  496. <div class="sub header">DOM settings specify how this module should interface with the DOM</div>
  497. </h3>
  498. <table class="ui celled definition table">
  499. <thead>
  500. <th>Setting</th>
  501. <th class="six wide">Default</th>
  502. <th>Description</th>
  503. </thead>
  504. <tbody>
  505. <tr>
  506. <td>namespace</td>
  507. <td>form</td>
  508. <td>Event namespace. Makes sure module teardown does not effect other events attached to an element.</td>
  509. </tr>
  510. <tr>
  511. <td>selector</td>
  512. <td>
  513. <div class="code">
  514. selector : {
  515. message : '.error.message',
  516. field : 'input, textarea, select',
  517. group : '.field',
  518. input : 'input',
  519. prompt : '.prompt',
  520. submit : '.submit'
  521. }
  522. </div>
  523. </td>
  524. <td>Selectors used to match functionality to DOM</td>
  525. </tr>
  526. <tr>
  527. <td>metadata</td>
  528. <td>
  529. <div class="code">
  530. metadata : {
  531. validate: 'validate'
  532. },
  533. </div>
  534. </td>
  535. <td>
  536. HTML5 metadata attributes
  537. </td>
  538. </tr>
  539. <tr>
  540. <td>className</td>
  541. <td>
  542. <div class="code">
  543. className : {
  544. active : 'active',
  545. placeholder : 'default',
  546. disabled : 'disabled',
  547. visible : 'visible'
  548. }
  549. </div>
  550. </td>
  551. <td>Class names used to attach style to state</td>
  552. </tr>
  553. </tbody>
  554. </table>
  555. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  556. <h3 class="ui header">
  557. Debug Settings
  558. <div class="sub header">Debug settings controls debug output to the console</div>
  559. </h3>
  560. <table class="ui celled sortable definition table">
  561. <thead>
  562. <th>Setting</th>
  563. <th class="four wide">Default</th>
  564. <th>Description</th>
  565. </thead>
  566. <tbody>
  567. <tr>
  568. <td>name</td>
  569. <td>Form</td>
  570. <td>Name used in debug logs</td>
  571. </tr>
  572. <tr>
  573. <td>debug</td>
  574. <td>True</td>
  575. <td>Provides standard debug output to console</td>
  576. </tr>
  577. <tr>
  578. <td>performance</td>
  579. <td>True</td>
  580. <td>Provides standard debug output to console</td>
  581. </tr>
  582. <tr>
  583. <td>verbose</td>
  584. <td>True</td>
  585. <td>Provides ancillary debug output to console</td>
  586. </tr>
  587. <tr>
  588. <td>errors</td>
  589. <td colspan="2">
  590. <div class="code">
  591. errors : {
  592. method : 'The method you called is not defined.'
  593. }
  594. </div>
  595. </td>
  596. </tr>
  597. </tbody>
  598. </table>
  599. </div>
  600. </div>