Kernel::System::Role::ValidatesMethodParams

NAME

Kernel::System::Role::ValidatesMethodParams – Role for modules to validate method parameters.

PUBLIC INTERFACE

Provides methods to be defined in modules / classes, that are used to validate method parameters. The field validations will be performed via the data validation backend (see Kernel::System::DataValidation for more information about how to use it, the different validators and it's possible parameters).

has MethodParamValidationErrors

Attribute that probably contains the collected errors of the very last method parameter validation. This attribute will be cleared implicitly if the method parameter validation will be called.

requires MethodParamValidationSchema

Method that returns the global validation definition for method parameters. These field schemata will be dynamically requested by their corresponding field name, during the parameter validation.

Modules can define it like this:

    sub MethodParamValidationSchema {
        return {
            TicketID => [
                'Required',
                {
                    Validator => 'Pattern',
                    Arguments => '^\d+$'
                },
                'Ticket::TicketID',
            ],
            UserID => [
                'Required',
                'Agent::UserID'
            ],
        };
    }

Validation schemata can be (temporary) overwritten during param validation calls.

sub ValidateMethodParams

The /ValidateMethodParams() method will be called by the related object methods to validate input parameters.

    return if !$Self->ValidateMethodParams(
        Fields => [           # mandatory, an array of used schema fields to be validated.
            'TicketID',
            {
                Field  => 'UserID',
                Schema => [
                    {
                        Validator => 'DataType',
                        Arguments => 'PositiveInt'
                    },
                ],
            },
        ],
        Params => {           # mandatory, a hash reference containing the input keys and values.
            TicketID => 123,
            UserID   => 123,
        },
        AllowUnknownValues => 1,    # optional, allow values, that don't exist in the schema. Default: 0
        Silent             => 1,    # optional, skips adding log entries on errors. Default 0
    );

In case of any validation errors, this role loops over all occurred problems and generates log entries for them, unless the Silent parameter is set to true. The return values are always either undef or 1, to be able to perform early returns in the related methods.

Returns undef in case of errors and 1 in case of success.

Scroll to Top