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.

551 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 behaviors 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 maching 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 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 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 empty</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. </tbody>
  353. </table>
  354. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  355. <h3 class="ui header">
  356. Callbacks
  357. <div class="sub header">Callback settings specify a function to occur after a specific behavior.</div>
  358. </h3>
  359. <table class="ui celled green definition table segment">
  360. <thead>
  361. <th class="four wide">Setting</th>
  362. <th>Context</th>
  363. <th>Description</th>
  364. </thead>
  365. <tbody>
  366. <tr>
  367. <td>onValid</td>
  368. <td>field</td>
  369. <td>Callback on each valid field</td>
  370. </tr>
  371. <tr>
  372. <td>onInvalid</td>
  373. <td>field</td>
  374. <td>Callback on each invalid field</td>
  375. </tr>
  376. <tr>
  377. <td>onSuccess</td>
  378. <td>form</td>
  379. <td>Callback if a form is all valid</td>
  380. </tr>
  381. <tr>
  382. <td>onFailure</td>
  383. <td>form</td>
  384. <td>Callback if any form field is invalid</td>
  385. </tr>
  386. </tbody>
  387. </table>
  388. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  389. <h3 class="ui header">
  390. Templates
  391. <div class="sub header">Templates are used to construct elements</div>
  392. </h3>
  393. <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>
  394. <table class="ui celled blue definition table segment">
  395. <thead>
  396. <th class="four wide">Template</th>
  397. <th>Arguments</th>
  398. <th>Description</th>
  399. </thead>
  400. <tbody>
  401. <tr>
  402. <td>error</td>
  403. <td>Errors (Array)</td>
  404. <td>Constructs the contents of an error message</td>
  405. </tr>
  406. <tr>
  407. <td>prompt</td>
  408. <td>Errors (Array)</td>
  409. <td>Constructs an element to prompt the user to an invalid field</td>
  410. </tr>
  411. </tbody>
  412. </table>
  413. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  414. <h3 class="ui header">
  415. DOM Settings
  416. <div class="sub header">DOM settings specify how this module should interface with the DOM</div>
  417. </h3>
  418. <table class="ui celled purple definition table segment">
  419. <thead>
  420. <th>Setting</th>
  421. <th class="six wide">Default</th>
  422. <th>Description</th>
  423. </thead>
  424. <tbody>
  425. <tr>
  426. <td>namespace</td>
  427. <td>form</td>
  428. <td>Event namespace. Makes sure module teardown does not effect other events attached to an element.</td>
  429. </tr>
  430. <tr>
  431. <td>selector</td>
  432. <td>
  433. <div class="code">
  434. selector : {
  435. message : '.error.message',
  436. field : 'input, textarea, select',
  437. group : '.field',
  438. input : 'input',
  439. prompt : '.prompt',
  440. submit : '.submit'
  441. }
  442. </div>
  443. </td>
  444. <td>Selectors used to match functionality to DOM</td>
  445. </tr>
  446. <tr>
  447. <td>metadata</td>
  448. <td>
  449. <div class="code">
  450. metadata : {
  451. validate: 'validate'
  452. },
  453. </div>
  454. </td>
  455. <td>
  456. HTML5 metadata attributes
  457. </td>
  458. </tr>
  459. <tr>
  460. <td>className</td>
  461. <td>
  462. <div class="code">
  463. className : {
  464. active : 'active',
  465. placeholder : 'default',
  466. disabled : 'disabled',
  467. visible : 'visible'
  468. }
  469. </div>
  470. </td>
  471. <td>Class names used to attach style to state</td>
  472. </tr>
  473. </tbody>
  474. </table>
  475. <div class="ui horizontal divider"><i class="icon setting"></i></div>
  476. <h3 class="ui header">
  477. Debug Settings
  478. <div class="sub header">Debug settings controls debug output to the console</div>
  479. </h3>
  480. <table class="ui blue celled definition table segment">
  481. <thead>
  482. <th>Setting</th>
  483. <th class="four wide">Default</th>
  484. <th>Description</th>
  485. </thead>
  486. <tbody>
  487. <tr>
  488. <td>name</td>
  489. <td>Form</td>
  490. <td>Name used in debug logs</td>
  491. </tr>
  492. <tr>
  493. <td>debug</td>
  494. <td>True</td>
  495. <td>Provides standard debug output to console</td>
  496. </tr>
  497. <tr>
  498. <td>performance</td>
  499. <td>True</td>
  500. <td>Provides standard debug output to console</td>
  501. </tr>
  502. <tr>
  503. <td>verbose</td>
  504. <td>True</td>
  505. <td>Provides ancillary debug output to console</td>
  506. </tr>
  507. <tr>
  508. <td>errors</td>
  509. <td colspan="2">
  510. <div class="code">
  511. errors : {
  512. method : 'The method you called is not defined.'
  513. }
  514. </div>
  515. </td>
  516. </tr>
  517. </tbody>
  518. </table>
  519. </div>
  520. </body>
  521. </html>