i want make form for change password, with field old password and new password like this
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'old_password')->textInput()->label('Old Password ') ?>
<?= $form->field($model, 'password')->passwordInput()->label(' New Password ') ?>
<?= $form->field($model, 'confirm_password')->passwordInput()->label('Confirm new password') ?>
<div class="form-group">
<?= Html::submitButton('Change', ['class' => 'btn btn-primary btn-fill']) ?>
</div>
<?php ActiveForm::end(); ?>
I have controller like this
public function actionPass()
{
$id=Yii::$app->user->id;
$modelUser = $this->findModel($id);
try {
$model = new \frontend\models\ChangePasswordForm($id);
} catch (InvalidParamException $e) {
throw new \yii\web\BadRequestHttpException($e->getMessage());
}
if ($model->load(\Yii::$app->request->post())) {
if ( $model->validate() && $model->changePassword()) {
Yii::$app->session->setFlash('success', 'Password success to change ');
return $this->render('index', [
'model' => $model,
'modelUser' => $modelUser,
]);
} else {
Yii::$app->session->setFlash('error', 'Password failed to change!');
return $this->render('index', [
'model' => $model,
'modelUser' => $modelUser,
]);
}
}
i have model to manage change password process
class ChangePasswordForm extends Model
{
public $id;
public $password;
public $confirm_password;
public $old_password;
/**
* #var \common\models\User
*/
private $_user;
/**
* Creates a form model given a token.
*
* #param string $token
* #param array $config name-value pairs that will be used to initialize the object properties
* #throws \yii\base\InvalidParamException if token is empty or not valid
*/
public function __construct($id, $config = [])
{
$this->_user = User::findIdentity($id);
if (!$this->_user) {
throw new InvalidParamException('Unable to find user!');
}
$this->id = $this->_user->id;
parent::__construct($config);
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['password','confirm_password'], 'required'],
[['password','confirm_password'], 'string', 'min' => 8],
['confirm_password', 'compare', 'compareAttribute' => 'password'],
['old_password','findPasswords'],
];
}
/**
* Changes password.
*
* #return boolean if password was changed.
*/
public function changePassword()
{
$user = $this->_user;
$user->setPassword($this->password);
//$user->ps=$this->password;
return $user->save();
}
public function findPasswords($attribute, $params, $validator)
{
$user = User::findOne(Yii::$app->user->id);
if (!Yii::$app->getSecurity()->validatePassword($this->old_password, $user->password_hash))
$this->addError($attribute, 'The Old Password not match.');
}
}
the error validation for new password and confirm new password works, but not for old password field, the error not show in form when i fill password that not match with old password. Any help?
Shouldn't it be like this?
public function findPasswords($attribute, $params, $validator)
{
$user = User::findOne(Yii::$app->user->id);
if (!Yii::$app->getSecurity()->validatePassword($this->old_password, $user->password_hash))
$this->addError($attribute, 'The Old Password not match.');
return false;
}
return true;
}
Related
I'm trying to upload multiple images when I create a Post, but only one Image is stored in database, in server all images are saved but in my table only first image is saved but not all so I created function for upload
// Function to upload images
public function CreateGallery($model_Post, $model_Gallery)
{
$model_Post->Post_id = md5(microtime());
$model_Post->User_id = Yii::$app->user->id;
$GLRID = md5(uniqid());
// file
$GFolder = 'upload/images/'. $model_Post->Post_id .'/' ;
mkdir ($GFolder, 0777, true);
if ( $model_Post->save() ){
$model_Gallery->GalleryFile = UploadedFile::getInstances($model_Gallery, 'GalleryFile');
$ly_GalName = uniqid();
foreach ($model_Gallery->GalleryFile as $GalleryImage) {
++$ly_GalName;
$model_Gallery->Gallery_id = ++$GLRID;
$model_Gallery->User_id = $model_Post->User_id;
$model_Gallery->Post_id = $model_Post->Post_id;
$GalleryImage->saveAs($GFolder . $GalName . '.' . $GalleryImage->extension);
$model_Gallery->Gallery_image = $GFolder . $GalName . '.' . $GalleryImage->extension;
}
$model_Gallery->save(false);
}
}
and inside my controller I add the function I created
public function actionCreate()
{
$model_Post = new Post();
$model_Gallery = new Gallery;
$actions_UploadImages = new ActionsUploadImages();
if ($model->load(Yii::$app->request->post()) ) {
$actions_UploadImages->CreateGallery($model_Post, $model_Gallery)
return $this->redirect(['view', 'id' => $model_Post->Post_id]);
} else {
return $this->render('create', [
'model_Post' => $model_Post,
'model_Gallery' => $model_Gallery,
]);
}
}
and my views/create my code is
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model_Post, 'Post_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model_Gallery, 'GalleryFile[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<?= $form->field($model_Post, 'Post_text')->textarea(['rows' => 2]) ?>
<?= $form->field($model_Post, 'Permission_id')->dropdownList($model_Permission->PermissionList()) ?>
<div class="form-group">
<?= Html::submitButton($model_Post->isNewRecord ? 'Create' : 'Update', ['class' => $model_Post->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
I tried to change gallery_id to AUTO_INCREMENT but still the same problem, I tried to remove if ( $model_Post->save() ), but I get error from database. Only like it's is now it's save all images on server but in table save only one image.
//// gallery model :
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "gallery".
*
* #property string $Gallery_id
* #property string $Gallery_image
* #property string $Post_id
* #property string $User_id
*
* #property Post $post
* #property Userlogin $user
*/
class Gallery extends \yii\db\ActiveRecord
{
public $GalleryFile;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'gallery';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['Gallery_id', 'Post_id', 'User_id'], 'required'],
[['Gallery_id'], 'string', 'max' => 200],
[['Gallery_image'], 'string', 'max' => 350],
[['Post_id', 'User_id'], 'string', 'max' => 300],
[['GalleryFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, jpeg, gif', 'maxFiles' => 5],
[['Post_id'], 'exist', 'skipOnError' => true, 'targetClass' => Post::className(), 'targetAttribute' => ['Post_id' => 'Post_id']],
[['User_id'], 'exist', 'skipOnError' => true, 'targetClass' => Userlogin::className(), 'targetAttribute' => ['User_id' => 'User_id']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'Gallery_id' => 'Gallery ID',
'Gallery_image' => 'Gallery Image',
'Post_id' => 'Post ID',
'User_id' => 'User ID',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getPost()
{
return $this->hasOne(Post::className(), ['Post_id' => 'Post_id']);
}
/**
* #return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(Userlogin::className(), ['User_id' => 'User_id']);
}
/**
* #inheritdoc
* #return \app\queries\GalleryQuery the active query used by this AR class.
*/
public static function find()
{
return new \app\queries\GalleryQuery(get_called_class());
}
}
you need write a new model en the for each , like that
Function
Create a new model object in the for each .
Set the $model_Gallery->save(false); in inside the foreach
// Function to upload images
public function CreateGallery($model_Post, $model_Gallery)
{
$model_Post->Post_id = md5(microtime());
$model_Post->User_id = Yii::$app->user->id;
$GLRID = md5(uniqid());
// file
$GFolder = 'upload/images/' . $model_Post->Post_id . '/';
mkdir($GFolder, 0777, true);
if ($model_Post->save()) {
$model_Gallery->GalleryFile = UploadedFile::getInstances($model_Gallery, 'GalleryFile');
$ly_GalName = uniqid();
foreach ($model_Gallery->GalleryFile as $GalleryImage) {
$model_GalleryImage = new Gallery;
++$ly_GalName;
$model_GalleryImage->Gallery_id = ++$GLRID;
$model_GalleryImage->User_id = $model_Post->User_id;
$model_GalleryImage->Post_id = $model_Post->Post_id;
$GalleryImage->saveAs($GFolder . $GalName . '.' . $GalleryImage->extension);
$model_GalleryImage->Gallery_image = $GFolder . $GalName . '.' . $GalleryImage->extension;
$model_GalleryImage->save(false); //here , remove 'false' to work validations
}
}
}
Controller
when the action load the data ,it should not be like that ?
if ($model_Gallery->load(Yii::$app->request->post()) and $model_Post->load(Yii::$app->request->post())) {
//the rest of the code
}
Please note that we use save(false) to skip over validations inside the models as the user input data... with the user input
All you have to do is iterate $_FILES object using foreach loop and create new model every time. To validate code manage flag for that
I have a model named Taluka. I am supposed to select District and enter as many talukas for that specific district. Every thing is working, but when I enter multiple talukas, only last taluka is getting saved in database table. I have also tried the solution given in Yii2 Insert multiple records of a same table
But the error I received is "Call to a member function isAttributeRequired() on array"
Model:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "taluka".
*
*/
class Taluka extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public $talukas=[];
public static function tableName()
{
return 'taluka';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['DistrictId', 'Taluka'], 'required'],
[['DistrictId'], 'integer'],
[['talukas'], 'required'],
[['Taluka'], 'string', 'max' => 100],
[['DistrictId'], 'exist', 'skipOnError' => true, 'targetClass' => District::className(), 'targetAttribute' => ['DistrictId' => 'DistrictId']],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'TalukaId' => 'Taluka ID',
'DistrictId' => 'District',
'talukas' => 'Taluka',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getDistrict()
{
return $this->hasOne(District::className(), ['DistrictId' => 'DistrictId']);
}
}
Controller:
<?php
namespace app\controllers;
use Yii;
use app\models\Taluka;
use app\models\TalukaSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\models\District;
use app\models\Model;
/**
* TalukaController implements the CRUD actions for Taluka model.
*/
class TalukaController extends Controller
{
/**
* #inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Taluka models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new TalukaSearch();
$dataProvider = $searchModel->search(Yii::$app->request-`>queryParams);`
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Taluka model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Taluka model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Taluka();
if ($model->load(Yii::$app->request->post()) ) {
echo $model->DistrictId;
$talukalist = $model->talukas;
if(is_array($talukalist))
{
foreach($talukalist as $v)
{
}
}
foreach($talukalist as $talukalist)
{
//echo $talukalist;
$model->Taluka = $talukalist;
echo $model->Taluka;
$model->save(false);
}
//return $this->redirect(['view', 'id' => $model->TalukaId]);
}
else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Taluka model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->TalukaId]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Taluka model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Taluka model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Taluka the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Taluka::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
View:
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use app\models\District;
use yii\helpers\ArrayHelper;
use unclead\multipleinput\MultipleInput;
/* #var $this yii\web\View */
/* #var $model app\models\Taluka */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="taluka-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form', 'layout' => 'horizontal',
'fieldConfig' => [
'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
'horizontalCssClasses' => [
'label' => 'col-sm-5',
//'offset' => 'col-sm-offset-2',
//'wrapper' => 'col-sm-7',
'error' => '',
'hint' => '',
],
],]);?>
<div class="panel panel-primary " >
<div class="panel panel-heading"><font size="3"><b>Taluka</b></font></div>
<div class="row">
<div class="col-sm-5">
<?= $form->field($model, 'DistrictId')->dropDownList(ArrayHelper::map(District::find()->all(),'DistrictId','District'), ['prompt' => 'Select District']) ?>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<?php
echo $form->field($model, 'talukas')->widget(MultipleInput::className(), [
'max' => 500,
'min' => 1, // should be at least 2 rows
'allowEmptyList' => false,
//'enableGuessTitle' => true,
//'addButtonPosition' => MultipleInput::POS_HEADER // show add button in the header
]);
?>
</div>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
At first, let’s understand why you have an error "Call to a member function isAttributeRequired() on array". The reason is in rules method:
public function rules()
{
return [
[['DistrictId', 'Taluka'], 'required'],
[['DistrictId'], 'integer'],
[['talukas'], 'required'],// <-- This line causes an error. Reqired filed in ActiveRecord model could not be an array.
[['Taluka'], 'string', 'max' => 100],
[['DistrictId'], 'exist', 'skipOnError' => true, 'targetClass' => District::className(), 'targetAttribute' => ['DistrictId' => 'DistrictId']],
];
}
So it's better to remove [['talukas'], 'required'] from rules(). It is a custom field so it is not checked by ActiveRecord logic.
Also, there is a strange logic in actionCreate(). Keep in mind, that you've added a custom field to your model and don't fill Taluka, which is required, according to you model rules(). So you can't just load $_POST into model, and need to iterate through talukas to create a new record for each:
public function actionCreate()
{
$model = new Taluka();
if ($model->load(Yii::$app->request->post())) {
$talukaList = $model->talukas;
if (is_array($talukaList)) {
foreach ($talukaList as $taluka) {
$talukaRecord = new Taluka();
$talukaRecord->DistrictId = $model->DistrictId;
$talukaRecord->Taluka = $taluka;
$talukaRecord->save();
}
}
return $this->redirect(['view', 'id' => $talukaRecord->TalukaId]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
I assume, that TalukaId is an autoincrement primary key. After saving all records, it will redirect you to the last created taluka.
In my basic app project, I'm trying to integrate the signup form, I'm getting this error:
Invalid validation rule: a rule must specify both attribute names and validator type.
My code is here.
SignUpForm.php
<?php
namespace app\models;
use yii\base\Model;
use app\models\User;
/**
* Signup form
*/
class SignupForm extends Model
{
public $user_fname;
public $user_email;
public $user_password_hash;
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_fname','user_email', 'user_password_hash'], 'required'],
// rememberMe must be a boolean value
['user_password_hash','match','pattern'=>'$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$','message'=>'Password must have atleast 1 uppercase and 1 number '],
[['user_password_hash'],'string','min'=>6],
//email validation
['user_email','email']
[['user_email'], 'string', 'max' => 255],
[['user_fname'], 'string', 'max' => 45],
];
}
/**
* Signs user up.
*
* #return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new SimUser();
$user->user_fname = $this->user_fname;
$user->user_email = $this->user_email;
$user->setPassword($this->user_password_hash);
$user->generateAuthKey();
$user->save();
return $user;
}
}
Site Controller.php/signup method
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
my view file
<?php
/* #var $this yii\web\View */
/* #var $form yii\bootstrap\ActiveForm */
/* #var $model \frontend\models\SignupForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to signup:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'user_fname')->textInput() ?>
<?= $form->field($model, 'user_email')->textInput() ?>
<?= $form->field($model, 'user_password_hash')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
My User file, this s the file to which the data has to be posted.. I retrieve login details from here.
namespace app\models;
use Yii;
/**
* This is the model class for table "sim_user".
*
* #property integer $user_id
* #property string $user_email
* #property string $user_password_hash
* #property string $user_fname
* #property string $user_lname
* #property integer $user_company
* #property string $user_authcode
* #property integer $user_suspended
* #property string $user_created
* #property integer $user_deleted
* #property string $user_auth_key
* #property string $user_access_token
*/
class SimUser extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'sim_user';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['user_email', 'user_password_hash']],
[['user_company', 'user_suspended', 'user_deleted'], 'integer'],
[['user_created'], 'safe'],
[['user_email'], 'string', 'max' => 255],
[['user_password_hash'], 'string', 'max' => 20],
[['user_fname', 'user_lname'], 'string', 'max' => 45],
[['user_auth_key'], 'string', 'max' => 32],
[['user_access_token'], 'string', 'max' => 100],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_email' => Yii::t('app', 'User Email'),
'user_password_hash' => Yii::t('app', 'User Password Hash'),
'user_fname' => Yii::t('app', 'User Fname'),
'user_lname' => Yii::t('app', 'User Lname'),
'user_company' => Yii::t('app', 'User Company'),
'user_authcode' => Yii::t('app', 'User Authcode'),
'user_suspended' => Yii::t('app', 'User Suspended'),
'user_created' => Yii::t('app', 'User Created'),
'user_deleted' => Yii::t('app', 'User Deleted'),
'user_auth_key' => Yii::t('app', 'User Auth Key'),
'user_access_token' => Yii::t('app', 'User Access Token'),
];
}
public function getAuthKey() {
return $this->user_auth_key;
}
public function getId() {
return $this->user_id;
}
public function validateAuthKey($authKey) {
return $this->user_auth_key = $authkey;
}
public static function findIdentity($id) {
return self::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null) {
return $this->user_access_token;
}
public static function findByUsername($email){
return self::findOne(['user_email'=>$email]);
}
public function validatePassword($password){
return $this->user_password_hash === $password;
}
public function setPassword($password)
{
$this->user_password_hash = Yii::$app->security->generatePasswordHash($password);
}
public function generateAuthKey()
{
$this->user_auth_key = Yii::$app->security->generateRandomString();
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert))
{
if ($this->isNewRecord)
{
$this->user_auth_key = \Yii::$app->security->generateRandomString();
}
return true;
}
return false;
}
}
First rule in your SimUser class is probably causing this error.
return [
[['user_email', 'user_password_hash']],
...
];
Maybe you mean:
return [
[['user_email', 'user_password_hash'], 'required'],
...
];
In you code you have:
['user_email', 'email']
should be:
[['user_email', 'email'], 'email'],
or
[['user_email'], 'email'],
When I'm selecting First Category - I can see the data in firebug that should populate in the secon level but it's instead populating 0,1,2,3,4 and I'm unable to select it.
My _form is
<?php
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use yii\web\View;
use frontend\assets\XyzAsset;
use yii\helpers\ArrayHelper;
use dosamigos\datepicker\DatePicker;
use kartik\select2\Select2;
use frontend\modules\production\models\Productbatch;
use frontend\modules\production\models\Productnames;
use kartik\depdrop\DepDrop;
use yii\helpers\Json;
//XyzAsset::register($this);
/* #var $this yii\web\View */
/* #var $model frontend\modules\production\models\Production */
/* #var $form yii\widgets\ActiveForm */
?>
<div class="production-form">
<?php $form = ActiveForm::begin(); ?>
<!--<?= Html::a('Select Product', ['/production/productbatch/index'], ['class'=>'btn btn-primary']) ?> -->
<?= $form->field($model, 'productiondate')->widget(
DatePicker::className(), [
// inline too, not bad
'inline' => false,
// modify template for custom rendering
//'template' => '<div class="well well-sm" style="background-color: #fff; width:250px">{input}</div>',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
<!-- echo CHtml::button("(+)",array('title'=>"Select Product",'onclick'=>'js:selectproductforproduction();')); -->
<?= $form->field($model, 'productname')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'language' => 'en',
'options' => ['placeholder' => 'Select Product Name', 'id' => 'cat-id'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'batchno')->widget(DepDrop::classname(), [
'options'=>['id'=>'subcat-id'],
'pluginOptions'=>[
'depends'=>['cat-id'],
'placeholder'=>'Select BatchNo',
'url'=>Url::to(['/production/productbatch/subcat'])
]
]); ?>
<?= $form->field($model, 'prodqty')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Controller -
<?php
namespace frontend\modules\production\controllers;
use Yii;
use frontend\modules\production\models\Productbatch;
use frontend\modules\production\models\ProductbatchSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\Json;
/**
* ProductbatchController implements the CRUD actions for Productbatch model.
*/
class ProductbatchController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Productbatch models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new ProductbatchSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Productbatch model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Productbatch model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Productbatch();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->itemid]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Productbatch model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->itemid]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Productbatch model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
public function actionSubcat() {
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = Productbatch::getBatchNo($cat_id);
// the getSubCatList function will query the database based on the
// cat_id and return an array like below:
// [
// ['id'=>'<sub-cat-id-1>', 'name'=>'<sub-cat-name1>'],
// ['id'=>'<sub-cat_id_2>', 'name'=>'<sub-cat-name2>']
// ]
echo Json::encode(['output'=>$out, 'selected'=>'']);
return;
}
}
echo Json::encode(['output'=>'', 'selected'=>'']);
}
/**
* Finds the Productbatch model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Productbatch the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Productbatch::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Model
<?php
namespace frontend\modules\production\models;
use Yii;
/**
* This is the model class for table "productbatch".
*
* #property integer $itemid
* #property string $productname
* #property string $batchno
* #property string $mfgdate
* #property string $expdate
* #property double $mrp
* #property double $rate
*
* #property Productnames $productname0
*/
class Productbatch extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'productbatch';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['batchno'], 'string'],
[['mfgdate', 'expdate'], 'safe'],
[['mrp', 'rate'], 'number'],
[['productname'], 'string', 'max' => 25]
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'itemid' => 'Itemid',
'productname' => 'Productname',
'batchno' => 'Batchno',
'mfgdate' => 'Mfgdate',
'expdate' => 'Expdate',
'mrp' => 'Mrp',
'rate' => 'Rate',
];
}
/**
* #return \yii\db\ActiveQuery
*/
public function getProductname0()
{
return $this->hasOne(Productnames::className(), ['productnames_productname' => 'productname']);
}
public static function getBatchNo($cat_id)
{
$data = static::find()->where(['productname'=>$cat_id])->select(['batchno'])->asArray()->all();
$value = (count($data) == 0) ? ['' => ''] : $data;
return $value;
}
}
The output looks like -
Getting error when I am trying to create dynamic form in using yii2-dynamicform. at the time of create method it is working fine but at the time of update showing the error. I have two tables one is
1.vendors &
2.vendors_more_categories
Relation is 1-* between vendors & vendors_more_categories I just refereed https://github.com/wbraganca/yii2-dynamicform this link.
<?php
namespace app\controllers;
namespace backend\controllers;
use Yii;
use app\models\Vendors;
use app\models\VendorsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use app\models\VendorsMoreCategories;
use backend\models\Model;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
/**
* VendorsController implements the CRUD actions for Vendors model.
*/
class VendorsController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index','create', 'update', 'delete'],
'rules' => [
[
'actions' => ['index','create', 'update', 'delete'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Vendors models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new VendorsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Vendors model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Vendors model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Vendors();
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
if($model->load(Yii::$app->request->post())){
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname());
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagename = Vendors::find()->orderBy('ven_id DESC')->one();
$imagename=$imagename->ven_id+1;
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$imagename.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Updates an existing Vendors model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
//print_r($model->attributes);
$modelsVendorsMoreCategories = $model->ven_id;
if($model->load(Yii::$app->request->post())){
$oldIDs = ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id');
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname(), $modelsVendorsMoreCategories);
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id')));
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$model->ven_id.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('update', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Deletes an existing Vendors model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Vendors model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Vendors the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Vendors::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
//Function used for deleting the images
public function actionDeleteimg($id, $field)
{
$img = $this->findModel($id)->$field;
if($img){
if (!unlink($img)) {
return false;
}
}
$img = $this->findModel($id);
$img->$field = NULL;
$img->update();
return $this->redirect(['update', 'id' => $id]);
}
//Function used for getting more sub categories for vendor
public function actionGetSubCategories()
{
$mbcid=$_GET['ven_main_category_id'];
$sbcid=$_GET['ven_sub_category_id'];
echo $mbcid;
}
public function actionLists($id)
{
$countVendors = Vendors::find()->where(['ven_contact_person_id' => $id])->count();
$vendors = Vendors::find()->where(['ven_contact_person_id' => $id])->all();
if ($countVendors > 0) {
foreach ($vendors as $vendor) {
echo "<option value='" . $vendor->ven_id . "'>" . $vendor->ven_company_name . "</option>";
}
} else {
echo "<option></option>";
}
}
}
You accessing modelsVendorsMoreCategories[0] (as an element of an array )
'model'=> $modelsVendorsMoreCategories[0],
in your DinamicForm widget but when you update the model you pass as $modelsVendorsMoreCategories this value
$modelsVendorsMoreCategories = $model->ven_id;
(don't seems an array, you must be sure this object contain an array with a proper element in 0 index))
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ?
[new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
$modelsVendorsMoreCategories should contain the VendorsMoreCategories model in actionUpdate method. In this code ($modelsVendorsMoreCategories = $model->ven_id) $modelsVendorsMoreCategories contains $model->ven_id. It is an integer value, not the VendorsMoreCategories object.
The Vendors model should contain a relation on the VendorsMoreCategories model:
class Vendors extends \yii\db\ActiveRecord
{
....
public function getVendorsMoreCategories()
{
return $this->hasMany(VendorsMoreCategories::className(), 'vendor_id'=>'id']);
}
}
And then, you should use that relation in your actionUpdate method:
$model = $this->findModel($id);
$modelsVendorsMoreCategories = $model->vendorsMoreCategories;
if(!$modelsVendorsMoreCategories) {
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
}
In your actionUpdate you have:
$modelsVendorsMoreCategories = $model->ven_id;
But you should have:
$modelsVendorsMoreCategories = $model->nameOfMyRelation;
To get what's the real name, go in your $model, and look for something like:
/**
* #return \yii\db\ActiveQuery
*/
public function getNameOfMyRelation()
{
return $this->hasMany(VendorsMoreCategories::className(), ['ven_id' => 'id']);
}
If you don't have any function making the relation of this two tables, write one. If you having trouble doing that, you can always use the gii's model generator and check the Vendors model (you dont need to replace it, just preview the code).
Check your create.php file in view folder, pass required variable on
_form.php file from here as:-
<?= $this->render('_form', [
'model' => $model,
'modelsAddress' => $modelsAddress,
]) ?>
Check Your create file in view folder:
Controller:
Controller pass the parameter into create.php
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
View:create.php
If You miss the parameter: 'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories.
It shows the Undefined variable error in _form.php page.
<?= $this->render('_form', [
'model' => $model,
'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories
])?>
View:_form.php
$modelsVendorsMoreCategories[0];
the paramater not passing before now it passing.