Yii2 dektrium add new field to user model and change it in account form - yii2

I have added a new field to user model 'paypal' and need to change it in overridden account form.
Override user model
<?php
namespace common\models;
class User extends \dektrium\user\models\User
{
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['create'][] = 'paypal';
$scenarios['update'][] = 'paypal';
$scenarios['register'][] = 'paypal';
return $scenarios;
}
public function rules()
{
$rules = parent::rules();
$rules['paypalLength'] = ['paypal', 'string', 'max' => 255];
return $rules;
}
}
Override SettingsForm model
<?php
namespace common\models;
class SettingsForm extends \dektrium\user\models\SettingsForm
{
public $paypal;
public function rules()
{
$rules = parent::rules();
$rules['paypalLength'] = ['paypal', 'string', 'max' => 255];
return $rules;
}
}
Configure module
'user' => [
'class' => 'dektrium\user\Module',
'modelMap' => [
'User' => 'common\models\User',
'RegistrationForm' => 'common\models\RegistrationForm',
'SettingsForm' => 'common\models\SettingsForm',
],
'controllerMap' => [
...
And I have overridden account form view. When I'm trying to change paypal field in user/settings/account it doesn't change it. What should I do to make it work?
Thanks.

Also you must override view paths:
'view' => [
'theme' => [
'pathMap' => [
'#dektrium/user/views' => '#app/views/user',
],
],
],
And after that open #vendor/dektrium/yii2-user/views and make a folder on #app/views based on dektrium view folders. For example create a folder named admin (because you have on #vendor/dektrium/yii2-user/views a folder named admin) and create corresponded folder on app views, i.e. #app/views/admin.
After that create your view file that you want to change on #app/views/[dektrium-folder] and change it.

Related

How to set home page in Yii2

public function actionIndex() {
$this->layout = 'landing';
$loginForm = new LoginForm();
if (\Yii::$app->request->getIsPost()) {
$loginForm->load(\Yii::$app->request->post());
if ($loginForm->validate()) {
$user = $loginForm->getUser();
\Yii::$app->user->login($user);
return $this->goHome();
}
}
}
method goHome() sends to the home page. I have added '' => 'site/index' to the URL Manager earlier to send people to the SiteController and Index action, but Yii2 does not do anything. How to set up a correct home page rule?
You should write homeUrl parameter on config/main.php. For example:
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'homeUrl' => ['some/home-url-example'],
'modules' => [
...
],
...
]

Yii2 rules work only use scenario

I write two scenario in Yii2 comment model, when user logged on or is guest.
my rules is:
public function rules()
{
return [
[['user_id'], 'required', 'on' => self::SCENARIO_USER],
[['name', 'email'], 'required', 'on' => self::SCENARIO_GUEST],
[['post_id', 'body', 'date'], 'required'],
[['user_id', 'parent_id', 'post_id', 'status'], 'integer'],
[['body'], 'string'],
[['date'], 'safe'],
[['name', 'email', 'site'], 'string', 'max' => 256],
];
}
and senarios funtion :
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_GUEST] = ['name' , 'email'];
$scenarios[self::SCENARIO_USER] = ['user_id'];
return $scenarios;
}
I use it as follows:
$commentModel = Yii::$app->user->isGuest ? new Comment(['scenario' => Comment::SCENARIO_GUEST]) : new Comment(['scenario' => Comment::SCENARIO_USER]);
if guest view form, only name and email checked and user fill form, no field checked !
why other rules don't check? how to fix it?
Refer Yii2 Scenarios
The scenarios() method returns an array whose keys are the scenario names and values the corresponding active attributes. An active attribute can be massively assigned and is subject to validation.
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_GUEST] = ['name' , 'email', 'date', 'body', 'site', 'post_id'];
$scenarios[self::SCENARIO_USER] = ['user_id', 'date', 'body', 'site', 'post_id'];
return $scenarios;
}
If you use scenarios() you need to define all attributes allowed to assign in given scenario. So if you want to allow guest to edit also body and date you need something like:
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_GUEST] = ['name' , 'email', 'body', 'date'];
$scenarios[self::SCENARIO_USER] = ['user_id'];
return $scenarios;
}
If you have many "shared" attributes between scenarios, you probably should not override this method and define it in rules() only.
See more in scenarios documentation.

Yii2 How to auto register new user without using site/signup

