Skip to content

Commit

Permalink
PageGuide: moved all validation inside model, empty rules are remove …
Browse files Browse the repository at this point in the history
…via filter, basic check for rule format, added a filter that reorder the json on step value

PageGuideController: removed (now) unnecessary validation from controller create and update
  • Loading branch information
[email protected] committed May 3, 2022
1 parent e95b82b commit 55695c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 47 deletions.
52 changes: 10 additions & 42 deletions src/controllers/PageGuideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class PageGuideController extends Controller
public function behaviors()
{
return [
'access' =>[
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['index','update','create','delete'],
'actions' => ['index', 'update', 'create', 'delete'],
'allow' => true,
'roles' => ['@'],
]
Expand Down Expand Up @@ -51,28 +51,8 @@ public function actionCreate()
$model = new PageGuide();

$post = Yii::$app->request->post();
if(Yii::$app->request->isPost) {

$origUrl = $post['PageGuide']['url'];

$post['PageGuide']['url'] = parse_url($post['PageGuide']['url'],PHP_URL_PATH);

$post['PageGuide']['rules'] = array_filter($post['PageGuide']['rules'], static function ($val) {
return !empty($val['element']);
});

if(empty($post['PageGuide']['rules'])) {
$model->url = $origUrl;
return $this->render('create', [
'model' => $model,
'rulesError' => Yii::t('pageGuide/view','Rules not set')
]);
}

$post['PageGuide']['rules'] = Json::encode($post['PageGuide']['rules']);

if (Yii::$app->request->isPost) {
PageGuide::deleteAll(['url' => $post['PageGuide']['url']]);

if ($model->load($post) && $model->save()) {
return $this->redirect(['index']);
}
Expand All @@ -86,30 +66,18 @@ public function actionCreate()
public function actionUpdate($id)
{
$model = $this->findModel($id);

if(Yii::$app->request->isPost) {
$rulesError = '';
if (Yii::$app->request->isPost) {
$post = Yii::$app->request->post();

$post['PageGuide']['rules'] = array_filter($post['PageGuide']['rules'], static function ($val) {
return !empty($val['element']);
});

if(empty($post['PageGuide']['rules'])) {
return $this->render('update', [
'model' => $model,
'rulesError' => Yii::t('pageGuide/view','Rules not set')
]);
}

$post['PageGuide']['rules'] = Json::encode($post['PageGuide']['rules']);

if ($model->load($post) && $model->save()) {
return $this->redirect(['index']);
}
$rulesError = $model->getFirstError('rules');
}

return $this->render('update', [
'model' => $model,
'rulesError' => $rulesError,
]);
}

Expand All @@ -118,11 +86,11 @@ public function actionDelete($id)
try {
$model = $this->findModel($id);
} catch (NotFoundHttpException $e) {
Yii::$app->session->setFlash('warning',$e->getMessage());
Yii::$app->session->setFlash('warning', $e->getMessage());
return $this->redirect(['index']);
}

if($model) {
if ($model) {
$model->delete();
}

Expand All @@ -140,6 +108,6 @@ protected function findModel($id): ?PageGuide
return $model;
}

throw new NotFoundHttpException(Yii::t('pageGuide/view','Not found'));
throw new NotFoundHttpException(Yii::t('pageGuide/view', 'Not found'));
}
}
58 changes: 53 additions & 5 deletions src/models/PageGuide.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace matejch\pageGuide\models;

use Exception;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\Json;


/**
* This is the model class for table "page_guide".
Expand All @@ -29,22 +32,67 @@ public function rules()
{
return [
[['url'], 'required'],
[['rules'], 'string'],
[['url'], 'string', 'max' => 1024],
[['url'], 'trim'],
[['url'],'filter','filter'=>'strip_tags','skipOnArray' => true]
[['url'], 'filter', 'filter' => 'strip_tags', 'skipOnArray' => true],
[['rules'], 'validateRuleFormat'],
[['rules'], 'filter', 'filter' => [$this, 'stepOrder']],
[['url'], 'filter', 'filter' => function ($value) {
return parse_url($value, PHP_URL_PATH);
}],
];
}

public function validateRuleFormat($attribute, $params, $validator): void
{
if (!is_array($this->rules)) {
try {
$this->rules = Json::decode($this->rules);
} catch (Exception $e) {
$this->rules = '{}';
$this->addError('rules', 'Invalid rule format');
return;
}
}

$this->rules = array_filter($this->rules, static function ($val) {
return !empty($val['element']);
});
if (empty($this->rules)) {
$this->rules = '{}';
$this->addError('rules', Yii::t('pageGuide/view', 'Rules not set'));
return;
}

foreach ($this->rules as $rule) {
if (!is_array($rule) || !isset($rule['step'], $rule['element'], $rule['intro'])) {
$this->addError('rules', Yii::t('pageGuide/view', 'Invalid rule format'));
break;
}
}
Json::encode($this->rules);
}

/**
* Order the rule on step value
*/
public function stepOrder(string $value): array
{
$value = Json::decode($value);
$keys = array_column($value, 'step');
array_multisort($keys, SORT_ASC, $value);
return Json::encode($value);
}

/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => Yii::t('pageGuide/model','ID'),
'url' => Yii::t('pageGuide/model','Url'),
'rules' => Yii::t('pageGuide/model','Rules'),
'id' => Yii::t('pageGuide/model', 'ID'),
'url' => Yii::t('pageGuide/model', 'Url'),
'rules' => Yii::t('pageGuide/model', 'Rules'),
];
}
}

0 comments on commit 55695c9

Please sign in to comment.