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