Yii2 - Upload form error - yii2

I need to implement an upload form and I using krajee's FileInput plugin :
http://demos.krajee.com/widget-details/fileinput
this is my form view
<!--modal render for upload-->
<?php
use yii\widgets\ActiveForm;
use kartik\widgets\FileInput;
use yii\helpers\Url;
?>
<div class="row" >
<div class="col-md-4" >
ファイル <label class="label label-warning">必須</label>
</div>
<div class="col-md-8 file-input" bgcolor="#FAFCFF">
<?= FileInput::widget([
'name' => 'file',
'model' => $model,
'options'=>[
'multiple'=>true
],
'pluginOptions' => [
'uploadUrl' => Url::to(['document/upload-file']),
'uploadExtraData' => [
'parent_folder' => "22222",
'parent_id' => 13
],
'maxFileCount' => 10
]
]);
?>
</div>
</div>
when form load it said:
You must set an input type = file for bootstrap-fileinput plugin to
initialize.
Please tell me what wrong?
//Update model rules
public $extensions = [];
public function rules()
{
return [
['file', 'file', 'extensions' => $this->extensions, 'skipOnEmpty' => false, 'checkExtensionByMimeType' => false],
];
}

You need to set id
<?= FileInput::widget([
'id' => 'fileUpload'
'name' => 'file',

Related

Is there a way to dynamically change maximumSelectionLength on kartik/select2 in yii2?

I am trying to update through javascript the maximumSelectionLength parameter of a select2 field (listeProduits) based on the value of a second select2 (nom_id) when it is changed.
I'm first storing the maxChildren values in a js table before loading the form so that I can access it on the client side when a new nom_id is selected.
The below approach doesn't work though as it messes up the listeProduits select2.
<script>
var nomMaxChildren = <?= json_encode(ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'max_children')); ?>;
alert(nomMaxChildren[1]);
</script>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'nom_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Nom::find()->asArray()->where(['type' => 'Boite'])->all(), 'id', 'nom'),
'options' => ['placeholder' => Yii::t('app','Select a name...')],
'pluginEvents' => [
"select2:select" => "function() {
$('select[name =\"listeProduits[]\"]').select2({
maximumSelectionLength: nomMaxChildren[\$(this).val()]
});
}",
]
]);?>
<label class="control-label"><?= Yii::t('app', 'Produits')?></label>';
<?= Select2::widget([
'attribute' => 'listeProduits',
'name' => 'listeProduits',
'data' => ArrayHelper::map(Produit::find()->asArray()->where(['statut_id' => 2])->andWhere(['boite_id' => null])->orWhere(['boite_id' => $model->id])->all(), 'id', 'numero_de_serie'),
'value' => (is_null($model->id)) ? null : ArrayHelper::map(Produit::find()->asArray()->where(['boite_id' => $model->id])->all(), 'id', 'id'),
'options' => ['placeholder' => Yii::t('app','Select products')],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'maximumSelectionLength' => (isset($model->nom)) ? $model->nom->max_children : null,
],
'showToggleAll' => false,
]);?>
Also if you have any idea on how to validate the fact that the maximumSelectionLength is ok on client side, it would be great :D
set maximumSelectionLength property dynamically :
$('#demo-select').select2({ maximumSelectionLength: 1 });

select2 widget selected will updated other select2

Hi all i am newbie in yii2 need your advise
i use select2 widget, when i selected value it will throw another value that i had set in array before. so how can i do this in yii2. so far i doing this.i have try using jquery function using pluginevents in select2 but still stuck..here is my code
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?= $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = $(this).val();
$("input#target").val($(this).val());
}',
]
]);
?>
<?= $form->field($model, 'nama_wp')->textInput(['id' => 'target']) ?>
how can i insert 'nama_wp' that already set in array into field nama_wp
thx for helping
First thing you need to change the
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
to
<?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
The you need to access the current value of the select2 via data.currentTarget.value, so change the code for change to the following.
<?php echo $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = data.currentTarget.value;
$("#target").val(data_id );
}',
]
]);
?>

Yii2 multiple ListView in one page

