forked from kartik-v/yii2-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormTrait.php
executable file
·73 lines (68 loc) · 2.29 KB
/
FormTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* @package yii2-builder
* @author Kartik Visweswaran <[email protected]>
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2016
* @version 1.6.2
*/
namespace kartik\builder;
use yii\base\InvalidConfigException;
use yii\base\Model;
use kartik\form\ActiveForm;
/**
* Trait for all methods used in all the form builder widgets in `yii2-builder` and initialized within [[BaseForm]].
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
trait FormTrait
{
/**
* Checks base configuration and throws a configuration exception if invalid.
*
* @throws InvalidConfigException
*/
protected function checkBaseConfig()
{
if (empty($this->form) && empty($this->formName)) {
throw new InvalidConfigException(
"The 'formName' property must be set when you are not using with ActiveForm."
);
}
if (!empty($this->form) && !$this->form instanceof ActiveForm) {
throw new InvalidConfigException(
"The 'form' property must be an instance of '\\kartik\\widgets\\ActiveForm' or '\\kartik\\form\\ActiveForm'."
);
}
if (empty($this->attributes)) {
throw new InvalidConfigException("The 'attributes' array must be set.");
}
}
/**
* Checks the form configuration and throws a configuration exception if invalid.
*
* @throws InvalidConfigException
*/
protected function checkFormConfig()
{
if (!$this->hasModel() && empty($this->formName)) {
throw new InvalidConfigException(
"Either the 'formName' has to be set or a valid 'model' property must be set extending from '\\yii\\base\\Model'."
);
}
if (empty($this->formName) && (empty($this->form) || !$this->form instanceof ActiveForm)) {
throw new InvalidConfigException(
"The 'form' property must be set and must be an instance of '\\kartik\\form\\ActiveForm'."
);
}
}
/**
* Check if a valid model is set for the object instance.
*
* @return boolean whether there is a valid model set.
*/
protected function hasModel()
{
return isset($this->model) && $this->model instanceof Model;
}
}