$field) { if(!in_array($fieldName, $this->excludedNames)) { $validator[$fieldName . ".0"] = 'required|string'; $validator[$fieldName . ".1"] = 'required|string'; if ($generateStructure) { $formStructure['fields'][$fieldName]['title'] = $field[0]; $formStructure['fields'][$fieldName]['type'] = $field[1]; } } } $validatorInstance = Validator::make($fields, $validator); return ($generateStructure) ? collect([ 'validator' => $validatorInstance, 'structure' => $this->encode($formStructure) ]) : $validatorInstance; } /** * The getResponseValidator method is similar to the getValidator method; It basically takes * an array of fields from a previous form (that probably went through the other method) and adds validation * to the field names. * * Also generates the storable response structure if you tell it to. * * @param array $fields The received fields * @param array $formStructure The form structure - You must supply this if you want the response structure * @param bool $generateResponseStructure Whether to generate the response structure * @return Validator|Collection A collection or a validator, depending on the args. Will return validatior if only fields are supplied. */ public function getResponseValidator(array $fields, array $formStructure = [], bool $generateResponseStructure = true) { $responseStructure = []; $validator = []; if (empty($formStructure) && $generateResponseStructure) { throw new \InvalidArgumentException('Illegal combination of arguments supplied! Please check the method\'s documentation.'); } foreach($fields as $fieldName => $value) { if(!in_array($fieldName, $this->excludedNames)) { $validator[$fieldName] = 'required|string'; if ($generateResponseStructure) { $responseStructure['responses'][$fieldName]['type'] = $formStructure['fields'][$fieldName]['type'] ?? 'Unavailable'; $responseStructure['responses'][$fieldName]['title'] = $formStructure['fields'][$fieldName]['title']; $responseStructure['responses'][$fieldName]['response'] = $value; } } } $validatorInstance = Validator::make($fields, $validator); return ($generateResponseStructure) ? collect([ 'validator' => $validatorInstance, 'responseStructure' => $this->encode($responseStructure) ]) : $validatorInstance; } }