namespace backend\modules\article\models;use yii\db\ActiveRecord;class Category extends ActiveRecord{ public static function tableName() { return 'category'; } public function rules() { return [ [['categoryName','categoryDir'],'required','message'=>'不可為空'] ]; }}
上面定義的錯誤資訊,輸出都是"不可為空",不能根據categoryName、categoryDir的不同而顯示不同的資訊,想讓categoryName輸出"categoryName不可為空",讓categoryDir輸出"categoryDir不可為空",不知道有沒有相關的變數可以做到?
回複內容:
namespace backend\modules\article\models;use yii\db\ActiveRecord;class Category extends ActiveRecord{ public static function tableName() { return 'category'; } public function rules() { return [ [['categoryName','categoryDir'],'required','message'=>'不可為空'] ]; }}
上面定義的錯誤資訊,輸出都是"不可為空",不能根據categoryName、categoryDir的不同而顯示不同的資訊,想讓categoryName輸出"categoryName不可為空",讓categoryDir輸出"categoryDir不可為空",不知道有沒有相關的變數可以做到?
我想你需要的是這個:
[['username', 'password'], 'required', 'message' => '{attribute}不可為空!'],
public function rules() { return [ ['categoryName','required','message'=>'categoryName不可為空'], ['categoryDir','required','message'=>'categoryDir不可為空'] ]; }
這樣可以嗎?