I am trying to validate new fields in the new table userext that extends rainlab.user table. For some reason columns from my new table aren't validated but those new columns are updated correctly in the database. Values from the rainlab.user table validate correctly. Can somebody help me with this problem?
Plugin.php:
...
use RainLab\User\Models\User as UserModel;
use RainLab\User\Controllers\Users as UsersController;
use Poly\UserExt\Models\UserExt as UserExtModel;
...
public function boot()
{
UserModel::extend(function(UserModel $model) {
$model->hasOne['userext'] = ['Poly\UserExt\Models\UserExt'];
$model->bindEvent('model.beforeValidate', function() use ($model) {
$model->rules['is_teacher'] = 'numeric';
$model->rules['credits'] = 'numeric';
});
});
UsersController::extendFormFields(function($form, $model, $context){
if(!$model instanceof UserModel)
{ return; }
if(!$model->exists) return;
UserExtModel::getFromUser($model);
$model->hasOne['userext'] = ['Poly\UserExt\Models\UserExt'];
$form->addTabFields([
'userext[is_teacher]'=> [
'label' => 'Is a teacher?',
'tab' => 'account',
'type' => 'number',
],
'userext[credits]'=> [
'label' => 'Credits',
'tab' => 'account',
'type' => 'number',
],
]);
$model->rules['is_teacher'] = 'numeric';
$model->rules['credits'] = 'numeric';
});
}
...
Userext.php:
<?php namespace Poly\UserExt\Models;
use Model;
class UserExt extends Model
{
use \October\Rain\Database\Traits\Validation;
public $table = 'poly_userext_userexts';
protected $guarded = ['*'];
protected $fillable = [
'is_teacher',
'credits',
];
public $rules = [
'is_teacher' => ['numeric'],
'credits' => ['numeric'],
];
public $belongsTo = [
'user' => ['RainLab\User\Models\User']
];
public static function getFromUser($user)
{
if($user->userext)
return $user->userext;
$userext = new static;
$userext->user = $user;
$userext->save();
$user->userext = $userext;
return $userext;
}
}