From e10f7854c03c0e6d869f42e37c2df1bb7f879885 Mon Sep 17 00:00:00 2001 From: Yuriy Bakhtin Date: Thu, 2 Jun 2022 17:26:31 +0300 Subject: [PATCH] New event to append rules for active record (#5645) * New event to append rules for active record * Implement new post validation on focus field * Revert "Implement new post validation on focus field" This reverts commit 395917537cbfabb61e6950286ba09a60b3691e51. * Validate post creating on focus event by AJAX * Validate new post message on blur event by ajax request * Fix creating of new Post * Validate new post message on focus event by ajax request * Skip rule "required" for post message on AJAX validation * Move code to new module "Interaction limits" * Clear code for creating of new post * Remove a not used action * Improve new comment form for event triggers * Update CHANGELOG_DEV.md Co-authored-by: Lucas Bartholemy --- CHANGELOG_DEV.md | 1 + protected/humhub/components/ActiveRecord.php | 33 +++++++++++++ .../humhub/modules/comment/widgets/Form.php | 1 + .../modules/comment/widgets/views/form.php | 1 + .../resources/js/humhub.content.form.js | 23 ++++----- .../js/humhub.ui.richtext.prosemirror.js | 3 +- .../content/widgets/WallCreateContentForm.php | 8 ++-- .../richtext/AbstractRichTextEditor.php | 10 +++- .../widgets/views/wallCreateContentForm.php | 14 +++--- protected/humhub/modules/post/Events.php | 1 - protected/humhub/modules/post/config.php | 3 +- .../post/controllers/PostController.php | 2 +- protected/humhub/modules/post/models/Post.php | 15 +++--- .../modules/post/widgets/CreateForm.php | 48 ------------------- .../humhub/modules/post/widgets/Form.php | 6 ++- .../modules/post/widgets/views/form.php | 9 +++- static/less/stream.less | 8 ++-- themes/HumHub/css/theme.css | 2 +- 18 files changed, 95 insertions(+), 93 deletions(-) delete mode 100644 protected/humhub/modules/post/widgets/CreateForm.php diff --git a/CHANGELOG_DEV.md b/CHANGELOG_DEV.md index 9effb269fa..163536bce1 100644 --- a/CHANGELOG_DEV.md +++ b/CHANGELOG_DEV.md @@ -3,3 +3,4 @@ - Enh #5655: Possibility to archive and lock comments on global contents - Enh #3593: Allow SSO provider to register and auto create username on registration - Enh #5695: Truncate long profile texts in People cards +- Enh #5602: Added API to inject additional validation rules by modules on demand diff --git a/protected/humhub/components/ActiveRecord.php b/protected/humhub/components/ActiveRecord.php index 93eca7fff7..ce83fc9dca 100644 --- a/protected/humhub/components/ActiveRecord.php +++ b/protected/humhub/components/ActiveRecord.php @@ -11,7 +11,9 @@ namespace humhub\components; use Yii; use humhub\modules\user\models\User; use humhub\modules\file\components\FileManager; +use yii\base\InvalidConfigException; use yii\db\Expression; +use yii\validators\Validator; /** * Description of ActiveRecord @@ -35,6 +37,11 @@ class ActiveRecord extends \yii\db\ActiveRecord */ public $fileManagerEnableHistory = false; + /** + * @event Event is used to append rules what defined in [[rules()]]. + */ + const EVENT_APPEND_RULES = 'appendRules'; + /** * @inheritdoc */ @@ -183,4 +190,30 @@ class ActiveRecord extends \yii\db\ActiveRecord { return $attribute === null ? '' : parent::getAttributeLabel($attribute); } + + /** + * @inheritdoc + */ + public function createValidators() + { + $validators = parent::createValidators(); + + $event = new Event(); + $this->trigger(self::EVENT_APPEND_RULES, $event); + + if (is_array($event->result)) { + foreach ($event->result as $rule) { + if ($rule instanceof Validator) { + $validators->append($rule); + } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type + $validator = Validator::createValidator($rule[1], $this, (array)$rule[0], array_slice($rule, 2)); + $validators->append($validator); + } else { + throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.'); + } + } + } + + return $validators; + } } diff --git a/protected/humhub/modules/comment/widgets/Form.php b/protected/humhub/modules/comment/widgets/Form.php index 63e6b44573..888d959841 100644 --- a/protected/humhub/modules/comment/widgets/Form.php +++ b/protected/humhub/modules/comment/widgets/Form.php @@ -77,6 +77,7 @@ class Form extends Widget if (!$this->model) { $this->model = new CommentModel(); + $this->model->setPolyMorphicRelation($this->object); } return $this->render('form', [ diff --git a/protected/humhub/modules/comment/widgets/views/form.php b/protected/humhub/modules/comment/widgets/views/form.php index 7cb3128e65..7feb273f89 100644 --- a/protected/humhub/modules/comment/widgets/views/form.php +++ b/protected/humhub/modules/comment/widgets/views/form.php @@ -48,6 +48,7 @@ $placeholder = ($isNestedComment)
field($model, 'message')->widget(RichTextField::class, [ 'id' => 'newCommentForm_' . $id, + 'form' => $form, 'layout' => RichTextField::LAYOUT_INLINE, 'pluginOptions' => ['maxHeight' => '300px'], 'mentioningUrl' => $mentioningUrl, diff --git a/protected/humhub/modules/content/resources/js/humhub.content.form.js b/protected/humhub/modules/content/resources/js/humhub.content.form.js index aeea40d5d2..a0746976cc 100644 --- a/protected/humhub/modules/content/resources/js/humhub.content.form.js +++ b/protected/humhub/modules/content/resources/js/humhub.content.form.js @@ -20,14 +20,10 @@ humhub.module('content.form', function(module, require, $) { object.inherits(CreateForm, Widget); CreateForm.prototype.init = function() { - var that = this; - this.$.hide(); // Hide options by default $('.contentForm_options').hide(); - $('#contentFormError').hide(); - this.setDefaultVisibility(); this.$.fadeIn('fast'); @@ -46,8 +42,9 @@ humhub.module('content.form', function(module, require, $) { }; CreateForm.prototype.submit = function(evt) { - this.$.find("#contentFormError, .preferences, .fileinput-button").hide(); - this.$.find("#contentFormError li").remove(); + this.$.find('.preferences, .fileinput-button').hide(); + this.$.find('.help-block-error').html(''); + this.$.find('.has-error').removeClass('has-error'); var that = this; evt.block = 'manual'; @@ -116,14 +113,12 @@ humhub.module('content.form', function(module, require, $) { }; CreateForm.prototype.handleError = function(response) { - $('#contentFormError').show(); - $.each(response.errors, function(fieldName, errorMessage) { - // Mark Fields as Error - var fieldId = 'contentForm_' + fieldName; - $('#' + fieldId).addClass('error'); - $.each(errorMessage, function(key, msg) { - $('#contentFormError').append('
  • ' + msg + '
  • '); - }); + var that = this; + $.each(response.errors, function(fieldName, errorMessages) { + that.$.find('.field-post-' + fieldName).addClass('has-error'); + var fieldSelector = '.field-contentForm_' + fieldName; + that.$.find(fieldSelector + ', ' + fieldSelector + '_input') + .find('.help-block-error').html(errorMessages.join('
    ')); }); }; diff --git a/protected/humhub/modules/content/resources/js/humhub.ui.richtext.prosemirror.js b/protected/humhub/modules/content/resources/js/humhub.ui.richtext.prosemirror.js index cc936bcd3b..7db0ca68bf 100644 --- a/protected/humhub/modules/content/resources/js/humhub.ui.richtext.prosemirror.js +++ b/protected/humhub/modules/content/resources/js/humhub.ui.richtext.prosemirror.js @@ -61,7 +61,7 @@ humhub.module('ui.richtext.prosemirror', function(module, require, $) { var that = this; this.$.on('focusout', function() { - that.getInput().val(that.editor.serialize()); + that.getInput().val(that.editor.serialize()).trigger('blur'); }).on('clear', function() { that.editor.clear(); }).on('focus', function() { @@ -70,6 +70,7 @@ humhub.module('ui.richtext.prosemirror', function(module, require, $) { this.$.find('.humhub-ui-richtext').on('focus', function() { that.focus(); + that.getInput().val(that.editor.serialize()).trigger('blur'); }) if (this.options.backupInterval) { diff --git a/protected/humhub/modules/content/widgets/WallCreateContentForm.php b/protected/humhub/modules/content/widgets/WallCreateContentForm.php index 64bc60d644..5c2db7ae9a 100644 --- a/protected/humhub/modules/content/widgets/WallCreateContentForm.php +++ b/protected/humhub/modules/content/widgets/WallCreateContentForm.php @@ -11,6 +11,7 @@ namespace humhub\modules\content\widgets; use humhub\modules\content\permissions\CreatePublicContent; use humhub\modules\stream\actions\StreamEntryResponse; use humhub\modules\topic\models\Topic; +use humhub\modules\ui\form\widgets\ActiveForm; use Yii; use yii\web\HttpException; use humhub\components\Widget; @@ -67,11 +68,12 @@ class WallCreateContentForm extends Widget /** * Returns the custom form implementation. * + * @param ActiveForm * @return string */ - public function renderForm() + public function renderForm(ActiveForm $form): string { - return ""; + return ''; } /** @@ -88,7 +90,7 @@ class WallCreateContentForm extends Widget } return $this->render('@humhub/modules/content/widgets/views/wallCreateContentForm', [ - 'form' => $this->renderForm(), + 'wallCreateContentForm' => $this, 'contentContainer' => $this->contentContainer, 'submitUrl' => $this->contentContainer->createUrl($this->submitUrl), 'submitButtonText' => $this->submitButtonText, diff --git a/protected/humhub/modules/content/widgets/richtext/AbstractRichTextEditor.php b/protected/humhub/modules/content/widgets/richtext/AbstractRichTextEditor.php index ff8fb0691f..f7b1eb8da4 100644 --- a/protected/humhub/modules/content/widgets/richtext/AbstractRichTextEditor.php +++ b/protected/humhub/modules/content/widgets/richtext/AbstractRichTextEditor.php @@ -118,6 +118,12 @@ class AbstractRichTextEditor extends JsInputWidget */ public $pluginOptions = []; + /** + * Options for field of active form + * @var array + */ + public $fieldOptions = []; + /** * If set to true the picker will be focused automatically. * @@ -175,7 +181,7 @@ class AbstractRichTextEditor extends JsInputWidget $inputOptions = $this->getInputAttributes(); if ($this->form != null) { - $input = $this->form->field($this->model, $this->attribute)->textarea($inputOptions)->label(false); + $input = $this->form->field($this->model, $this->attribute, $this->fieldOptions)->textarea($inputOptions)->label(false); $richText = Html::tag('div', $this->editOutput($this->getValue()), $this->getOptions()); $richText = $this->getLabel() . $richText; } elseif ($this->model != null) { @@ -188,7 +194,7 @@ class AbstractRichTextEditor extends JsInputWidget $richText = $this->getLabel() . $richText; } - return $input . $richText . $this->prepend(); + return $richText . $input . $this->prepend(); } /** diff --git a/protected/humhub/modules/content/widgets/views/wallCreateContentForm.php b/protected/humhub/modules/content/widgets/views/wallCreateContentForm.php index 685367f87d..446146d70c 100644 --- a/protected/humhub/modules/content/widgets/views/wallCreateContentForm.php +++ b/protected/humhub/modules/content/widgets/views/wallCreateContentForm.php @@ -1,6 +1,8 @@ createUrl