TForm::setErrorClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * TForm.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Forms!
9
 * @subpackage     Forms
10
 * @since          1.0.0
11
 *
12
 * @date           26.05.13
13
 */
14
15
namespace IPub\Forms\Forms;
16
17
use Nette;
18
use Nette\Forms\Controls;
19
20
/**
21
 * Form trait for decorating form
22
 *
23
 * @package        iPublikuj:Forms!
24
 * @subpackage     Forms
25
 *
26
 * @author         Adam Kadlec <[email protected]>
27
 */
28
trait TForm
29
{
30
	/**
31
	 * @var mixed
32
	 */
33
	protected $id;
34
35
	/**
36
	 * @var string|NULL
37
	 */
38
	protected $errorClass = NULL;
39
40
	/**
41
	 * @param array $defaults
42
	 *
43
	 * @return void
44
	 */
45
	public function restore(array $defaults = []) : void
46
	{
47
		$this->setDefaults($defaults, TRUE);
0 ignored issues
show
Bug introduced by
It seems like setDefaults() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
48
		$this->setValues($defaults, TRUE);
0 ignored issues
show
Bug introduced by
It seems like setValues() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
49
	}
50
51
	/**
52
	 * @return mixed
53
	 */
54
	public function getId()
55
	{
56
		return $this->id;
57
	}
58
59
	/**
60
	 * @param mixed $id
61
	 *
62
	 * @return void
63
	 */
64
	public function setId($id) : void
65
	{
66
		$this->id = $id;
67
	}
68
69
	/**
70
	 * @param string $errorClass
71
	 *
72
	 * @return void
73
	 */
74
	public function setErrorClass(string $errorClass) : void
75
	{
76
		$this->errorClass = $errorClass;
77
	}
78
79
	/**
80
	 * @return void
81
	 */
82
	protected function beforeRender() : void
83
	{
84
		parent::beforeRender();
85
86
		/** @var Controls\BaseControl $control */
87
		foreach ($this->getControls() as $control) {
0 ignored issues
show
Bug introduced by
It seems like getControls() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
88
			$inputClass = [];
89
90
			if ($control->isRequired()) {
91
				$control->getLabelPrototype()->appendAttribute('required', 'required');
92
				$control->getLabelPrototype()->appendAttribute('class', 'ipub-field-required');
93
94
				$inputClass[] = 'ipub-field-required';
95
			}
96
97
			if ($control->hasErrors()) {
98
				$inputClass[] = 'ipub-field-error';
99
100
				if ($this->errorClass) {
101
					$inputClass[] = $this->errorClass;
102
				}
103
			}
104
105
			$control->getControlPrototype()->addAttributes(['class' => $inputClass]);
106
		}
107
	}
108
109
	/**
110
	 * @param string $name
111
	 * @param string $class
112
	 *
113
	 * @return void
114
	 */
115
	protected function addExtension(string $name, string $class) : void
116
	{
117
		Nette\Forms\Container::extensionMethod($name, function (Nette\Forms\Container $container, $name, $label = NULL) use ($class) {
118
			return $container[$name] = new $class($label);
119
		});
120
	}
121
}
122