Yii2 ActiveForm文本字段最大编号

Yii2 ActiveForm文本字段最大编号,yii2,active-form,Yii2,Active Form,我有如下字段 <?= $form->field($model, 'phone') ->textInput(['type' => 'number', 'maxlength' => 13]) ->label('Phone') ?> 为什么'maxlength'不起作用?如何让它工作 在此之前谢谢您它将不起作用,因为您正在为您的输入字段使用type=>number,您必须将其更改为type=>text <?= $form->field($m

我有如下字段

<?= $form->field($model, 'phone')
->textInput(['type' => 'number', 'maxlength' => 13])
->label('Phone') 
?>

为什么
'maxlength'
不起作用?如何让它工作


在此之前谢谢您

它将不起作用,因为您正在为您的输入字段使用
type=>number
,您必须将其更改为
type=>text

<?= $form->field($model, 'phone')
->textInput(['type' => 'text', 'maxlength' => 13])
->label('Phone') 
?>
除了上面的解决方案之外,您还可以选择使用自定义验证选项在模型内部验证

模型中手机的规则应该如下所示

[['phone'],'PhoneLimit']


public function PhoneLimit($attribute)
{
    if (!preg_match('/^[0-9]{13}$/', $this->$attribute)) {
        $this->addError($attribute, 'Please provide digits only no more than 13.');
    }
}

尝试输入数字->'type'=>'number','min'=>1',max'=>999

为什么要在输入字段中使用
type=number
有什么原因吗?为您添加了一个答案,看看这是否对您有帮助out@MuhammadOmerAslam好了,先生,请慢慢来
[['phone'],'PhoneLimit']


public function PhoneLimit($attribute)
{
    if (!preg_match('/^[0-9]{13}$/', $this->$attribute)) {
        $this->addError($attribute, 'Please provide digits only no more than 13.');
    }
}