yii2

Yii2, GridView, save filters value in session

浪子不回头ぞ 提交于 2019-12-24 14:24:56
问题 Is there any solution to save filters (in session) so user can see "last used filters" when he come back? Please link in manual or docs, or your code. I found nothing searching this question. 回答1: This is somewhat a hack, but this saves filter, page and sorting for me. Place this in your Controller, assuming your Model is called Customer : $searchModel = new CustomerSearch(); $params = Yii::$app->request->queryParams; if (count($params) <= 1) { $params = Yii::$app->session['customerparams'];

RBAC for basic yii2 template

﹥>﹥吖頭↗ 提交于 2019-12-24 13:59:20
问题 i want to create an application where only admin can perform all the crud operations but other users can only create and update posts. I did find tutorials based on rbac but only for advanced template but i am using the basic template. I also followed the yii2 guide but i did not understood it very well like executing ./yii rbac/init console command. How do i do it? 回答1: first of all create a Helper Class called PermissionHelpers in your model folder: namespace app\models; use Yii; class

Yii2: loading fixtures for specific functional tests

不羁的心 提交于 2019-12-24 13:19:34
问题 Short version: Using Yii2 Advanced Application Template and Codeception functional tests, is there a way to load a specific fixture only before a particular scenario (Cest class)? (Background / side note: I have a vague feeling I might be approaching this wrong, since arguably, if tests should be completely isolated with regards to db, they should be unit tests and not functional ones. However, due to time constraints on the project I started with functional tests while postponing unit

Yii2: ActiveForm: combine rules / multiple validation on one field

萝らか妹 提交于 2019-12-24 12:42:29
问题 LoginForm: public function rules() { return [ // username and password are both required [['username', 'password'], 'required'], // username should be a number and of 8 digits [['username'], 'number', 'message'=>'{attribute} must be a number'], [['username'], 'string', 'length' => 8], // password is validated by validatePassword() ['password', 'validatePassword'], ]; } /** * Validates the password. * This method serves as the inline validation for password. * * @param string $attribute the

Yii2 user identity loss after page redirection

荒凉一梦 提交于 2019-12-24 11:29:52
问题 I was working on user login in yii2, but instead of using active record there's another server handle the validation of user's username and password. So the following is what I did to work around: in LoginForm.php , I made some changes in validatePassword() which apiRest() is a method I used to send request to the remote server in charge of the validation. if (!$this->hasErrors()) { $json = '{ "username": "' . $this->username . '", "password": "' . $this->password . '" }'; $response = $this-

Yii2: Signup page shows error “403 forbidden you are not allowed to access the page”

北战南征 提交于 2019-12-24 11:04:43
问题 I have my signup controller code. public function actionSignup() { $model = new SignupForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($user = $model->signup()) { $this->layout = 'index'; return $this->goHome(); } } $this->layout = 'index'; return $this->render('signup', [ 'model' => $model, ]); } When i, through url pass the address as index.php?r=site/login then there appears login form & that works fine. But when i pass as index.php?r=site/signup then it

Yii2 model get method for field containing underscore

£可爱£侵袭症+ 提交于 2019-12-24 10:39:42
问题 Model name: listing ; Field name: contact_name . User input involved, so I want to format the output, consistently, with some variant of getContactName , i.e. any call to $model->contact_name returns the formatted output. Yes, I can use, for example, getContactName and $model->contactName , but I have not found any variant of getcontact_name that will work with the default $model->contact_name . I'm aware that I could configure Gii to create some additional functions, and various other

How to get previous and next row model ID in yii2 gridview data row

早过忘川 提交于 2019-12-24 10:37:59
问题 While in GridView widget and displaying ActiveDataprovider results that i fetched from a customized query, i want to get next and previous row $model->id while displaying the records, what i need to do is to show links to previous and next posts while in reading mode, my gridview already generates link to the post view but there doesnt seem to be an option to get next DB row's record ID i have already tried to use default values in the function available but they dont seem to hold any related

Yii2: Conditional fill of Json data

≯℡__Kan透↙ 提交于 2019-12-24 10:35:34
问题 I have a table ipd_charges like id doctor_name charges_cash charges_cashless 1 1 300 600 2 2 200 400 and in my controller the code is like this which is working fine public function actionCharges($id){ $model= \app\models\IpdCharges::findOne(['doctor'=>$id]); return \yii\helpers\Json::encode([ 'visit_charges'=>$model->charges_cash, ]); } Now what I want is to fill the data on condition and tried to modify the above code which is only returning the first condition in either case public

Yii2 POST image to model in API without Yii2 Naming convention

萝らか妹 提交于 2019-12-24 09:57:47
问题 I'm creating an endpoint for a mobile application to send a image to the server. I'm posting the image with the POSTMAN extension for chrome. The image is in the $_FILES variable, and named image . How can I load this image into a model, or the UploadedFile class? The $model->load(Yii::$app->request->post()) line does not correctly load the file, as it is not in Yii2's naming convention for forms. It's currently returning: { "success": false, "message": "Required parameter 'image' is not set.