1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace roaresearch\yii2\formgenerator\models; |
4
|
|
|
|
5
|
|
|
use yii\base\Model; |
6
|
|
|
use yii\db\ActiveQuery; |
7
|
|
|
use yii\helpers\ArrayHelper; |
8
|
|
|
use yii\validators\Validator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Model class for table `{{%formgenerator_field_rule}}` |
12
|
|
|
* |
13
|
|
|
* @property integer $id |
14
|
|
|
* @property integer $field_id |
15
|
|
|
* @property string $rule_class |
16
|
|
|
* |
17
|
|
|
* @property Field $field |
18
|
|
|
* @property FieldRuleProperty[] $properties |
19
|
|
|
*/ |
20
|
|
|
class FieldRule extends \roaresearch\yii2\rmdb\models\Entity |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string full class name of the model used in the relation |
24
|
|
|
* `getField()`. |
25
|
|
|
*/ |
26
|
|
|
protected $fieldClass = Field::class; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string full class name of the model used in the relation |
30
|
|
|
* `getProperties()`. |
31
|
|
|
*/ |
32
|
|
|
protected $propertyClass = FieldRuleProperty::class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
12 |
|
public static function tableName() |
38
|
|
|
{ |
39
|
12 |
|
return '{{%formgenerator_field_rule}}'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
11 |
|
protected function attributeTypecast(): array |
46
|
|
|
{ |
47
|
11 |
|
return parent::attributeTypecast() + [ |
48
|
11 |
|
'id' => 'integer', |
49
|
|
|
'field_id' => 'integer', |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
1 |
|
public function rules() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
1 |
|
[['field_id', 'rule_class'], 'required'], |
60
|
|
|
[['field_id'], 'integer'], |
61
|
|
|
[ |
62
|
|
|
['field_id'], |
63
|
|
|
'exist', |
64
|
|
|
'skipOnError' => true, |
65
|
|
|
'targetClass' => Field::class, |
66
|
|
|
'targetAttribute' => ['field_id' => 'id'], |
67
|
|
|
], |
68
|
|
|
[['rule_class'], 'string', 'min' => 2], |
69
|
|
|
// todo check its a valid validator class |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
1 |
|
public function attributeLabels() |
77
|
|
|
{ |
78
|
1 |
|
return array_merge([ |
79
|
1 |
|
'field_id' => 'Field ID', |
80
|
|
|
'rule_class' => 'Rule Class', |
81
|
1 |
|
], parent::attributeLabels()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return ActiveQuery |
86
|
|
|
*/ |
87
|
11 |
|
public function getField(): ActiveQuery |
88
|
|
|
{ |
89
|
11 |
|
return $this->hasOne($this->fieldClass, ['id' => 'field_id']); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return ActiveQuery |
94
|
|
|
*/ |
95
|
3 |
|
public function getProperties(): ActiveQuery |
96
|
|
|
{ |
97
|
3 |
|
return $this->hasMany($this->propertyClass, ['rule_id' => 'id']) |
98
|
3 |
|
->inverseOf('rule'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param Model $model |
103
|
|
|
* @param array $attributes |
104
|
|
|
*/ |
105
|
2 |
|
public function buildValidator(Model $model, $attributes) |
106
|
|
|
{ |
107
|
2 |
|
return Validator::createValidator( |
108
|
2 |
|
$this->rule_class, |
109
|
2 |
|
$model, |
110
|
2 |
|
(array) $attributes, |
111
|
2 |
|
ArrayHelper::map($this->properties, 'property', 'value') |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|