I'm using 2 widgets in one page, Both are using listview, one is for blogs and another one is for pages and both are using ScrollPager. I want to show more button in each separately. The problem is the show more button is shown only for pages but not for blog, if I remove the page widget it will display for blogs. I'm trying to display show more for pages and show more for blogs. I tried by pageParam but the problem still exist.
widget:
class UserPagesWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderContent = new ActiveDataProvider([
'query' => Post::find()->Where(['user_id' => $this->usr_id])
->orderBy(['post_crdate' => SORT_DESC]),
'pagination' => [
'pageParam' => 'contentPagination',
'pageSize' => 5,
],
]);
return $this->render('/user/widget/pstList', [
'dataProviderContent' => $dataProviderContent,
]);
}
}
Render file:
<?= ListView::widget([
'dataProvider' => $dataProviderContent,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Content',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-pst-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Blog Widget:
class UserBlogsWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderBlog = new ActiveDataProvider([
'query' => Blog::find()->Where(['user_id' => $this->usr_id])
->orderBy(['blog_update' => SORT_DESC]),
'pagination' => [
'pageParam' => 'blogPagination',
'pageSize' => 6,
],
]);
return $this->render('/user/widget/blgList', [
'dataProviderBlog' => $dataProviderBlog,
]);
}
}
Blog render file:
<?= ListView::widget([
'dataProvider' => $dataProviderBlog,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Blogs',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-blg-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Both widgets are called in one view file.
<div class="row chn-row">
<div class="col-sm-12" style="padding:15px">
<!-- separing -->
<?= UserPagesWidget::widget(['usr_id' => $model->user_id]) ?>
<!-- separing -->
</div>
<div class="col-sm-12" style="padding:15px">
<?= UserBlogsWidget::widget(['usr_id' => $model->user_id]) ?>
</div>
</div>
The problem is the show more button is displayed only for pages.
You need to adjust selectors to distinguish between two widgets. By default ScrollPager uses some generic selector (.list-view) that will match all list views. You should explicitly set ID for ListView widgets and use it in ScrollPager config as selector for widget initialization.
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderBlog,
'options' => ['id' => 'blog-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#blog-list-view',
'paginationSelector' => '#blog-list-view .pagination',
],
]) ?>
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderContent,
'options' => ['id' => 'content-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#content-list-view',
'paginationSelector' => '#content-list-view .pagination',
],
]) ?>
See https://kop.github.io/yii2-scroll-pager/#general-options

Get data from select2 and pass it to controller in yii2

