参考:http://www.yiiframework.com/doc-2.0/guide-rest-resources.html#overriding-fields
我重写字段()以重命名用户模型中的字段。
GET - 响应数据字段按预期重命名
POST - 验证错误字段名称未重命名
public function rules()
{
return [
[['User_Name', 'Password', 'Auth_Token', 'User_Type_Id', 'User_Status_Id'], 'required'],
]
}
public function fields()
{
return [
'user_id' => 'User_Id',
'access_token' => 'Auth_Token',
'user_name' => 'User_Name',
'user_type' => 'userType.User_Type_Name',
'name' => 'userProfile.Fname',
];
}
[
{
"user_id": "1",
"access_token": "123",
"user_name": "kiran",
"user_type": "Customer",
"name": "customer"
}
]
[ { "field": "User_Name", "message": "User Name cannot be blank." }, { "field": "Password", "message": "Password cannot be blank." }, { "field": "Auth_Token", "message": "Auth Token cannot be blank." }, { "field": "User_Type_Id", "message": "User Type ID cannot be blank." }, { "field": "User_Status_Id", "message": "User Status ID cannot be blank." } ]
答案 0 :(得分:0)
参考:Yii2 REST create with fields()
覆盖addError()方法以更改错误字段名称
hadoopFile -> groupByKey -> map