UploadedFile::getInstances giving empty array in and file upload not working in yii2 - yii

If I use the field as below to upload files and when I print $_FILES in controller, the result is empty.
<?php echo $form->field($userformmodel, 'user_images[]')->fileInput(['multiple' => true, 'class' => 'multi with-preview accept-gif|jpg|png|jpeg']); ?>
Whereas, if I give a static input as below and print $_FILES in my controller then I am getting the array which is not empty.
<input type="file" class="multi with-preview accept-gif|jpg|png|jpeg MultiFile-applied" name="user_images[]" multiple="" value="">
What could be the issue. Why am I getting empty array if I use yii model for generating input field?

Related

Show fa icon inside Chtml::link() in Yii1

Im using CHtml::link() for attachment in my grid view and if i click on it, it will open in new tab to display.
My doubt is how can i use font awesome icon to display attachment in grid. Currently it showing from table but i want to display <i class="fa fa-paperclip" aria-hidden="true"></i> this icon for that column.
array(
'name' => 'invoice_attachment',
'value' => 'CHtml::link($data->invoice_attachment, Yii::app()->request->baseUrl.$data->invoice_attachment, array("target"=>"_blank"))',
'type' => 'raw',
),
The above code is my current output for link.
CHtml::link($text, $url, $htmlOptions) method accepts 3 parameters:
$text - this is what you want, right now youre setting here $data->invoice_attachment (propably file name) - change this to your <i> string
$url - a URL or an action route that can be used to create a URL
$htmlOptions - additional HTML attributes
Change $text param and that's all.

Changing the class name generated by ClistView Yii

Just a simple question, is it possible to change the classname generated by ClistView ?
by default, it generates
<div class="post">
for all the list.
I'd like to have
<div class=post1>
<div class=post2>
...
You can customize CListView styles with bellow parameters:
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$YOUR_DATA_PROVIDET,
'itemView'=>'...',
'sortableAttributes'=>array(),
'cssFile'=>' YOU CAN ASSIGN A CSS FILE TO YOUR CLISTVIEW',
'itemsCssClass'=>'SOME CLASS',
'pagerCssClass'=>'SOME CLASS',
'sorterCssClass'=>'SOME CLASS',
'summaryCssClass'=>'SOME CLASS',
));
for more information you can check CListView's Official document in the following link:
CListView
UPDATE:
If you want to change other names, you must edit the source of yii's CGridView. But changing the style of it could be more easier.
If you want a different, incrementing class on each looped list item, change your itemView partial like this:
using the ID of each model:
<div class="post<?php print $data->id; ?>">
<?php
print_r($data->attributes); // Or whatever
?>
</div>
using the 'index' of the current iteration:
<div class="post<?php print $index; ?>">
<?php
print_r($data->attributes); // Or whatever
?>
</div>
More info available here

How to implement a dynamic js script in Yii List View?

Hello and thanks for reading my question. I have a typical list view:
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'emptyText'=>'No Jobs',
)); ?>
In my _view file I have a div and a button that slideToggles the div. If I just put the Javascript at the top of the page, it does not work because the results are dynamic and the name of the div changes with the id returned, eg:
id="detailsDiv-<?php echo $data->id_employer_contract;?>"
The problem is in my Javascript, which is as follows:
<?php Yii::app()->clientScript->registerScript('details', "$('#details-$data-id_employer_contract').click(function(){
$('#detailsDiv-$data->id_employer_contract').slideToggle();
return false;});");?>
How can I make this Javascript code dynamic? Meaning, how can I loop through the id? I tried adding the code to the listview property ajaxUpdate but it's still not working. Can someone tell me how I can loop a Javascript in a list view?
Add the id to your toggle buttons as data attribute:
<button class="toggleDetails" data-id="<?php echo $data->id_employer_contract ?>">
Then you can access these data attributes like this js:
<?php Yii::app()->clientScript->registerScript('toggleDetails', "
$('.toggleDetails').click(function(e){
var id = $(this).data('id');
$('#detailsDiv-' + id).slideToggle();
e.preventDefault();
});
", CClientScript::POS_READY) ?>
NOTE: You should not put this javascript into _view.php but into the main file where you render the List View. You only need this one single snippet to deal with all your buttons.

Multiplying values from a textbox in Yii

I'm new to Yii, and I'm trying to compute values from textboxes. I want the computation codes to be on the same form where the textbox is being displayed (since the product will also be shown on a textbox of the same form where they will input the factors). I've tried this guess, but it didn't work. How do I fix this problem?
<div class="row">
<?php echo $form->labelEx($model,'Quantity_In_Pieces'); ?>
<?php echo $form->textField($model,'Quantity_In_Pieces',array('Quantity_In_Pieces' => ('Quantity').val() * ('Hold').val());?>
<?php echo $form->error($model,'Quantity_In_Pieces'); ?>
</div>
seems like you are trying to write jQuery where there should be php code.
<?php echo $form->textField($model,'Quantity_In_Pieces',array('Quantity_In_Pieces' => $model->quantity * $model->hold);?>

YII CUploadedFile issue

why below code does not work?
$video = new Videos();
$video->photo = CUploadedFile::getInstance($video,'photo');
echo $video->photo->extensionName;
nothing comes in $video->photo and it gives error :: Trying to get property of non-object
1) File field is
<input type="file" id="photo" name="Video[photo]" />
2) Added enctype="multipart/form-data" in the form
3) Added photo field in model
please help
you need to be sure you have the CFormModel->rules() has the $photo, told to be a file.