mirror of
https://github.com/humhub/humhub.git
synced 2025-02-22 10:14:19 +01:00
#4396 create CommentForm for validation of model Comment.
This commit is contained in:
parent
e7c51f5a12
commit
6a82004db2
@ -1,10 +1,6 @@
|
||||
HumHub Changelog
|
||||
================
|
||||
|
||||
1.6.5 (Unreleased)
|
||||
------------------
|
||||
- Fix #4396: Submitting only picture in comment results in debug error.
|
||||
|
||||
|
||||
1.6.4 (Unreleased)
|
||||
------------------
|
||||
@ -14,7 +10,7 @@ This release also brings a [security update](https://github.com/yiisoft/yii2/sec
|
||||
- Fix #4361: Added missing nonce attribute in inline marketplace script
|
||||
- Fix #4371: Word break issue in notification dropdown
|
||||
- Fix #4384: Upgrade to Yii 2.0.38
|
||||
|
||||
- Fix #4396: Submitting only picture in comment results in debug error.
|
||||
|
||||
1.6.3 (September 9, 2020)
|
||||
-----------------------
|
||||
|
@ -10,6 +10,7 @@ namespace humhub\modules\comment\controllers;
|
||||
|
||||
use humhub\components\Controller;
|
||||
use humhub\libs\Helpers;
|
||||
use humhub\modules\comment\models\forms\CommentForm;
|
||||
use humhub\modules\comment\Module;
|
||||
use humhub\modules\content\components\ContentActiveRecord;
|
||||
use humhub\modules\content\models\Content;
|
||||
@ -21,7 +22,6 @@ use humhub\components\behaviors\AccessControl;
|
||||
use humhub\modules\comment\models\Comment;
|
||||
use humhub\modules\comment\widgets\Comment as CommentWidget;
|
||||
use humhub\modules\comment\widgets\ShowMore;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\ForbiddenHttpException;
|
||||
|
||||
/**
|
||||
@ -130,22 +130,15 @@ class CommentController extends Controller
|
||||
}
|
||||
|
||||
return Comment::getDb()->transaction(function ($db) {
|
||||
$message = Yii::$app->request->post('message');
|
||||
$files = Yii::$app->request->post('fileList');
|
||||
|
||||
if (empty(trim($message)) && empty($files)) {
|
||||
throw new BadRequestHttpException(Yii::t('CommentModule.base', 'The comment must not be empty!'));
|
||||
$form = new CommentForm($this->target);
|
||||
|
||||
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
|
||||
$comment = $form->save();
|
||||
return $this->renderAjaxContent(CommentWidget::widget(['comment' => $comment]));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment = new Comment(['message' => $message]);
|
||||
$comment->setPolyMorphicRelation($this->target);
|
||||
$comment->save();
|
||||
$comment->fileManager->attach($files);
|
||||
|
||||
// Reload comment to get populated created_at field
|
||||
$comment->refresh();
|
||||
|
||||
return $this->renderAjaxContent(CommentWidget::widget(['comment' => $comment]));
|
||||
});
|
||||
}
|
||||
|
||||
@ -154,14 +147,26 @@ class CommentController extends Controller
|
||||
{
|
||||
$comment = Comment::findOne(['id' => Yii::$app->request->get('id')]);
|
||||
|
||||
// Find all file attached to this comment
|
||||
$files = $comment->fileManager->find()->asArray()->all();
|
||||
$fileList = [];
|
||||
$i = 0;
|
||||
|
||||
// Fill up files array
|
||||
foreach ($files as $file) {
|
||||
$fileList[$i] = $file['guid'];
|
||||
$i++;
|
||||
}
|
||||
|
||||
if (!$comment->canEdit()) {
|
||||
throw new HttpException(403, Yii::t('CommentModule.base', 'Access denied!'));
|
||||
}
|
||||
|
||||
if ($comment->load(Yii::$app->request->post()) && $comment->save()) {
|
||||
$form = new CommentForm($this->target, $fileList);
|
||||
|
||||
// Reload comment to get populated updated_at field
|
||||
$comment = Comment::findOne(['id' => $comment->id]);
|
||||
if ($form->load(Yii::$app->request->post(), '') && $form->validate()) {
|
||||
|
||||
$comment = $form->update($comment);
|
||||
|
||||
return $this->renderAjaxContent(CommentWidget::widget([
|
||||
'comment' => $comment,
|
||||
|
@ -57,9 +57,6 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
|
||||
{
|
||||
return [
|
||||
[['message'], 'safe'],
|
||||
[['message'], 'required', 'when' => function ($comment) {
|
||||
return !$comment->fileManager->find()->exists();
|
||||
}],
|
||||
];
|
||||
}
|
||||
|
||||
|
122
protected/humhub/modules/comment/models/forms/CommentForm.php
Normal file
122
protected/humhub/modules/comment/models/forms/CommentForm.php
Normal file
@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace humhub\modules\comment\models\forms;
|
||||
|
||||
use humhub\modules\comment\models\Comment;
|
||||
use humhub\modules\content\components\ContentActiveRecord;
|
||||
use Yii;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
|
||||
/**
|
||||
* CommentForm
|
||||
* @package humhub\modules\comment\models\forms
|
||||
*
|
||||
* @since 0.5
|
||||
*/
|
||||
class CommentForm extends yii\base\Model
|
||||
{
|
||||
public $message;
|
||||
public $fileList;
|
||||
|
||||
/**
|
||||
* @var Comment|ContentActiveRecord The model to comment
|
||||
*/
|
||||
public $target;
|
||||
|
||||
public function __construct($target, $fileList = null)
|
||||
{
|
||||
$this->target = $target;
|
||||
$this->fileList = $fileList;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array validation rules for model attributes.
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['message'], 'safe'],
|
||||
[['message'], 'required', 'isEmpty' => function ($message) {
|
||||
$hasFile = !is_null($this->fileList) && !empty($this->fileList) ? true : false;
|
||||
|
||||
//check if message empty and attached file exists
|
||||
if (empty($message) && !$hasFile) {
|
||||
throw new ServerErrorHttpException(Yii::t('CommentModule.base', 'The comment must not be empty!'));
|
||||
}
|
||||
}],
|
||||
[['fileList'], 'safe'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function load($data, $formName = null)
|
||||
{
|
||||
// When user updates comment $data contain 'Comment', otherwise not
|
||||
if (isset($data['Comment'])) {
|
||||
$data['message'] = $data['Comment']['message'];
|
||||
unset($data['Comment']);
|
||||
}
|
||||
|
||||
return parent::load($data, $formName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function formName()
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the form
|
||||
*
|
||||
* @param Comment $comment
|
||||
* @return Comment|boolean
|
||||
*/
|
||||
public function update($comment)
|
||||
{
|
||||
|
||||
$comment->message = $this->message;
|
||||
$comment->setPolyMorphicRelation($this->target);
|
||||
|
||||
//check if model saved
|
||||
if (!$comment->save()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment->fileManager->attach($this->fileList);
|
||||
|
||||
// Reload comment to get populated created_at field
|
||||
$comment->refresh();
|
||||
return $comment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves the form
|
||||
*
|
||||
* @return Comment|boolean
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
|
||||
$comment = new Comment(['message' => $this->message]);
|
||||
$comment->setPolyMorphicRelation($this->target);
|
||||
|
||||
//check if model saved
|
||||
if (!$comment->save()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$comment->fileManager->attach($this->fileList);
|
||||
|
||||
// Reload comment to get populated created_at field
|
||||
$comment->refresh();
|
||||
return $comment;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user