I've one select2 form field, two datepickers and a search button in productnames index file. It is only to search data I'm unable to get the data selected in the select2 widget and pass it to controller which in turn can search other models.
index.php
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;
/* #var $this yii\web\View */
/* #var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<div class="row">
<div class="form-group">
<div class="col-xs-5 col-sm-5 col-lg-5" >
<?php
echo Select2::widget([
'model' => $model,
'attribute' => 'productnames_productname',
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'options' => ['placeholder' => 'Select Product'],
'pluginOptions' => [
'allowClear' => true
],
//'productname' => $productname,
]);
?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= DatePicker::widget([
'name' => 'Start Date',
'attribute' => 'from_date',
'value' => '2014-01-31',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= DatePicker::widget([
'name' => 'End Date',
'attribute' => 'to_date',
'value' => '2014-01-31',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1" >
<?= Html::a('Search', ['/sbbtdtproduct/production/index','productname' => $model['productnames_productname']], ['class'=>'btn btn-primary']) ?>
</div>
</div>
</div>
</div>
production controller action
public function actionIndex($productname)
{
$productname = yii::$app->request->get('productnames_productname');
$searchModel = new ProductionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
productionsearch model
public function search($params,$productname)
{
$query = Production::find()
->where(['productname' => $productname]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'productionid' => $this->productionid,
'productiondate' => $this->productiondate,
'itemid' => $this->itemid,
'prodqty' => $this->prodqty,
]);
$query->andFilterWhere(['like', 'productname', $this->productname])
->andFilterWhere(['like', 'batchno', $this->batchno]);
return $dataProvider;
}
error -
update -
Database Log
The data base Log shows that no value has been passed from search model.
I can see the value selected in select2 or datepicker as below, but it's not passing to the controller.
1.on your view page
You have not added form tag, so other parameters will not get posted, you must add everything inside form tag and submit that form, as follows
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use kartik\select2\Select2;
use dosamigos\datepicker\DatePicker;
use yii\helpers\ArrayHelper;
use frontend\modules\sbbtdtproduct\models\Productnames;
use yii\helpers\Json;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $searchModel frontend\modules\sbbtdtproduct\models\ProductnamesSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Productnames';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="productnames-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]);
$form = ActiveForm::begin([
'action' => ['/sbbtdtproduct/production/index'],
'method' => 'post',
'options' => ['data-pjax' => true],
'enableClientValidation' => FALSE
]);
?>
<div class="row">
<div class="form-group">
<div class="col-xs-5 col-sm-5 col-lg-5" >
<?php
echo Select2::widget([
'model' => $model,
'attribute' => 'productnames_productname',
'data' => ArrayHelper::map(Productnames::find()->all(),'productnames_productname','productnames_productname'),
'options' => ['placeholder' => 'Select Product'],
'pluginOptions' => [
'allowClear' => true
],
//'productname' => $productname,
]);
?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= DatePicker::widget([
'name' => 'Start Date',
'attribute' => 'from_date',
'value' => '2014-01-31',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-3 col-sm-3 col-lg-3">
<?= DatePicker::widget([
'name' => 'End Date',
'attribute' => 'to_date',
'value' => '2014-01-31',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1" >
<?= Html::submitButton('Search', ['class'=>'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
2.Controller
now inside your controller you can access parametes as follows
public function actionIndex()
{
$productname = Yii::$app->request->post('productnames_productname');
$searchModel = new ProductionSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $productname);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

sjaakp/yii2-illustrated-behavior Yii2 extension for image not working

I am getting error; Is there any complete example of how to use the behavior(configuration in model,view,controller) practically;the documentation on github doesn't really help.
In my model the field that store the img link in db is imglink; the code are:
public $photo;//The file attributes
public function behaviors(){
return [
[
"class" => "sjaakp\illustrated\Illustrated",
"attributes"=>[
'img'=>[
'aspectRatio'=>1.0,
'cropSize'=>90,]
]
],
];
}
//rules : $rules[] = ['photo','file'];
in my view :
use sjaakp\illustrated\Uploader;
$this->title = \Yii::t('app','Choose your Profile Photo');
$this->params['breadcrumbs'][] = $this->title;
?>
<div id="partiechargee" class="panel panel-default center-block">
<?php if($titre==true){
echo '<p class="well-sm">';
$this->render('/_alert', ['module' => $module,]);
echo '</p>';}?>
<h3 class="panel-title panel-heading text-center" style="margin-bottom:5px;"><strong><?= Html::encode($this->title)?></strong></h3>
<?php $form = ActiveForm::begin([
'id'=> 'form-registration',
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'method'=>'post',
'action'=>\Yii::$app->urlManager->createUrl(['settings/profilephoto']),
'options'=>['class'=>'well-sm text-center',
'enctype'=>'multipart/form-data']]); ?>
<?= $form->field($model, 'photo')->widget(Uploader::className([
'deleteOptions' => [
'label' => '<i class="fa fa-trash"></i>',
'title' => 'Delete image'
]
])) ?>
<p class="text-center">
<?= Html::submitButton(Yii::t('app', 'Exit'), ['class' => 'btn btn-danger cancel btn-block']) ?>
</p>
<?php ActiveForm::end(); ?>
I am getting error : Undefined index: photo
This :
public function behaviors(){
return [
[
"class" => "sjaakp\illustrated\Illustrated",
"attributes"=>[
'img'=>[
'aspectRatio'=>1.0,
'cropSize'=>90,]
]
],
];
}
Should be:
public function behaviors(){
return [
[
"class" => "sjaakp\illustrated\Illustrated",
"attributes"=>[
'photo'//Important to put the attribute of fileinput,
'img'=>[
'aspectRatio'=>1.0,
'cropSize'=>90,]
]
],
];
}