Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new validation framework and an example in Features. (#1769)
* Adds a new validation framework and an example in Features. - Using the new validation, we can apply form validation to all modals and other places. - The Features has been updated to have an example of how to use this new validation including a new validation.js with the common rules for add/edit modals. - There is still work to be done in the handling of updates and submissions but this should get us close enough for now. * Removes debugging statement * Removes old data modal handler * Preserves the original onfocusout functionality. * Adds documentation and usage example * Fixes linting * Fixes linting II...the relintining * Fixes linting again..
- Loading branch information
1 parent
093d000
commit f945d83
Showing
7 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import $ from 'jquery'; | ||
import ko from 'knockout'; | ||
import 'jquery-validation'; | ||
|
||
/** | ||
* Binds a form element to use jQuery validation tools | ||
* The argument should be a an object of settings from jQuery Validate. | ||
* | ||
* jQuery Validation Docs: | ||
* https://jqueryvalidation.org/validate/ | ||
* | ||
* Usage Example | ||
* ------------- | ||
* <form class="form-horizontal" data-bind="validate: validationSettings"> | ||
* <!-- Inputs in a validated form must have a name. --> | ||
* <input name="name" value="" type="text" /> | ||
* <!-- Either buttons or input[type=submit] works fine.. --> | ||
* <input type="submit" value="Add" /> | ||
* </form> | ||
* | ||
*/ | ||
ko.bindingHandlers.validate = { | ||
init: (element, valueAccessor) => { | ||
const value = valueAccessor(); | ||
|
||
// Add a new handler callback that adds the form as a param for | ||
// checking overall validity. | ||
// Preserve the old onfocusout in case it was being used. | ||
value.ogonfocusout = value.onfocusout; | ||
value.onfocusout = (input, event) => { | ||
if (value.updateHandler) { | ||
value.updateHandler($(element), input); | ||
} | ||
if (value.ogonfocusout) { | ||
value.ogonfocusout(input, event); | ||
} | ||
}; | ||
|
||
$(element).validate(ko.unwrap(value)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import 'bin/knockout-validation'; | ||
import { | ||
AuthenticationServiceManager, | ||
ChatServiceManager, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/charactersheet/viewmodels/character/features/validation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default { | ||
rules: { | ||
name: { | ||
required: true | ||
} | ||
}, | ||
messages: { | ||
// name: "Please give me your name...", | ||
} | ||
}; |