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.

564 lines
16 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="example">
  111. <h3 class="ui header">Validation with Message</h3>
  112. <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>
  113. <div class="info message">The template for error messages can be modified by adjusting settings.template.error</div>
  114. <div class="ui form segment">
  115. <p>Let's go ahead and get you signed up.</p>
  116. <div class="two fields">
  117. <div class="field">
  118. <label>First Name</label>
  119. <input placeholder="First Name" name="first-name" type="text">
  120. </div>
  121. <div class="field">
  122. <label>Last Name</label>
  123. <input placeholder="Last Name" name="last-name" type="text">
  124. </div>
  125. </div>
  126. <div class="field">
  127. <label>Username</label>
  128. <input placeholder="Username" name="username" type="text">
  129. </div>
  130. <div class="field">
  131. <label>Password</label>
  132. <input type="password" name="password">
  133. </div>
  134. <div class="inline field">
  135. <div class="ui checkbox">
  136. <input type="checkbox" name="terms" />
  137. <label>I agree to the Terms and Conditions</label>
  138. </div>
  139. </div>
  140. <div class="ui blue submit button">Submit</div>
  141. <div class="ui error message"></div>
  142. </div>
  143. </div>
  144. <div class="dog example">
  145. <h3 class="ui header">Custom Validation</h3>
  146. <p>Custom form validation requires passing in a validation object with the rules required to validate your form.</p>
  147. <div class="ignore code">
  148. $('.ui.form')
  149. .form({
  150. dog: {
  151. identifier: 'dog',
  152. rules: [
  153. {
  154. type: 'empty',
  155. prompt: 'You must have a dog to add'
  156. },
  157. {
  158. type: 'contains[fluffy]',
  159. prompt: 'I only want you to add fluffy dogs!'
  160. },
  161. {
  162. type: 'not[mean]',
  163. prompt: 'Why would you add a mean dog to the list?'
  164. }
  165. ]
  166. }
  167. })
  168. ;
  169. </div>
  170. <div class="ui form segment">
  171. <p>Let's go ahead and get you signed up.</p>
  172. <div class="field">
  173. <label>Dog</label>
  174. <input placeholder="Dog" name="dog" type="text">
  175. </div>
  176. <div class="ui blue submit button">Add Dog <i class="add icon"></i></div>
  177. <div class="ui error message"></div>
  178. </div>
  179. </div>
  180. <div class="inline example">
  181. <h3 class="ui header">Inline Validation and Validation Events</h3>
  182. <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>
  183. <p>This example also uses a different validation event. Each element will be validated on input blur instead of the default form submit.</p>
  184. <div class="code" data-type="javascript">
  185. $('.ui.dropdown')
  186. .form(validationRules, {
  187. inline : true,
  188. on : 'blur'
  189. })
  190. ;
  191. </div>
  192. <div class="ui form segment">
  193. <p>Let's go ahead and get you signed up.</p>
  194. <div class="two fields">
  195. <div class="field">
  196. <label>First Name</label>
  197. <input placeholder="First Name" name="first-name" type="text">
  198. </div>
  199. <div class="field">
  200. <label>Last Name</label>
  201. <input placeholder="Last Name" name="last-name" type="text">
  202. </div>
  203. </div>
  204. <div class="field">
  205. <label>Username</label>
  206. <input placeholder="Username" name="username" type="text">
  207. </div>
  208. <div class="field">
  209. <label>Password</label>
  210. <input type="password" name="password">
  211. </div>
  212. <div class="inline field">
  213. <div class="ui checkbox">
  214. <input type="checkbox" name="terms" />
  215. <label>I agree to the Terms and Conditions</label>
  216. </div>
  217. </div>
  218. <div class="ui blue submit button">Submit</div>
  219. </div>
  220. </div>
  221. <h2 class="ui dividing header">Behavior</h2>
  222. All the following <a href="/module.html#/behavior">behaviors</a> can be called using the syntax <code>$('.foo').form('behavior name', argumentOne, argumentTwo)</code>
  223. <table class="ui definition celled table segment">
  224. <tr>
  225. <td>submit</td>
  226. <td>Submits selected form</td>
  227. </tr>
  228. <tr>
  229. <td>validate form</td>
  230. <td>Validates form and calls onSuccess or onFailure</td>
  231. </tr>
  232. <tr>
  233. <td>get change event</td>
  234. <td>gets browser property change event</td>
  235. </tr>
  236. <tr>
  237. <td>get field(id)</td>
  238. <td>Returns element with matching name, id, or data-validate metadata to ID</td>
  239. </tr>
  240. <tr>
  241. <td>get validation(element)</td>
  242. <td>Returns validation rules for a given field</td>
  243. </tr>
  244. <tr>
  245. <td>has field(identifier)</td>
  246. <td>Returns whether a field exists</td>
  247. </tr>
  248. <tr>
  249. <td>add errors(errors)</td>
  250. <td>Adds errors to form, given an array errors</td>
  251. </tr>
  252. </table>
  253. <h2 class="ui dividing header">Settings</h2>
  254. <h3 class="ui header">
  255. Form Settings
  256. <div class="sub header">Form settings modify the form validation behavior</div>
  257. </h3>
  258. <table class="ui red celled sortable definition table segment">
  259. <thead>
  260. <th>Setting</th>
  261. <th class="four wide">Default</th>
  262. <th>Description</th>
  263. </thead>
  264. <tbody>
  265. <tr>
  266. <td>keyboardShortcuts</td>
  267. <td>true</td>
  268. <td>Adds keyboard shortcuts for enter and escape keys to submit form and blur fields respectively</td>
  269. </tr>
  270. <tr>
  271. <td>on</td>
  272. <td>submit</td>
  273. <td>Event used to trigger validation. Can be either <b>submit</b>, <b>blur</b> or <b>change</b>.</td>
  274. </tr>
  275. <tr>
  276. <td>revalidate</td>
  277. <td>true</td>
  278. <td>If set to true will revalidate fields with errors on input change</td>
  279. </tr>
  280. <tr>
  281. <td>delay</td>
  282. <td>true</td>
  283. <td>Delay from last typed letter to validate a field when using <code>on: change</code> or when revalidating a field.</td>
  284. </tr>
  285. <tr>
  286. <td>inline</td>
  287. <td>false</td>
  288. <td>Adds inline error on field validation error</td>
  289. </tr>
  290. <tr>
  291. <td>transition</td>
  292. <td>
  293. scale
  294. </td>
  295. <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>
  296. </tr>
  297. <tr>
  298. <td>duration</td>
  299. <td>150</td>
  300. <td>Animation speed for inline prompt</td>
  301. </tr>
  302. </tbody>
  303. </table>
  304. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  305. <h3 class="ui header">
  306. Validation Rules
  307. <div class="sub header">Validation rules are a set of conditions required to validate a field</div>
  308. </h3>
  309. <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>
  310. <table class="ui teal celled sortable definition table segment">
  311. <thead>
  312. <th class="four wide">Name</th>
  313. <th>Arguments</th>
  314. <th>Description</th>
  315. </thead>
  316. <tbody>
  317. <tr>
  318. <td>empty</td>
  319. <td>value</td>
  320. <td>Checks whether a field is empty</td>
  321. </tr>
  322. <tr>
  323. <td>email</td>
  324. <td>value</td>
  325. <td>Checks whether a field is a valid email address</td>
  326. </tr>
  327. <tr>
  328. <td>length</td>
  329. <td>value</td>
  330. <td>Checks whether a field is longer than a length</td>
  331. </tr>
  332. <tr>
  333. <td>not</td>
  334. <td>value, notValue</td>
  335. <td>Checks whether a field is not a value</td>
  336. </tr>
  337. <tr>
  338. <td>contains</td>
  339. <td>value, text</td>
  340. <td>Checks whether a field contains text</td>
  341. </tr>
  342. <tr>
  343. <td>is</td>
  344. <td>value, text</td>
  345. <td>Checks whether a field matches a value</td>
  346. </tr>
  347. <tr>
  348. <td>maxLength</td>
  349. <td>value</td>
  350. <td>Checks whether a field is less than a max length</td>
  351. </tr>
  352. <tr>
  353. <td>match</td>
  354. <td>value, fieldIdentifier</td>
  355. <td>Checks whether a field matches another field</td>
  356. </tr>
  357. <tr>
  358. <td>url</td>
  359. <td>value</td>
  360. <td>Checks whether a field is a url</td>
  361. </tr>
  362. <tr>
  363. <td>checked</td>
  364. <td>-</td>
  365. <td>Checks whether a checkbox field is checked</td>
  366. </tr>
  367. </tbody>
  368. </table>
  369. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  370. <h3 class="ui header">
  371. Callbacks
  372. <div class="sub header">Callbacks specify a function to occur after a specific behavior.</div>
  373. </h3>
  374. <table class="ui celled green definition table segment">
  375. <thead>
  376. <th class="four wide">Setting</th>
  377. <th>Context</th>
  378. <th>Description</th>
  379. </thead>
  380. <tbody>
  381. <tr>
  382. <td>onValid</td>
  383. <td>field</td>
  384. <td>Callback on each valid field</td>
  385. </tr>
  386. <tr>
  387. <td>onInvalid</td>
  388. <td>field</td>
  389. <td>Callback on each invalid field</td>
  390. </tr>
  391. <tr>
  392. <td>onSuccess</td>
  393. <td>form</td>
  394. <td>Callback if a form is all valid</td>
  395. </tr>
  396. <tr>
  397. <td>onFailure</td>
  398. <td>form</td>
  399. <td>Callback if any form field is invalid</td>
  400. </tr>
  401. </tbody>
  402. </table>
  403. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  404. <h3 class="ui header">
  405. Templates
  406. <div class="sub header">Templates are used to construct elements</div>
  407. </h3>
  408. <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>
  409. <table class="ui celled blue definition table segment">
  410. <thead>
  411. <th class="four wide">Template</th>
  412. <th>Arguments</th>
  413. <th>Description</th>
  414. </thead>
  415. <tbody>
  416. <tr>
  417. <td>error</td>
  418. <td>Errors (Array)</td>
  419. <td>Constructs the contents of an error message</td>
  420. </tr>
  421. <tr>
  422. <td>prompt</td>
  423. <td>Errors (Array)</td>
  424. <td>Constructs an element to prompt the user to an invalid field</td>
  425. </tr>
  426. </tbody>
  427. </table>
  428. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  429. <h3 class="ui header">
  430. DOM Settings
  431. <div class="sub header">DOM settings specify how this module should interface with the DOM</div>
  432. </h3>
  433. <table class="ui celled purple definition table segment">
  434. <thead>
  435. <th>Setting</th>
  436. <th class="six wide">Default</th>
  437. <th>Description</th>
  438. </thead>
  439. <tbody>
  440. <tr>
  441. <td>namespace</td>
  442. <td>form</td>
  443. <td>Event namespace. Makes sure module teardown does not effect other events attached to an element.</td>
  444. </tr>
  445. <tr>
  446. <td>selector</td>
  447. <td>
  448. <div class="code">
  449. selector : {
  450. message : '.error.message',
  451. field : 'input, textarea, select',
  452. group : '.field',
  453. input : 'input',
  454. prompt : '.prompt',
  455. submit : '.submit'
  456. }
  457. </div>
  458. </td>
  459. <td>Selectors used to match functionality to DOM</td>
  460. </tr>
  461. <tr>
  462. <td>metadata</td>
  463. <td>
  464. <div class="code">
  465. metadata : {
  466. validate: 'validate'
  467. },
  468. </div>
  469. </td>
  470. <td>
  471. HTML5 metadata attributes
  472. </td>
  473. </tr>
  474. <tr>
  475. <td>className</td>
  476. <td>
  477. <div class="code">
  478. className : {
  479. active : 'active',
  480. placeholder : 'default',
  481. disabled : 'disabled',
  482. visible : 'visible'
  483. }
  484. </div>
  485. </td>
  486. <td>Class names used to attach style to state</td>
  487. </tr>
  488. </tbody>
  489. </table>
  490. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  491. <h3 class="ui header">
  492. Debug Settings
  493. <div class="sub header">Debug settings controls debug output to the console</div>
  494. </h3>
  495. <table class="ui blue celled sortable definition table segment">
  496. <thead>
  497. <th>Setting</th>
  498. <th class="four wide">Default</th>
  499. <th>Description</th>
  500. </thead>
  501. <tbody>
  502. <tr>
  503. <td>name</td>
  504. <td>Form</td>
  505. <td>Name used in debug logs</td>
  506. </tr>
  507. <tr>
  508. <td>debug</td>
  509. <td>True</td>
  510. <td>Provides standard debug output to console</td>
  511. </tr>
  512. <tr>
  513. <td>performance</td>
  514. <td>True</td>
  515. <td>Provides standard debug output to console</td>
  516. </tr>
  517. <tr>
  518. <td>verbose</td>
  519. <td>True</td>
  520. <td>Provides ancillary debug output to console</td>
  521. </tr>
  522. <tr>
  523. <td>errors</td>
  524. <td colspan="2">
  525. <div class="code">
  526. errors : {
  527. method : 'The method you called is not defined.'
  528. }
  529. </div>
  530. </td>
  531. </tr>
  532. </tbody>
  533. </table>
  534. </div>