Conditioned field validation (#418)
* wip: validation condition input * form custom validation condition * Default message on form condition validation * field validation condition test * fix linting * update tests, add pass test * Polish UI --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\Forms\Form;
|
||||
use App\Rules\CustomFieldValidationRule;
|
||||
use App\Rules\StorageFile;
|
||||
use App\Rules\ValidHCaptcha;
|
||||
use App\Rules\ValidPhoneInputRule;
|
||||
@@ -50,18 +51,17 @@ class AnswerFormRequest extends FormRequest
|
||||
*/
|
||||
public function rules()
|
||||
{
|
||||
$selectionFields = collect($this->form->properties)->filter(function ($pro) {
|
||||
return in_array($pro['type'], ['select', 'multi_select']);
|
||||
});
|
||||
foreach ($this->form->properties as $property) {
|
||||
$rules = [];
|
||||
|
||||
/*if (!$this->form->is_pro) { // If not pro then not check logic
|
||||
$property['logic'] = false;
|
||||
}*/
|
||||
|
||||
// For get values instead of Id for select/multi select options
|
||||
$data = $this->toArray();
|
||||
$selectionFields = collect($this->form->properties)->filter(function ($pro) {
|
||||
return in_array($pro['type'], ['select', 'multi_select']);
|
||||
});
|
||||
foreach ($selectionFields as $field) {
|
||||
if (isset($data[$field['id']]) && is_array($data[$field['id']])) {
|
||||
$data[$field['id']] = array_map(function ($val) use ($field) {
|
||||
@@ -87,6 +87,11 @@ class AnswerFormRequest extends FormRequest
|
||||
$rules[] = 'nullable';
|
||||
}
|
||||
|
||||
// User custom validation
|
||||
if(!(Str::of($property['type'])->startsWith('nf-')) && isset($property['validation'])) {
|
||||
$rules[] = (new CustomFieldValidationRule($property['validation'], $data));
|
||||
}
|
||||
|
||||
// Clean id to escape "."
|
||||
$propertyId = $property['id'];
|
||||
if (in_array($property['type'], ['multi_select'])) {
|
||||
|
||||
Reference in New Issue
Block a user