BlogArticlesTable::validationDefault()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 23
nc 1
nop 1
1
<?php
2
namespace App\Model\Table;
3
4
use Cake\ORM\Table;
5
use Cake\Validation\Validator;
6
7
class BlogArticlesTable extends Table
8
{
9
10
    /**
11
     * Initialize method
12
     *
13
     * @param array $config The configuration for the Table.
14
     *
15
     * @return void
16
     */
17
    public function initialize(array $config)
18
    {
19
        $this->table('blog_articles');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
20
        $this->displayField('title');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
21
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
23
        $this->addBehavior('Timestamp');
24
        $this->addBehavior('CounterCache', [
25
            'Users' => ['blog_article_count'],
26
            'BlogCategories' => ['article_count']
27
        ]);
28
        $this->addBehavior('Translate', [
29
            'fields' => ['title', 'content'],
30
            'translationTable' => 'BlogArticlesI18n'
31
        ]);
32
33
        $this->belongsTo('BlogCategories', [
34
            'foreignKey' => 'category_id',
35
        ]);
36
        $this->belongsTo('Users', [
37
            'foreignKey' => 'user_id',
38
        ]);
39
        $this->hasMany('BlogArticlesComments', [
40
            'foreignKey' => 'article_id',
41
            'dependent' => true
42
        ]);
43
        $this->hasMany('BlogArticlesLikes', [
44
            'foreignKey' => 'article_id',
45
            'dependent' => true
46
        ]);
47
        $this->hasOne('BlogAttachments', [
48
            'foreignKey' => 'article_id',
49
            'dependent' => true,
50
            'cascadeCallbacks' => true
51
        ]);
52
        $this->hasOne('Polls', [
53
            'foreignKey' => 'article_id',
54
            'dependent' => true
55
        ]);
56
    }
57
58
    /**
59
     * Default validation rules.
60
     *
61
     * @param \Cake\Validation\Validator $validator Instance of the validator.
62
     *
63
     * @return \Cake\Validation\Validator
64
     */
65
    public function validationDefault(Validator $validator)
66
    {
67
        $validator
68
            ->notEmpty('category_id', __("You must select a category."))
69
            ->add('category_id', 'numeric', [
70
                'rule' => 'numeric'
71
            ])
72
            ->notEmpty('title', __("The title is required."))
73
            ->add('title', [
74
                'unique' => [
75
                    'rule' => 'validateUnique',
76
                    'provider' => 'table',
77
                    'message' => __("This title is already used.")
78
                ],
79
                'minLength' => [
80
                    'rule' => ['minLength', 5],
81
                    'message' => __("Please, {0} characters minimum for the title.", 5)
82
                ]
83
            ])
84
            ->notEmpty('content', __("The content is required."))
85
            ->add('content', 'minLength', [
86
                'rule' => ['minLength', 15],
87
                'message' => __("Please, {0} characters minimum for the content.", 15)
88
            ])
89
            ->notEmpty('is_display')
90
            ->add('is_display', 'inList', [
91
                'rule' => ['inList', [0, 1]],
92
                'message' => __("Incorrect value, please do not attempt to modify the HTML, little hacker. ;)")
93
            ]);
94
95
        return $validator;
96
    }
97
}
98