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.

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