I want to signup new user every time I add a new client. I do not want to manually register a new user using the web/site/signup form via the browser.
I am trying to auto register new users from ClientController and I am having no luck at all. No error returned, it is as if the code doesn't run at all.
I can easily register the same user through site controller at site/signup but can not at all auto register through another controller by calling SignupForm model. Please help
I have this in my client controller
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use yii\helpers\Html;
use frontend\models\Client;
use frontend\models\SignupForm;
class ClientController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
public function actionCreate()
{
$model = new Client;
if ($model->load(Yii::$app->request->post()))
{
$model->save();
$useremail = $model->Email;
$signupForm = new SignupForm();
$signupForm->username = $useremail;
$signupForm->password = $useremail;
$signupForm->email = $useremail;
$signupForm->created_at = time();
$signupForm->picture = null;
$signupForm->created_by = Yii::$app->session->get('USER_ID');
$NewUser = $signupForm->signup();
print_r($NewUser);
exit;
}
else{
return $this->render('create', [
'model' => $model
]);
}
}
}
and this is my SignupForm code:
<?php
namespace frontend\models;
use common\models\User;
use yii\base\Model;
use Yii;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
public $picture;
public $created_by;
/**
* #inheritdoc
*/
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
[['picture'], 'string'],
[['picture'],'default', 'value'=>null],
[['created_by'], 'integer'],
[['created_by'], 'default', 'value'=>Yii::$app->session->get('USER_ID')],
];
}
/**
* Signs user up.
*
* #return User|null the saved model or null if saving fails
*/
public function signup()
{
$this->created_by = Yii::$app->session->get('USER_ID');
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->created_by = $this->created_by;
$user->picture = $this->picture;
//Save user
$user->save(false);
return $user;
}
return null;
}
}

Yii2 multi captcha in one page

I have a page that contains multi captcha in separated forms form example login and register modals , etc.
if I use below codes the problem is that when refresh one captcha then another captcha will be unusable because they use same session varible:
1) Login
login modal (view):
echo Captcha::widget([
'id' => 'Login-captcha',
'name' => 'LoginModel[captcha]',
'captchaAction' => '/site/captcha'
]);
LoginModel :
public function rules()
{
return [
['captcha', 'captcha'],
];
}
2) Register
register modal (view):
echo Captcha::widget([
'id' => 'register-captcha',
'name' => 'RegisterModel[captcha]',
'captchaAction' => '/site/captcha'
]);
RegisterModel:
public function rules()
{
return [
['captcha', 'captcha'],
];
}
to solve session problem I used different captcha actions to set different session variables:
1)Login
login modal view:
echo Captcha::widget([
'id' => 'Login-captcha',
'name' => 'LoginModel[captcha]',
'captchaAction' => '/site/captcha-login'
]);
LoginModel :
public function rules()
{
return [
['captcha', 'captcha', 'captchaAction' => 'site/captcha-login',],
];
}
2) Register
register modal (view):
echo Captcha::widget([
'id' => 'register-captcha',
'name' => 'RegisterModel[captcha]',
'captchaAction' => '/site/captcha-register'
]);
RegisterModel:
public function rules()
{
return [
['captcha', 'captcha', 'captchaAction' => 'site/captcha-register',],
];
}
until now everything is ok but when I move sessions from regular php files to database by below config in commponent section of main config file:
'session' => [
'class' => 'yii\web\DbSession',
],
then captchas in the first page load not works and have to refresh them to work correctly.
what is the problem?
Try this, it is for yii1 but you can get the idea, more detail
public function rules()
{
return array(
...
array('verifyCode1', 'captcha', ...
array('verifyCode2', 'verifycaptcha2', ...
);
}
public function verifycaptcha2($attribute, $params)
{
$captcha2 = Yii::app()->getController()->createAction('captcha2nd');
if (!$captcha2->validate($this->verifyCode2, false))
{
$this->addError('verifyCode2', 'invalid captcha.');
}
}
Also see this

Yii2 validation rule specific to a scenarios

I have the following rules and scenarios
public function rules(){
return [
[['name','email','password'],'required'],
['email','myvalidation'],
['email','email'],
[['name', 'email', 'password'], 'required', 'on' => 'register'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['login'] = ['name','password','email'];//Scenario Values Only Accepted
return $scenarios;
}
I want the rule 'myvalidation' applied only to the login scenario and not at all in other cases.How this can be achieved in Yii2 ?
Remember you can also use "except". In example:
public function rules()
{
return [
[['first_name', 'email', 'phone', 'password'], 'required', 'except' => 'changepassword'],
[['password'], 'required', 'on' => 'changepassword']
]}
Just specify on property in this validation rule:
['email', 'myvalidation', 'on' => 'login'],