*/ abstract protected function extensionFieldsModel(): Model|string; /** The validated values of each extension that sent any, keyed by extension id. */ public function extensionValues(): ValidatedExtensionValues { return $this->extensionValues ?? ValidatedExtensionValues::none(); } /** * The validated input, with the validated extension field values under `extensions`. * * @return array */ public function actionInput(): array { return [...parent::actionInput(), 'extensions' => $this->extensionValues()]; } /** * Refuses extension values the signed-in user may not change before anything is * validated, then validates each extension's values once the request's own rules ran, * whether the request builds its validator itself or Laravel does. * * {@inheritdoc} */ protected function getValidatorInstance(): Validator { if ($this->extensionFieldsValidator instanceof Validator) { return $this->extensionFieldsValidator; } $fields = $this->container->make(ExtensionFields::class); $model = $this->extensionFieldsModel(); $input = $this->input('extensions'); $applicationApi = $this instanceof ApplicationApiRequest; $running = array_keys($fields->for($model instanceof Model ? $model::class : $model, $applicationApi)); $fields->authorize($model, is_array($input) ? array_values(array_filter(array_keys($input), is_string(...))) : [], $applicationApi); // A created model has no stored values to keep, so every extension the user may // change runs its rules, sent or not. An update leaves out what the request leaves out. $defaults = $model instanceof Model ? [] : array_values(array_diff($fields->authorizedFor($model, $applicationApi), is_array($input) ? array_keys($input) : [])); $validator = parent::getValidatorInstance(); $validator->after(function (Validator $validator) use ($fields, $model, $applicationApi, $running, $defaults): void { $this->extensionValues = $fields->validate($validator, $model, $this->submittedExtensionValues($validator, $running), $applicationApi, $defaults); }); return $this->extensionFieldsValidator = $validator; } /** * The submitted values of each running extension, reporting input that is not keyed by * extension and then by field, or nested too deeply. Values for other extensions are * ignored without being read. * * @param list $running * @return array> */ private function submittedExtensionValues(Validator $validator, array $running): array { $input = $this->input('extensions'); if ($input === null) { return []; } if (! is_array($input) || ($input !== [] && array_is_list($input))) { $validator->errors()->add('extensions', 'The extensions field must be an object keyed by extension.'); return []; } $submitted = []; foreach (array_intersect_key($input, array_flip($running)) as $extension => $values) { if (! is_array($values) || array_any(array_keys($values), fn (int|string $key): bool => is_int($key))) { $validator->errors()->add('extensions.'.$extension, sprintf('The %s values must be an object keyed by field.', $extension)); continue; } try { ExtensionSettingValueGuard::assertValue($values); $submitted[$extension] = $values; } catch (UnexpectedValueException) { $validator->errors()->add('extensions.'.$extension, sprintf('The %s values are nested too deeply.', $extension)); } } return $submitted; } }