public function process(Request $request) { // Ajax-validation is only possible, if the _formID was submitted (automatically done by the FormBuilder). if (\Request::has('_formID')) { // The FormBuilder should have saved the requestObject this form uses inside the session. // We check, if it is there, and can continue only, if it is. $sessionKeyForRequestObject = 'htmlBuilder.formBuilder.requestObjects.' . \Request::input('_formID'); if (Session::has($sessionKeyForRequestObject)) { // Normally we assume a successful submission and return just an empty JSON-array. $returnCode = 200; $return = []; // We instantiate the requestObject. $formRequest = FormBuilderTools::getRequestObject(Session::get($sessionKeyForRequestObject)); // We instantiate a controller with the submitted request-data // and the rules and messages from the requestObject. $validator = Validator::make(\Request::all(), $formRequest->rules(), $formRequest->messages()); // Perform validation, extract error-messages for all fields on failure, put them inside a $return['errors']-array, and return status code 422. if ($validator->fails()) { $errors = []; foreach (array_dot(\Request::all()) as $fieldName => $fieldValue) { $fieldErrors = FormBuilderTools::extractErrorsForField($fieldName, $validator->errors()->getMessages(), \Request::all()); if (count($fieldErrors) > 0) { $errors[FormBuilderTools::convertArrayFieldDotNotation2HtmlName($fieldName)] = $fieldErrors; } } $return['errors'] = $errors; $returnCode = 422; } return new JsonResponse($return, $returnCode); } } }
/** * Gets the error(s) of field $name from $errors. * Furthermore: If it is the first element of an array (e.g. domainList.0.domainName), * the errors of the array itself are also returned. * For multidimensional arrays this works recursively. * Example: * This server-side request array: * domainList => [ * 0 => [ * domainLabel => 'first-domain', * domainPdo => 'at', * ], * 1 => [ * domainLabel => 'second-domain', * domainPdo => 'at', * ], * * Returns errors in the following structure: * * - Field 'domainList[0][domainLabel]' returns errors for: * - domainList[0][domainLabel] * - domainList[0] * - domainList * - Field 'domainList[0][domainPdo]' returns errors for: * - domainList[0][domainPdo] * - Field 'domainList[1][domainLabel]' returns errors for: * - domainList[1][domainLabel] * - domainList[1] * - Field 'domainList[1][domainPdo]' returns errors for: * - domainList[1][domainPdo] * * @param string $name: Name of the field (array-fields must be in dot-notation (e.g. "domainList.0.domainName")) * @param array $errors: Array of all errors (in dot-notation) * @param array $request: Array of full request (in dot-notation) * @return array */ public static function extractErrorsForField($name = '', $errors = [], $request = []) { $return = []; $isArray = false; // Check, if the name is an array-key (e.g. "domainList.0.domainName"). if (strpos($name, '.') !== false) { $isArray = true; } // If the field is the first element of an array, // we also add errors regarding the array itself. // (e.g. in case of a 'size:5' validation rule on the array itself) if ($isArray) { $arrayName = substr($name, 0, strrpos($name, '.')); $arrayFieldKey = substr($name, strrpos($name, '.') + 1); $addArrayErrors = true; if (array_has($request, $arrayName) && strval(key(array_get($request, $arrayName))) !== $arrayFieldKey) { $addArrayErrors = false; } if ($addArrayErrors) { $return = array_merge(FormBuilderTools::extractErrorsForField($arrayName, $errors, $request), $return); } } // Get the errors (if present) from $this->errors. if (isset($errors[$name])) { $return = array_merge($return, $errors[$name]); } return $return; }
/** * Gets the error(s) of a field currently stored in the FormBuilder-object. * Furthermore: If it is the first element of an array (e.g. domainName[0]), * the errors of the array itself are also returned. * This even works for multidimensional arrays. * Example: * This server-side request array: * domainList => [ * 0 => [ * domainLabel => 'first-domain', * domainPdo => 'at', * ], * 1 => [ * domainLabel => 'second-domain', * domainPdo => 'at', * ], * * Returns errors in the following structure: * * - Field 'domainList[0][domainLabel]' returns errors for: * - domainList[0][domainLabel] * - domainList[0] * - domainList * - Field 'domainList[0][domainPdo]' returns errors for: * - domainList[0][domainPdo] * - Field 'domainList[1][domainLabel]' returns errors for: * - domainList[1][domainLabel] * - domainList[1] * - Field 'domainList[1][domainPdo]' returns errors for: * - domainList[1][domainPdo] * * @param string $name * @return array */ public function getErrorsForField($name = '') { $errors = []; if (count($this->errors) > 0) { $errors = FormBuilderTools::extractErrorsForField(FormBuilderTools::convertArrayFieldHtmlName2DotNotation($name), $this->errors, Request::old()); } return $errors; }