yii2

Yii2 form validation - compare password repeat only when password field is filled

╄→尐↘猪︶ㄣ 提交于 2020-01-14 03:10:26
问题 My form validation uses the following rules: [['password', 'password_repeat'], 'required'], ['password_repeat', 'compare', 'compareAttribute' => 'password', 'message' => "Passwords don't match"], How to write rules for password_repeat to compare it with password only if user fill password field. If user skip password , validation for password_repeat should be also skipped. 回答1: You can use scenarios for that: public function rules() { return [ [['username', 'password'], 'required', 'on' =>

Yii2 Clean URLs & Action Parameters

血红的双手。 提交于 2020-01-13 13:10:00
问题 I've enabled clean URLs on my Yii2 application, but I can't seem to get arguments to pass to the action. I expect this: localhost/app/web/a/b/c/d To map to the following: AController->actionB($c, $d) It's not happening. Here's my .htaccess: RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php The relevant part of my web.php: 'urlManager' => [ 'class' => 'yii\web\UrlManager', 'enablePrettyUrl' => true, 'showScriptName' => false,

Yii2 php init command

牧云@^-^@ 提交于 2020-01-13 11:13:17
问题 What does php init command actually do? When I run this command in cmd after creating my project using this command: composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced I see many files generated in different locations like common, backend, frontend. `some@user:/var/www/html/yiiapp$ php init Yii Application Initialization Tool v1.0 Which environment do you want the application to be initialized in? [0] Development [1] Production Your choice [0-1, or "q" to quit] 0

Setting default model value depending on a scenario

牧云@^-^@ 提交于 2020-01-13 09:51:10
问题 I can't sort out why setting default value doesn't work for me. Here is my controller: class QueryController extends ActiveController { public $modelClass = 'app\models\Query'; public $createScenario = 'restCreate'; public $updateScenario = 'restUpdate'; I don't override the default createAction method. Model: class Query extends ActiveRecord {public function rules() { return [ [['userId', 'name', 'created', 'isOneTime', 'isArchived', 'settings', 'engine'], 'required'], [['userId'], 'integer'

Setting default model value depending on a scenario

非 Y 不嫁゛ 提交于 2020-01-13 09:50:24
问题 I can't sort out why setting default value doesn't work for me. Here is my controller: class QueryController extends ActiveController { public $modelClass = 'app\models\Query'; public $createScenario = 'restCreate'; public $updateScenario = 'restUpdate'; I don't override the default createAction method. Model: class Query extends ActiveRecord {public function rules() { return [ [['userId', 'name', 'created', 'isOneTime', 'isArchived', 'settings', 'engine'], 'required'], [['userId'], 'integer'

How to run console command in yii2 from web

偶尔善良 提交于 2020-01-13 08:26:48
问题 I have created a console command in console/controllers with SuggestionController . If i run command like php yii suggestions , its working. I want to know how to execute console command from web without any extensions of yii2 . 回答1: It can be done much simpler $oldApp = \Yii::$app; new \yii\console\Application([ 'id' => 'Command runner', 'basePath' => '@app', 'components' => [ 'db' => $oldApp->db, ], ); \Yii::$app->runAction('migrate/up', ['migrationPath' => '@yii/rbac/migrations/',

Render html in yii2 Gridview Widget

半世苍凉 提交于 2020-01-13 08:04:46
问题 That's how I am rendering the values on a grid view but instead of links I can see the textual value. How can I make it render html instead of text? 回答1: In link column configuration add: 'format' => 'html', or if you want some extra markup there 'format' => 'raw', In case of raw remember to encode values coming from outside users because it's not done automatically. 回答2: A better way of doing this in Yii. 'value' => function ($data) { return Html::a($data->name, [$data->url, 'someData' =>

Yii 2 nested forms

我怕爱的太早我们不能终老 提交于 2020-01-13 06:09:01
问题 Firstly sorry for my bad english! I'm using the Yii2-dynamicforms extension to nested forms in Yii 2. Following this guide, I was able to make Create Action work properly, but Update Action partially. Let me explain using my case... I can create a Mission (missao) with many Groups (grupo) and many Slots for each group; On Update, I can add some Slots for existing Groups, but when I try to add a new Group and new slots for this new Group, I got an error Undefined offset: 2 // for example The

Yii 2 nested forms

心已入冬 提交于 2020-01-13 06:07:16
问题 Firstly sorry for my bad english! I'm using the Yii2-dynamicforms extension to nested forms in Yii 2. Following this guide, I was able to make Create Action work properly, but Update Action partially. Let me explain using my case... I can create a Mission (missao) with many Groups (grupo) and many Slots for each group; On Update, I can add some Slots for existing Groups, but when I try to add a new Group and new slots for this new Group, I got an error Undefined offset: 2 // for example The

Yii2 + Redis as Database

China☆狼群 提交于 2020-01-13 05:26:31
问题 I want to use Yii2 and redis as database. So far, I got Redis ActiveRecord Class for Yii2 from Here. link1 link2 but, I got a problem. WHY THIS CLASS ADDS ANYTHING AS HASH IN REDIS???? Above that I cant Find in which pattern it Insert data. I add one user and it will add a user under user:xxx namespace and another record under s:user:xxx and so on but none of theme has any fields that i defined in attributes!! only contain IDs. I know that a Key-value type database and RDBMS are different and