我在yii2中有一个模态类,规则定义为
return [
[['charity_name', 'address', 'contact_name', 'title', 'dialling_code', 'phone_no', 'email', 'notes', 'added_by', 'created_at'], 'required'],
[['dialling_code', 'phone_no', 'added_by', 'created_at', 'status'], 'integer'],
[['charity_name', 'contact_name', 'email'], 'string', 'max' => 100],
[['address', 'title'], 'string', 'max' => 200],
[['notes'], 'string', 'max' => 255],
[['charity_name'], 'unique'],
[['added_by'], 'exist', 'skipOnError' => true, 'targetClass' => Users::className(), 'targetAttribute' => ['added_by' => 'id']],
[['dialling_code'], 'exist', 'skipOnError' => true, 'targetClass' => CountryDiallingCodes::className(), 'targetAttribute' => ['dialling_code' => 'id']],
];
我想显示消息为"电话号码应为数字"所以我将下面的函数放在扩展类中作为
public function rules() {
$rules = parent::rules();
$rules[] = [['phone_no'], 'integer', ['message'=>'Phone number should be numeric']];
return $rules;
}
但它似乎没有工作..请让我知道如何在扩展模态类中为属性添加自定义消息
答案 0 :(得分:0)
public function rules() {
$rules = parent::rules();
$rules[] = [['phone_no'], 'integer', 'message'=>'Phone number should be numeric'];
return $rules;
}
从[]
删除message
。
您也可以添加pattern
进行验证。
$rules[] = [['mobile_no'], 'match', 'pattern'=>"/^\d{10}$/", 'message'=>'Phone number should be numeric'];