This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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;
}
}
That's because the model.beforeValidate method is fired in the validation method itself, where the rules have already been passed from the model to the validator. Your new rules are added to the model's rules property, but they aren't used because the validator already has the rules by that point.
To sum it up, hook into an earlier event like beforeSave.
Thank you Luke but your suggestion didn't work.
I made one change beforeValidate
into beforeSave
.
I added some code to inspect model in beforeValidate
$model->bindEvent('model.beforeSave', function() use ($model) {
$model->rules['is_teacher'] = 'numeric';
$model->rules['credits'] = 'numeric';
trace_log("\n\nmodel.beforeSave was called for model=".$model);
});
$model->bindEvent('model.beforeValidate', function() use ($model) {
//$model->rules['is_teacher'] = 'numeric';
//$model->rules['credits'] = 'numeric';
trace_log("\n\nmodel.beforeValidate was called for model=".$model);
});
[2017-02-16 19:13:01] local.INFO:
model.beforeValidate was called for model={"id":2,"name":"a","email":"irekjar@gmail.com","permissions":null,"is_activated":false,"activated_at":null,"last_login":null,"created_at":"2017-02-13 08:58:10","updated_at":"2017-02-16 17:58:29","username":"irekjar@gmail.com","surname":"jargi","deleted_at":null,"last_seen":null,"is_guest":0,"is_superuser":0,"avatar":null,"userext":{"id":2,"user_id":2,"is_teacher":"0","credits":12222222,"created_at":"2017-02-13 08:58:16","updated_at":"2017-02-16 19:06:35"},"groups":[{"id":1,"name":"Guest","code":"guest","description":"Default group for guest users.","created_at":"2017-01-31 22:33:42","updated_at":"2017-01-31 22:33:42"},{"id":4,"name":"Friend","code":"friend","description":"Generalized friend group.","created_at":"2017-02-12 17:43:06","updated_at":"2017-02-12 17:43:06"}]}
in trace_log for model I get new userext columns
"userext":{"id":2,"user_id":2,"is_teacher":"0","credits":12222222,"created_at":"2017-02-13 08:58:16","updated_at":"2017-02-16 19:06:35"}
so, new columns are present in new userext model during validation but the validation still fails for new columns
Last updated
This is caused by bug in core code. The solutions is here: https://github.com/octobercms/october/issues/2311#issuecomment-249346975
1-5 of 5