MaskedInput
API reference › yii › widgets › MaskedInput
MaskedInput generates a masked text input.
MaskedInput is similar to Html::textInput() except that an input mask will be used to force users to enter
properly formatted data, such as phone numbers, social security numbers.
To use MaskedInput, you must set the $mask property. The following example
shows how to use MaskedInput to collect phone numbers:
echo MaskedInput::widget([ 'name' => 'phone', 'mask' => '999-999-9999',]);You can also use this widget in an ActiveForm using the widget()
method, for example like this:
<?= $form->field($model, 'from_date')->widget(\yii\widgets\MaskedInput::class, [ 'mask' => '999-999-9999',]) ?>The masked text field is implemented based on the jQuery input masked plugin.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$aliases | array| | Custom aliases to use. Default: null. | MaskedInput |
$attribute | string| | The model attribute that this widget is associated with. Default: null. | InputWidget |
$autoIdPrefix static | string | The prefix to the automatically generated widget IDs. Default: 'w'. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$clientOptions | array | The JQuery plugin options for the input mask plugin. Default: []. | MaskedInput |
$definitions | array| | Custom mask definitions to use. Default: null. | MaskedInput |
$field | ActiveField | Active input field, which triggers this widget rendering. Default: null. Available since 2.0.11. | InputWidget |
$id | string | Id of the widget. | yii\ |
$mask | string|yii\ | The input mask (e.g. ‘99/99/9999’ for date input). Default: null. | MaskedInput |
$model | yii\| | The data model that this widget is associated with. Default: null. | InputWidget |
$name | string| | The input name. Default: null. | InputWidget |
$options | array | The HTML attributes for the input tag. Default: ['class' => 'form-control']. | MaskedInput |
$type | string | The type of the input tag. Default: 'text'. Available since 2.0.6. | MaskedInput |
$value | string | The input value. Default: null. | InputWidget |
$view | yii\ | The view object that can be used to render views or view files. | yii\ |
$viewPath read-only | string | The directory containing the view files for this widget. | yii\ |
$_hashVar protected | string| | The hashed variable to store the pluginOptions Default: null. | MaskedInput |
$_jsCallbacks protected | string[] | The inputmask properties can be contained callbacks Default: ['oncomplete',. | MaskedInput |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterRun() | This method is invoked right after a widget is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeRun() | This method is invoked right before the widget is executed. | yii\ |
begin() static | Begins a widget. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() static | Ends a widget. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getId() | Returns the ID of the widget. | yii\ |
getView() | Returns the view object that can be used to render views or view files. | yii\ |
getViewPath() | Returns the directory containing the view files for this widget. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the widget. | MaskedInput |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
registerClientScript() | Registers the needed client script and options. | MaskedInput |
render() | Renders a view. | yii\ |
renderFile() | Renders a view file. | yii\ |
run() | Executes the widget. | MaskedInput |
setId() | Sets the ID of the widget. | yii\ |
setView() | Sets the view object to be used by this widget. | yii\ |
trigger() | Triggers an event. | yii\ |
widget() static | Creates a widget instance and runs it. | yii\ |
hashPluginOptions() protected | Generates a hashed variable to store the plugin clientOptions. | MaskedInput |
hasModel() protected | InputWidget | |
initClientOptions() protected | Initializes client options. | MaskedInput |
renderInputHtml() protected | Render a HTML input tag. | InputWidget |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_INIT | yii\ | An event that is triggered when the widget is initialized via init(). Event name: 'init'. Available since 2.0.11. | yii\ |
EVENT_BEFORE_RUN | yii\ | An event raised right before executing a widget. Event name: 'beforeRun'. Available since 2.0.11. | yii\ |
EVENT_AFTER_RUN | yii\ | An event raised right after executing a widget. Event name: 'afterRun'. Available since 2.0.11. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
PLUGIN_NAME | 'inputmask' | The name of the jQuery plugin to use for this widget. | MaskedInput |
Property details
Section titled “Property details”$aliases
Section titled “$aliases ”public array|null $aliases = nullCustom aliases to use.
Should be configured as maskAlias => settings, where
maskAliasis a string containing a text to identify your mask alias definition (e.g. ‘phone’) andsettingsis an array containing settings for the mask symbol, exactly similar to parameters as passed in$clientOptions.
$definitions
Section titled “$definitions ”public array|null $definitions = nullCustom mask definitions to use.
Should be configured as maskSymbol => settings, where
maskSymbolis a string, containing a character to identify your mask definition andsettingsis an array, consisting of the following entries:validator: string, a JS regular expression or a JS function.cardinality: int, specifies how many characters are represented and validated for the definition.prevalidator: array, validate the characters before the definition cardinality is reached.definitionSymbol: string, allows shifting values from other definitions, with thisdefinitionSymbol.
$mask
Section titled “$mask ”public string|array|yii\web\JsExpression $mask = nullThe input mask (e.g. ‘99/99/9999’ for date input).
The following characters can be used in the mask and are predefined:
a: represents an alpha character (A-Z, a-z)9: represents a numeric character (0-9)*: represents an alphanumeric character (A-Z, a-z, 0-9)[and]: anything entered between the square brackets is considered optional user input. This is based on theoptionalmarkersetting in$clientOptions.
Additional definitions can be set through the $definitions property.
$type
Section titled “$type ”public string $type = 'text'The type of the input tag.
Currently only ‘text’ and ‘tel’ are supported.
Since
2.0.6
Method details
Section titled “Method details”registerClientScript()
Section titled “registerClientScript() ”public function registerClientScript(): mixedRegisters the needed client script and options.
Returns
mixed
run()
Section titled “run() ”public function run(): string|voidExecutes the widget.
Returns
string|void: The rendering result may be directly “echoed” or returned as a string
hashPluginOptions()
Section titled “hashPluginOptions() ”protected function hashPluginOptions(yii\web\View $view): mixedGenerates a hashed variable to store the plugin clientOptions.
Helps in reusing the variable for similar options passed for other widgets on the same page. The following special data attribute will also be added to the input field to allow accessing the client options via javascript:
- ‘data-plugin-inputmask’ will store the hashed variable storing the plugin options.
initClientOptions()
Section titled “initClientOptions() ”protected function initClientOptions(): mixedInitializes client options.
Returns
mixed