Validation trait for models

Public Properties

public bool $validationForced

validationForced is an internal marker to indicate if force option was used.

Protected Properties

protected Illuminate\Support\MessageBag $validationErrors

validationErrors message bag instance containing validation error messages

protected array $validationDefaultAttrNames

validationDefaultAttrNames default custom attribute names

Public Methods

public addValidationRule()

public addValidationRule($name, $definition): void

addValidationRule will append a rule to the stack and reset the value as a processed array

public errors()

public errors(): Illuminate\Support\MessageBag 

errors gets validation error message collection for the Model

public forceSave()

public forceSave($options = null, $sessionKey = null): bool 

forceSave the model even if validation fails

public initializeValidation()

public initializeValidation(): void

initializeValidation for this model

public isAttributeRequired()

public isAttributeRequired(
    string $attribute,
    bool $checkDependencies = true
): bool 

isAttributeRequired determines if an attribute is required based on the validation rules. checkDependencies checks the attribute dependencies (for required_if & required_with rules). Note that it will only be checked up to the next level, if another dependent rule is found then it will just assume the field is required.

public removeValidationRule()

public removeValidationRule($name, $definition): void

removeValidationRule removes a validation rule from the stack and resets the value as a processed array

public setValidationAttributeName()

public setValidationAttributeName(string $attr, string $name): void 

setValidationAttributeName programmatically sets the validation attribute names, will take lower priority to model defined attribute names found in $attributeNames

public setValidationAttributeNames()

public setValidationAttributeNames(array $attributeNames): void

setValidationAttributeNames programmatically sets multiple validation attribute names

public validate()

public validate(
    $rules = null,
    $customMessages = null,
    $attributeNames = null
): bool 

validate the model instance

public static validated()

public static validated(Closure|string $callback): void 

validated create a new native event for handling afterValidate()

public static validating()

public static validating(Closure|string $callback): void 

validating creates a new native event for handling beforeValidate()

Protected Methods

protected getRelationValidationValue()

protected getRelationValidationValue($relationName): void

getRelationValidationValue handles attachments that validate differently to their simple values

protected getValidationAttributes()

protected getValidationAttributes(): array 

getValidationAttributes returns the model data used for validation

protected static makeValidator()

protected static makeValidator(
    $connection = null
): Illuminate\Validation\Validator 

makeValidator instantiates the validator used by the validation process, depending if the class is being used inside or outside of Laravel. Optional connection string to make the validator use a different database connection than the default connection.

protected processRuleFieldNames()

protected processRuleFieldNames(array $rules): array 

processRuleFieldNames processes field names in a rule array Converts any field names using array notation (ie. field[child]) into dot notation (ie. field.child)

protected processValidationRules()

protected processValidationRules($rules): void

protected processValidationUniqueRule()

protected processValidationUniqueRule(
    string $definition,
    string $fieldName
): string 

processValidationUniqueRule rebuilds the unique validation rule to force for the existing key exclusion for existing models. It also checks for unique rules without a table name and includes the table name, since this is required by Laravel but not October.