Here is the Model code with inline validation rule.
namespace app\models;
use Yii;
class Country extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'country';
}
public function rules()
{
return [
...other rules...
['pan_no', 'checkPanCardUsers', 'skipOnEmpty' => false, 'skipOnError' => false]
];
}
public function checkPanCardUsers($attribute, $params)
{
...some condition ...
$this->addError($attribute, 'custom error message');
}
}
Controller code
public function actionSomeAction()
{
$model = new Country();
if ($model->load(Yii::$app->request->post()) {
if($model->validate()) {
$model->save();
}
}
return $this->render('country', [
'model' => $model,
]);
}
But the validation is not working.
die("Blablabla")to check.die($this->className());