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.

635 lines
20 KiB

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