Completed
Push — master ( fef881...1aed96 )
by vistart
20:35
created

JoinOrganizationForm::attributeHints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\forms;
14
15
use rhosocial\organization\Organization;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\Model;
19
20
/**
21
 * Class JoinOrganizationForm
22
 * @package rhosocial\organization\forms
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class JoinOrganizationForm extends Model
27
{
28
    /**
29
     * @var Organization
30
     */
31
    public $organization;
32
33
    /**
34
     * @var string
35
     */
36
    public $password = '';
37
38
    /**
39
     * @inheritdoc
40
     * @throws InvalidConfigException
41
     */
42
    public function init()
43
    {
44
        if (!$this->organization) {
45
            throw new InvalidConfigException("The organization should not be empty.");
46
        }
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function attributeLabels()
53
    {
54
        return [
55
            'password' => Yii::t('organization', 'Password'),
56
        ];
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function attributeHints()
63
    {
64
        return [
65
            'password' => Yii::t('organization', 'You need to provide password.'),
66
        ];
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function rules()
73
    {
74
        return [
75
            ['password', 'safe', 'when' => function ($model) {
76
                /* @var $model static */
77
                return empty($model->organization->joinPassword);
78
            }],
79
            ['password', 'string', 'max' => 255, 'when' => function ($model) {
80
                /* @var $model static */
81
                return !empty($model->organization->joinPassword);
82
            }],
83
            ['password', 'required', 'when' => function ($model) {
84
                /* @var $model static */
85
                return !empty($model->organization->joinPassword);
86
            }],
87
            ['password', 'validatePassword', 'when' => function ($model) {
88
                /* @var $model static */
89
                return !empty($model->organization->joinPassword);
90
            }],
91
        ];
92
    }
93
94
    /**
95
     * @param $attribute
96
     * @param $params
97
     * @param $validator
98
     */
99
    public function validatePassword($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $value = $this->$attribute;
102
        if ($this->organization->joinPassword != $value) {
103
            $this->addError($attribute, Yii::t('organization', 'Incorrect Password.'));
104
        }
105
    }
106
}
107