| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Parser; |
| 4 |
|
| 5 |
use FluentForm\App\Services\ConditionAssesor; |
| 6 |
use FluentForm\Framework\Helpers\ArrayHelper as Arr; |
| 7 |
|
| 8 |
class Validations |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Form fields that were defined when the form was created. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $fields; |
| 16 |
|
| 17 |
/** |
| 18 |
* Form inputs that were submited by the user. |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
protected $inputs; |
| 23 |
|
| 24 |
/** |
| 25 |
* The current field accessor of the fields' iteration. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
protected $accessor; |
| 30 |
|
| 31 |
/** |
| 32 |
* The repeater field settings. It is being set if the |
| 33 |
* field in iteration is indeed a repeater field. |
| 34 |
* |
| 35 |
* @var array |
| 36 |
*/ |
| 37 |
protected $repeater = [ |
| 38 |
'status' => false, |
| 39 |
'attribute' => '', |
| 40 |
'length' => 0, |
| 41 |
'rule' => '' |
| 42 |
]; |
| 43 |
|
| 44 |
/** |
| 45 |
* The extracted validation rules. |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
protected $rules = []; |
| 50 |
|
| 51 |
/** |
| 52 |
* The extracted validation messages. |
| 53 |
* |
| 54 |
* @var array |
| 55 |
*/ |
| 56 |
protected $messages = []; |
| 57 |
|
| 58 |
/** |
| 59 |
* The validation extractor constructor. |
| 60 |
* |
| 61 |
* @param array $formFields |
| 62 |
* @param array $formData |
| 63 |
*/ |
| 64 |
public function __construct($formFields = [], $formData = []) |
| 65 |
{ |
| 66 |
$this->fields = $formFields; |
| 67 |
$this->inputs = $formData; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get the extracted validation rules and messages. |
| 72 |
* |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function get() |
| 76 |
{ |
| 77 |
foreach ($this->fields as $fieldName => $field) { |
| 78 |
/* |
| 79 |
* todo: isApplicable has some issue for radio / checkbox $applicable getting false |
| 80 |
* even it's required! |
| 81 |
*/ |
| 82 |
$applicable = $this->setFieldAccessor($fieldName)->isApplicable($field); |
| 83 |
|
| 84 |
if ($applicable) { |
| 85 |
$fieldValue = $this->getFieldValue(); |
| 86 |
|
| 87 |
$rules = (array) $field['rules']; |
| 88 |
|
| 89 |
$hasRequiredRule = Arr::get($rules, 'required.value'); |
| 90 |
|
| 91 |
// If the field is a repeater we'll set some settings here. |
| 92 |
$this->setRepeater($fieldName, $field); |
| 93 |
|
| 94 |
foreach ($rules as $ruleName => $rule) { |
| 95 |
if ($this->shouldNotSkipThisRule($rule, $fieldValue, $hasRequiredRule)) { |
| 96 |
$this->prepareValidations($fieldName, $ruleName, $rule); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
return [$this->rules, $this->messages]; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Set the field accessor by replacing the `[]`, `*` by `.` |
| 108 |
* so that dot notation can be used to access the inputs. |
| 109 |
* |
| 110 |
* @param string $fieldName |
| 111 |
* @return $this |
| 112 |
*/ |
| 113 |
protected function setFieldAccessor($fieldName) |
| 114 |
{ |
| 115 |
$this->accessor = rtrim(str_replace(['[', ']', '*'], ['.'], $fieldName), '.'); |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Determines if the field is applicable for extracting validations. |
| 122 |
* |
| 123 |
* @param array $field |
| 124 |
* @return boolean |
| 125 |
*/ |
| 126 |
protected function isApplicable($field) |
| 127 |
{ |
| 128 |
// We need to evaluate if this field should be used to validate |
| 129 |
// the form data because it's possible that this field ain't |
| 130 |
// even present in the form data or it has conditional |
| 131 |
// logics that dictates it's presence in the rules. |
| 132 |
|
| 133 |
// NOTE: Previous code by Arif |
| 134 |
return Arr::has( |
| 135 |
$this->inputs, $this->accessor |
| 136 |
) && ConditionAssesor::evaluate( |
| 137 |
$field, $this->inputs |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get the field value from the form data. |
| 143 |
* |
| 144 |
* @return mixed |
| 145 |
*/ |
| 146 |
protected function getFieldValue() |
| 147 |
{ |
| 148 |
return Arr::get($this->inputs, $this->accessor); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Set the repeater settings if the field in |
| 153 |
* iteration is indeed a repeater field. |
| 154 |
* |
| 155 |
* @param string $fieldName |
| 156 |
* @param array $field |
| 157 |
* @return $this |
| 158 |
*/ |
| 159 |
protected function setRepeater($fieldName, $field) |
| 160 |
{ |
| 161 |
$isRepeater = Arr::get($field, 'element') === 'input_repeat' || Arr::get($field, 'element') == 'repeater_field'; |
| 162 |
|
| 163 |
if ($isRepeater) { |
| 164 |
$attribute = Arr::get($field, 'attributes.name'); |
| 165 |
$length = isset($attribute[0]) ? count($attribute[0]) : 0; |
| 166 |
$this->repeater = [ |
| 167 |
'status' => $isRepeater, |
| 168 |
'attribute' => $attribute, |
| 169 |
'length' => $length, |
| 170 |
'rule' => rtrim($fieldName, '.*') |
| 171 |
]; |
| 172 |
} else { |
| 173 |
$this->repeater['status'] = $isRepeater; |
| 174 |
} |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Determines if the iteration should skip this rule or not. |
| 181 |
* |
| 182 |
* @param array $rule |
| 183 |
* @return boolean |
| 184 |
*/ |
| 185 |
protected function shouldNotSkipThisRule($rule, $fieldValue, $hasRequiredRule) |
| 186 |
{ |
| 187 |
// If the rule is enabled and the field is not empty and |
| 188 |
// it does have at least one required rule then we |
| 189 |
// should validate it for other rules. Else, we |
| 190 |
// will skip this rule meaning enabling empty |
| 191 |
// submission for this field. |
| 192 |
return $rule['value'] && !($fieldValue === '' && !$hasRequiredRule); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Prepare the validation extraction. |
| 197 |
* |
| 198 |
* @param string $fieldName |
| 199 |
* @param string $ruleName |
| 200 |
* @param array $rule |
| 201 |
*/ |
| 202 |
protected function prepareValidations($fieldName, $ruleName, $rule) |
| 203 |
{ |
| 204 |
$logic = $this->getLogic($ruleName, $rule); |
| 205 |
|
| 206 |
if ($this->repeater['status']) { |
| 207 |
for ($i = 0; $i < $this->repeater['length']; $i++) { |
| 208 |
// We need to modify the field name for repeater field. |
| 209 |
$fieldName = $this->repeater['rule'].'['.$i.']'; |
| 210 |
|
| 211 |
$this->setValidations($fieldName, $ruleName, $rule, $logic); |
| 212 |
} |
| 213 |
} else { |
| 214 |
$this->setValidations($fieldName, $ruleName, $rule, $logic); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Set the validation rules & messages |
| 220 |
* |
| 221 |
* @param string $fieldName |
| 222 |
* @param string $ruleName |
| 223 |
* @param array $rule |
| 224 |
* @param string $logic |
| 225 |
*/ |
| 226 |
protected function setValidations($fieldName, $ruleName, $rule, $logic) |
| 227 |
{ |
| 228 |
// if there is already a rule for this field we need to |
| 229 |
// concat current rule to it. else assign current rule. |
| 230 |
$this->rules[$fieldName] = isset($this->rules[$fieldName]) ? |
| 231 |
$this->rules[$fieldName].'|'.$logic : $logic; |
| 232 |
|
| 233 |
$this->messages[$fieldName.'.'.$ruleName] = $rule['message']; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get the logic name for the current rule. |
| 238 |
* |
| 239 |
* @param string $ruleName |
| 240 |
* @param array $rule |
| 241 |
* @return string |
| 242 |
*/ |
| 243 |
protected function getLogic($ruleName, $rule) |
| 244 |
{ |
| 245 |
// The file type input has rule values in an array. For |
| 246 |
// that we are taking arrays into consideration. |
| 247 |
$ruleValue = is_array($rule['value']) ? |
| 248 |
implode(',', array_filter(array_values(str_replace('|', ',', $rule['value'])))) : |
| 249 |
$rule['value']; |
| 250 |
|
| 251 |
return $ruleName.':'.$ruleValue; |
| 252 |
} |
| 253 |
} |
| 254 |
|