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.

554 lines
16 KiB

  1. ---
  2. layout : 'default'
  3. css : 'form'
  4. title : 'Validate Form'
  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 w/ 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>inline</td>
  277. <td>false</td>
  278. <td>Adds inline error on field validation error</td>
  279. </tr>
  280. <tr>
  281. <td>transition</td>
  282. <td>
  283. scale
  284. </td>
  285. <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>
  286. </tr>
  287. <tr>
  288. <td>duration</td>
  289. <td>150</td>
  290. <td>Animation speed for inline prompt</td>
  291. </tr>
  292. </tbody>
  293. </table>
  294. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  295. <h3 class="ui header">
  296. Validation Rules
  297. <div class="sub header">Validation rules are a set of conditions required to validate a field</div>
  298. </h3>
  299. <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>
  300. <table class="ui teal celled sortable definition table segment">
  301. <thead>
  302. <th class="four wide">Name</th>
  303. <th>Arguments</th>
  304. <th>Description</th>
  305. </thead>
  306. <tbody>
  307. <tr>
  308. <td>empty</td>
  309. <td>value</td>
  310. <td>Checks whether a field is empty</td>
  311. </tr>
  312. <tr>
  313. <td>email</td>
  314. <td>value</td>
  315. <td>Checks whether a field is a valid email address</td>
  316. </tr>
  317. <tr>
  318. <td>length</td>
  319. <td>value</td>
  320. <td>Checks whether a field is longer than a length</td>
  321. </tr>
  322. <tr>
  323. <td>not</td>
  324. <td>value, notValue</td>
  325. <td>Checks whether a field is not a value</td>
  326. </tr>
  327. <tr>
  328. <td>contains</td>
  329. <td>value, text</td>
  330. <td>Checks whether a field contains text</td>
  331. </tr>
  332. <tr>
  333. <td>is</td>
  334. <td>value, text</td>
  335. <td>Checks whether a field matches a value</td>
  336. </tr>
  337. <tr>
  338. <td>maxLength</td>
  339. <td>value</td>
  340. <td>Checks whether a field is less than a max length</td>
  341. </tr>
  342. <tr>
  343. <td>match</td>
  344. <td>value, fieldIdentifier</td>
  345. <td>Checks whether a field matches another field</td>
  346. </tr>
  347. <tr>
  348. <td>url</td>
  349. <td>value</td>
  350. <td>Checks whether a field is a url</td>
  351. </tr>
  352. <tr>
  353. <td>checked</td>
  354. <td>-</td>
  355. <td>Checks whether a checkbox field is checked</td>
  356. </tr>
  357. </tbody>
  358. </table>
  359. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  360. <h3 class="ui header">
  361. Callbacks
  362. <div class="sub header">Callbacks specify a function to occur after a specific behavior.</div>
  363. </h3>
  364. <table class="ui celled green definition table segment">
  365. <thead>
  366. <th class="four wide">Setting</th>
  367. <th>Context</th>
  368. <th>Description</th>
  369. </thead>
  370. <tbody>
  371. <tr>
  372. <td>onValid</td>
  373. <td>field</td>
  374. <td>Callback on each valid field</td>
  375. </tr>
  376. <tr>
  377. <td>onInvalid</td>
  378. <td>field</td>
  379. <td>Callback on each invalid field</td>
  380. </tr>
  381. <tr>
  382. <td>onSuccess</td>
  383. <td>form</td>
  384. <td>Callback if a form is all valid</td>
  385. </tr>
  386. <tr>
  387. <td>onFailure</td>
  388. <td>form</td>
  389. <td>Callback if any form field is invalid</td>
  390. </tr>
  391. </tbody>
  392. </table>
  393. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  394. <h3 class="ui header">
  395. Templates
  396. <div class="sub header">Templates are used to construct elements</div>
  397. </h3>
  398. <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>
  399. <table class="ui celled blue definition table segment">
  400. <thead>
  401. <th class="four wide">Template</th>
  402. <th>Arguments</th>
  403. <th>Description</th>
  404. </thead>
  405. <tbody>
  406. <tr>
  407. <td>error</td>
  408. <td>Errors (Array)</td>
  409. <td>Constructs the contents of an error message</td>
  410. </tr>
  411. <tr>
  412. <td>prompt</td>
  413. <td>Errors (Array)</td>
  414. <td>Constructs an element to prompt the user to an invalid field</td>
  415. </tr>
  416. </tbody>
  417. </table>
  418. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  419. <h3 class="ui header">
  420. DOM Settings
  421. <div class="sub header">DOM settings specify how this module should interface with the DOM</div>
  422. </h3>
  423. <table class="ui celled purple definition table segment">
  424. <thead>
  425. <th>Setting</th>
  426. <th class="six wide">Default</th>
  427. <th>Description</th>
  428. </thead>
  429. <tbody>
  430. <tr>
  431. <td>namespace</td>
  432. <td>form</td>
  433. <td>Event namespace. Makes sure module teardown does not effect other events attached to an element.</td>
  434. </tr>
  435. <tr>
  436. <td>selector</td>
  437. <td>
  438. <div class="code">
  439. selector : {
  440. message : '.error.message',
  441. field : 'input, textarea, select',
  442. group : '.field',
  443. input : 'input',
  444. prompt : '.prompt',
  445. submit : '.submit'
  446. }
  447. </div>
  448. </td>
  449. <td>Selectors used to match functionality to DOM</td>
  450. </tr>
  451. <tr>
  452. <td>metadata</td>
  453. <td>
  454. <div class="code">
  455. metadata : {
  456. validate: 'validate'
  457. },
  458. </div>
  459. </td>
  460. <td>
  461. HTML5 metadata attributes
  462. </td>
  463. </tr>
  464. <tr>
  465. <td>className</td>
  466. <td>
  467. <div class="code">
  468. className : {
  469. active : 'active',
  470. placeholder : 'default',
  471. disabled : 'disabled',
  472. visible : 'visible'
  473. }
  474. </div>
  475. </td>
  476. <td>Class names used to attach style to state</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. Debug Settings
  483. <div class="sub header">Debug settings controls debug output to the console</div>
  484. </h3>
  485. <table class="ui blue celled sortable definition table segment">
  486. <thead>
  487. <th>Setting</th>
  488. <th class="four wide">Default</th>
  489. <th>Description</th>
  490. </thead>
  491. <tbody>
  492. <tr>
  493. <td>name</td>
  494. <td>Form</td>
  495. <td>Name used in debug logs</td>
  496. </tr>
  497. <tr>
  498. <td>debug</td>
  499. <td>True</td>
  500. <td>Provides standard debug output to console</td>
  501. </tr>
  502. <tr>
  503. <td>performance</td>
  504. <td>True</td>
  505. <td>Provides standard debug output to console</td>
  506. </tr>
  507. <tr>
  508. <td>verbose</td>
  509. <td>True</td>
  510. <td>Provides ancillary debug output to console</td>
  511. </tr>
  512. <tr>
  513. <td>errors</td>
  514. <td colspan="2">
  515. <div class="code">
  516. errors : {
  517. method : 'The method you called is not defined.'
  518. }
  519. </div>
  520. </td>
  521. </tr>
  522. </tbody>
  523. </table>
  524. </div>