| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\Settings\Validator; |
| 4 |
|
| 5 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 6 |
use FluentForm\Framework\Validator\Validator; |
| 7 |
|
| 8 |
class Notifications extends Validate |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Produce the necessary validation rules and corresponding messages |
| 12 |
* |
| 13 |
* @return array |
| 14 |
*/ |
| 15 |
public static function validations() |
| 16 |
{ |
| 17 |
return [ |
| 18 |
[ |
| 19 |
'sendTo.type' => 'required', |
| 20 |
'sendTo.email' => 'required_if:sendTo.type,email', |
| 21 |
'sendTo.field' => 'required_if:sendTo.type,field', |
| 22 |
'sendTo.routing' => 'required_if:sendTo.type,routing', |
| 23 |
'subject' => 'required', |
| 24 |
'message' => 'required', |
| 25 |
], |
| 26 |
[ |
| 27 |
'sendTo.type.required' => 'The Send To field is required.', |
| 28 |
'sendTo.email.required_if' => 'The Send to Email field is required.', |
| 29 |
'sendTo.field.required_if' => 'The Send to Field field is required.', |
| 30 |
'sendTo.routing' => 'Please fill all the routing rules above.', |
| 31 |
], |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Add conditional validations to the validator. |
| 37 |
* |
| 38 |
* @param \FluentForm\Framework\Validator\Validator $validator |
| 39 |
* |
| 40 |
* @return \FluentForm\Framework\Validator\Validator |
| 41 |
*/ |
| 42 |
public static function conditionalValidations(Validator $validator) |
| 43 |
{ |
| 44 |
$validator->sometimes('sendTo.routing', 'required', function ($input) { |
| 45 |
if ('routing' !== ArrayHelper::get($input, 'sendTo.type')) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$routingInputs = ArrayHelper::get($input, 'sendTo.routing'); |
| 50 |
$required = false; |
| 51 |
|
| 52 |
foreach ($routingInputs as $routingInput) { |
| 53 |
if (!$routingInput['input_value'] || !$routingInput['field']) { |
| 54 |
$required = true; |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $required; |
| 60 |
}); |
| 61 |
|
| 62 |
return $validator; |
| 63 |
} |
| 64 |
} |
| 65 |
|