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