-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP If the model contains errors, display inline errors in jsoneditor #60
Open
germanbisurgi
wants to merge
7
commits into
dmstr:master
Choose a base branch
from
germanbisurgi:feature/show-inline-errors-after-submission
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
124bb87
If the model contains errors, configure the 'show_editor' option in t…
gbisurgi 366d19d
Same applies to widget-translation
gbisurgi 7668207
Added ClientSideJsonValidator. This validator will be use when 'enabl…
gbisurgi 2654767
Added ClientSideJsonValidator rule for WidgetContent and WidgetConten…
gbisurgi b8b0695
Configured template option for json-editor based fields to reduce red…
gbisurgi 7f33fbf
Enabled client validation for widget and widget translation
gbisurgi e3c41e9
Added extra check
gbisurgi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,58 @@ | ||
<?php | ||
|
||
namespace hrzg\widget\validators; | ||
|
||
use yii\validators\Validator; | ||
|
||
class ClientSideJsonValidator extends Validator | ||
{ | ||
public function validateValue($value) | ||
{ | ||
return []; | ||
} | ||
|
||
public function clientValidateAttribute($model, $attribute, $view) | ||
{ | ||
return <<<JS | ||
const editor = window.jsonEditors[0] | ||
|
||
if (!editor) { | ||
return | ||
} | ||
|
||
const errors = editor.validate() | ||
|
||
|
||
// collect error messages for this validator | ||
errors.forEach(function (error) { | ||
const invalidEditor = editor.getEditor(error.path) | ||
const field = invalidEditor.schema.title || error.path | ||
const message = field + ': ' + error.message | ||
messages.push(message) | ||
}) | ||
|
||
// coerce json-editor to display own inline error messages | ||
Object.values(editor.editors).forEach(function (editor) { | ||
editor.is_dirty = true | ||
}) | ||
|
||
editor.root.showValidationErrors(errors) | ||
|
||
// remove the 'has-error' class from editor container. Using * selector because | ||
// the class name can change from model to mode but the attribute in this case | ||
// is always "default_properties_json" | ||
const targetNode = document.querySelector('[class*="default_properties_json"]') | ||
|
||
const observer = new MutationObserver((mutations) => { | ||
mutations.forEach(() => { | ||
if (targetNode.classList.contains('has-error')) { | ||
targetNode.classList.remove('has-error') | ||
observer.disconnect() | ||
} | ||
}) | ||
}) | ||
|
||
observer.observe(targetNode, { attributes: true, attributeFilter: ['class'] }) | ||
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it work, when using multiple JSONEditor on the same Page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ClientSideJsonValidator works with the first json-editor it finds. Currently, Widgets2 uses only one json-editor at a time.
The publication module relies on Widgets2 as a dependency, I manually tested it and found no errors. It's worth noting that the publication module doesn't actively use the ClientSideJsonValidator. Instead, relies on the the regular json-editor error messages during interaction.
If you have any potential scenarios that could conflict with this pull request, please let me know. I know this PR could create more issues than it solves :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added an extra check for the editor. In any case, if for some reason an error occurs during this client validation step, The page will just submit the form normally (no AJAX) and proceed with the normal flow, meaning backend validation and showing an error summary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
ClientSideJsonValidator
specific to widgets2 or would it actually belong to https://github.com/dmstr/yii2-json-editor ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is specific to widgets2