Skip to content


craft\validators\UsernameValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject

Class UsernameValidator.

View source

Public Properties

attributeNamesarray – Attribute names.
attributesarray, string – Attributes to be validated by this validator.
behaviorsyii\base\Behavior – List of behaviors attached to this component.
builtInValidatorsarray – List of built-in validators (name => class or configuration)
enableClientValidationboolean – Whether to enable client-side validation for this validator.
exceptarray, string – Scenarios that the validator should not be applied to.
isEmptycallable, null – A PHP callable that replaces the default implementation of isEmpty().
messagestring, null – The user-defined error message.
onarray, string – Scenarios that the validator can be applied to.
skipOnEmptyboolean – Whether this validation rule should be skipped if the attribute value is null or an empty string.
skipOnErrorboolean – Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules.
validationAttributesarray, null – List of attribute names.
whencallable, null – A PHP callable whose return value determines whether this validator should be applied.
whenClientstring, null – A JavaScript function name whose return value determines whether this validator should be applied on the client-side.

Public Methods

__call()Calls the named method which is not a class method.
__clone()This method is called after the object is created by cloning an existing one.
__get()Returns the value of a component property.
__isset()Checks if a property is set, i.e. defined and not null.
__set()Sets the value of a component property.
__unset()Sets a component property to be null.
addError()Adds an error about the specified attribute to the model object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
behaviors()Returns a list of behaviors that this component should behave as.
canGetProperty()Returns a value indicating whether a property can be read.
canSetProperty()Returns a value indicating whether a property can be set.
className()Returns the fully qualified name of this class.
clientValidateAttribute()Returns the JavaScript needed for performing client-side validation.
createValidator()Creates a validator object.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.
getAttributeNames()Returns cleaned attribute names without the ! character at the beginning.
getBehavior()Returns the named behavior object.
getBehaviors()Returns all behaviors attached to this component.
getClientOptions()Returns the client-side validation options.
getValidationAttributes()Returns a list of attributes this validator applies to.
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.
hasMethod()Returns a value indicating whether a method is defined.
hasProperty()Returns a value indicating whether a property is defined for this component.
init()Initializes the object.
isActive()Returns a value indicating whether the validator is active for the given scenario and attribute.
isEmpty()Checks if the given value is empty.
off()Detaches an existing event handler from this component.
on()Attaches an event handler to an event.
trigger()Triggers an event.
validate()Validates a given value.
validateAttribute()Validates a single attribute.
validateAttributes()Validates the specified object.
validateValue()Validates a value.


Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

View source


  • $value (mixed) – The data value to be validated.


array, null – The error message and the array of parameters to be inserted into the error message.

if (!$valid) {
    return [$this->message, [
        'param1' => $this->param1,
        'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
        'mimeTypes' => implode(', ', $this->mimeTypes),
        'param4' => 'etc...',

return null;

for this example message template can contain {param1}, {formattedLimit}, {mimeTypes}, {param4}

Null should be returned if the data is valid.


Protected Methods

formatMessage()Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available.