Completed
Push — middleware-wip ( 5cfd03...f2f782 )
by Romain
05:53
created

FieldHasErrorCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 FormZ project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\Formz\Condition\Items;
15
16
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler;
17
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
18
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
19
use Romm\Formz\Error\FormzMessageInterface;
20
use Romm\Formz\Form\Definition\FormDefinition;
21
22
/**
23
 * This condition will match when a field is does have a specific error.
24
 *
25
 * Note: an error is identified by a name of validation (example "isValid"), and
26
 * by the name of the error returned by the validator ("default" by default).
27
 */
28
class FieldHasErrorCondition extends AbstractConditionItem
29
{
30
    const CONDITION_IDENTIFIER = 'fieldHasError';
31
32
    /**
33
     * @inheritdoc
34
     * @var array
35
     */
36
    protected static $javaScriptFiles = [
37
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasError.js'
38
    ];
39
40
    /**
41
     * @var string
42
     * @validate NotEmpty
43
     */
44
    protected $fieldName;
45
46
    /**
47
     * @var string
48
     * @validate NotEmpty
49
     */
50
    protected $validationName;
51
52
    /**
53
     * @var string
54
     * @validate NotEmpty
55
     */
56
    protected $errorName;
57
58
    /**
59
     * @param string $fieldName
60
     * @param string $validationName
61
     * @param string $errorName
62
     */
63
    public function __construct($fieldName, $validationName, $errorName = 'default')
64
    {
65
        $this->fieldName = $fieldName;
66
        $this->validationName = $validationName;
67
        $this->errorName = $errorName;
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function getCssResult()
74
    {
75
        return sprintf(
76
            '[%s="1"]',
77
            DataAttributesAssetHandler::getFieldDataValidationMessageKey($this->fieldName, 'error', $this->validationName, $this->errorName)
78
        );
79
    }
80
81
    /**
82
     * @inheritdoc
83
     */
84
    public function getJavaScriptResult()
85
    {
86
        return $this->getDefaultJavaScriptCall([
87
            'fieldName'      => $this->fieldName,
88
            'validationName' => $this->validationName,
89
            'errorName'      => $this->errorName
90
        ]);
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96
    public function getPhpResult(PhpConditionDataObject $dataObject)
97
    {
98
        $flag = false;
99
        $formValidator = $dataObject->getFormValidator();
100
        $field = $this->formObject->getDefinition()->getField($this->fieldName);
101
        $formValidator->validateField($field);
102
        $result = $formValidator->getResult()->forProperty($this->fieldName);
103
104
        foreach ($result->getErrors() as $error) {
105
            if ($error instanceof FormzMessageInterface
106
                && $this->validationName === $error->getValidationName()
107
                && $this->errorName === $error->getMessageKey()
108
            ) {
109
                $flag = true;
110
                break;
111
            }
112
        }
113
114
        return $flag;
115
    }
116
117
    /**
118
     * Checks the condition configuration/options.
119
     *
120
     * If any syntax/configuration error is found, an exception of type
121
     * `InvalidConditionException` must be thrown.
122
     *
123
     * @param FormDefinition $formDefinition
124
     * @throws InvalidConditionException
125
     */
126
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
127
    {
128
        if (false === $formDefinition->hasField($this->fieldName)) {
129
            throw InvalidConditionException::conditionFieldHasErrorFieldNotFound($this->fieldName);
130
        }
131
132
        if (false === $formDefinition->getField($this->fieldName)->hasValidator($this->validationName)) {
133
            throw InvalidConditionException::conditionFieldHasErrorValidationNotFound($this->validationName, $this->fieldName);
134
        }
135
    }
136
}
137