i used kartik\file\FileInput for upload my file
my file is correct uploaded for file smaller than 2 mb !
what is my problem ????
echo '<label class="control-label">اضافه کردن فایل</label>';
echo FileInput::widget([
'model' => $model,
'pluginOptions' => [
'initialPreview' => [
"../../$url",
],
'initialPreviewAsData' => true,
'initialCaption' => "",
'initialPreviewConfig' => [
],
],
'attribute' => 'file',
'options' => ['multiple' => false,
'accept' => "$acc/*",
]
]);
}
this is my model :
class PostContent extends \yii\db\ActiveRecord
{
public $file;
Related
I've an application that was developed using Yii2, and this application I've use Kartik Input File for upload file.
Case
From the example above, I want to remove / hide the "File" label.
And I think, that label based on model name.
This is the code I use:
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
'options' => [
'accept' => 'doc/*', 'file/*',
'enableLabel' => false,
],
'pluginOptions' => [
'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
'showUpload' => FALSE,
'showPreview' => FALSE,
]
]);
?>
How do I can remove the label above?
Thanks
For removing the label you can simply use the following:
<?=
$form->field($model, 'file')->widget(FileInput::classname(), [
'options' => [
'accept' => 'doc/*', 'file/*',
'enableLabel' => false,
],
'pluginOptions' => [
'allowedFileExtensions' => ['csv', 'xls', 'xlsx'],
'showUpload' => FALSE,
'showPreview' => FALSE,
]
])->label(false);
?>
I have this piece of code that if the user clicks on it the link will be replaced by text making it unable to be clicked again. The problem now is that if the user access it directly in the url so it will simulate a link click. So how do I prevent users from accessing urls directly?
<?php
$isAdded = ActiveSubject::find()->where(['clientid' => $_user,'subjectid' => $subjects['subjectid'],])->exists();
if($isAdded):
?>
<b><p class="text-muted">ADDED</p></b>
<?php else: ?>
<p>
<?= Html::a('<b>ADD</b>',['site/addsubject', 'subjectid'=>$subjects['subjectid'], 'clientid' => $_user],['class' => 'btn-info btn-transparent btn-large']) ?>
</p>
<?php endif; ?>
</td>
<td>
<?= $subjects['slots'] ?>
</td>
<td>
<?php if($isAdded): ?>
<p class="text-primary">Awaiting Confirmation</p>
<?php endif; ?>
In controller
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['addsubject'],
'allow' => true,
'roles' => ['addsubject', 'yourmodelname'],
],
[
'allow' => true,
'roles' => ['superAdmin', 'admin', 'managerModule1', 'managerApp'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'addsubject' => ['post'],
],
],
];
}
checkout this 2 answers also
how to deny the access of url in yii even if we know the url?
how to limit access url view on yii2 by id
In which you can understand the use of filters.
Make it a POST link so that it has to clicked and can't be directly run from the browser
ie.
adding 'data-method' => 'post' to HTML::a
<?= Html::a('<b>ADD</b>',['site/addsubject', 'subjectid'=>$subjects['subjectid'], 'clientid' => $_user],['class' => 'btn-info btn-transparent btn-large', 'data-method' => 'post']) ?>
And in the Access Rules you can add rule to only accept POST Request
i.e
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'addsubject' => ['post'],
],
],
Hope this helps. Thanks.
Edit:
Below is sample for SiteController
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => True,
'actions' => [],
'roles' => []
],
[
'actions' => ['login', 'error', 'captcha'],
'allow' => true,
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
'addsubject' => ['post'],
],
],
];
}
I have this little problem in uploading multiple images using yii2 and Kartik fileInput extension.
this is the model:
public $file;
public static function tableName()
{
return 'news';
}
public function rules()
{
return [
[['news_desc', 'news_context', 'news_first_source', 'news_second_source', 'news_third_source', 'news_responsibe_party', 'news_first_testimony', 'news_second_testimony', 'news_body', 'news_lang'], 'string'],
[['news_gov_id', 'news_typ_id', 'news_is_in_slider', 'news_is_act', 'news_is_del'], 'integer'],
[['news_happened_date', 'news_created_date', 'file'], 'safe'],
[['news_typ_id', 'news_lang'], 'required'],
[['news_title'], 'string', 'max' => 255],
[['file'], 'file','maxFiles' => 6],
];
}
as you can notice I am using maxFiles but it didnt worked for me
Controller:
public function actionCreate_news()
{
$model = new News();
if ($model->load(Yii::$app->request->post())) {
$model->file = UploadedFile::getInstances($model, 'file');
var_dump($model->file);
die();}}
right now I am just var dump the files I get but the problem there is only one file not all the files i uploaded
view:
echo FileInput::widget([
'model' => $model,
'attribute' => 'file[]',
'name' => 'file[]',
'options' => [
'multiple' => 'true',
'accept' => 'image/*'
],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'allowedFileExtensions' => ['jpg','jpeg','png'],
'overwriteInitial' => false
],
]);
i have read all things about this issue and tried all the possible solutions but the problem is still there
when i press submit for the form only the last file will be submited
thanks
Remove 'name' => 'file[]', from this widget also single quotes from true.
echo FileInput::widget([
'model' => $model,
'attribute' => 'file[]',
'options' => [
'multiple' => true,
'accept' => 'image/*'
],
'pluginOptions' => [
'showCaption' => false,
'showRemove' => false,
'showUpload' => false,
'allowedFileExtensions' => ['jpg','jpeg','png'],
'overwriteInitial' => false
],
]);
Original:
echo FileInput::widget([
'model' => $model,
'attribute' => 'attachment_1[]',
'options' => ['multiple' => true]
]);
Thank you all I figured out the solution for my question i was trying to upload the images when submitting the form but it turned that i was wrong so i used the widget to upload the images using ajax like in here:
http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/
then using the plugin event i took the names of files to save in the DB when the form submited
I'm using wbraganca yii2-dynamicform and kartik yii2-widget-fileinput. The form works fine but when I'm trying the getinstance for upload the images always is null.
This is my controller.
public function addMultipleImage($model){
$modelsOptionValue = Model::createMultiple(OptionValue::classname());
Model::loadMultiple($modelsOptionValue, Yii::$app->request->post());
foreach ($modelsOptionValue as $index => $modelOptionValue) {
$modelOptionValue->sort_order = $index;
$file[$index]= UploadedFile::getInstanceByName("OptionValue[".$index."][upload_image]");
var_dump($file[$index]);
if($file[$index]){
$ext = end((explode(".", $file[$index]->name)));
// generate a unique file name
$modelOptionValue->img= Yii::$app->security->generateRandomString().".{$ext}";
$path[$index]= Yii::getAlias ('#web') ."/img/". $modelOptionValue->img;
}else{
return false;
}
}
View form
//_form.php
<?php $form = ActiveForm::begin( [
'enableClientValidation' => false,
'enableAjaxValidation' => true,
'validateOnChange' => true,
'validateOnBlur' => false,
'options' => [
'enctype' => 'multipart/form-data',
'id' => 'dynamic-form'
]
]);
?>
//_form_add_image.php
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.form-options-body',
'widgetItem' => '.form-options-item',
'min' => 1,
'insertButton' => '.add-item',
'deleteButton' => '.delete-item',
'model' => $modelsOptionValue[0],
'formId' => 'dynamic-form',
'formFields' => [
'upload_image'
],
]); ?>
<?= $form->field($modelOptionValue, "[{$index}]upload_image")->label(false)->widget(FileInput::classname(), [
'options' => [
'multiple' => false,
'accept' => 'image/*',
'class' => 'optionvalue-img'
],
'pluginOptions' => [
'previewFileType' => 'image',
'showCaption' => false,
'showUpload' => false,
'browseClass' => 'btn btn-default btn-sm',
'browseLabel' => ' Seleccionar Imagen',
'browseIcon' => '<i class="glyphicon glyphicon-picture"></i>',
'removeClass' => 'btn btn-danger btn-sm',
'removeLabel' => ' Borrar',
'removeIcon' => '<i class="fa fa-trash"></i>',
'previewSettings' => [
'image' => ['width' => '138px', 'height' => 'auto']
],
'initialPreview' => $initialPreview,
'layoutTemplates' => ['footer' => '']
]
]) ?>
and my model
public $upload_image;
/**
* #inheritdoc
*/
public static function tableName()
{
return 'option_value';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['upload_image'], 'file', 'extensions' => 'png, jpg', 'skipOnEmpty' => true],
[['id_montacarga', 'sort_order'], 'integer']
];
}
screenshot of yii2 log. updated
inspect element screenshot
Thanks!!!
Add 'enctype' => 'multipart/form-data' in ActiveForm.
<?php $form = ActiveForm::begin([
'options' => [
'enctype' => 'multipart/form-data',
'id' => 'dynamic-form'
]
]); ?>
Set Ajax Validation to false if you don't want ajax validation.
Reference
I have a view displaying a grid view for a particular model. In my local machine, it's working well but when I deploy the application to another server, an attribute is not found hence the Unknown Property Exception. When I look at the code though, the attribute is there.
Any ideas?
Here is the model class code: http://codebin.org/view/f0a713c1
The view code:
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'is_condemned',
'label' => 'Condemned',
'class' => '\kartik\grid\BooleanColumn',
'falseLabel' => 'Active',
'trueLabel' => 'Condemned'
],
],
// set your toolbar
'toolbar' => [
['content' =>
Html::a(FA::icon('plus') . ' Add', ['/equipment/default/create'], ['class' => 'btn btn-success'])
],
'{export}',
'{toggleData}',
],
// set export properties
'export' => [
'fontAwesome' => true,
'filename' => 'equipment-export-'.time(),
'exportConfig' => [
'html' => ['showCaption' => FALSE],
'pdf' => ['showCaption' => FALSE],
],
],
'bordered' => TRUE,
'striped' => TRUE,
'condensed' => TRUE,
'responsive' => TRUE,
'hover' => TRUE,
'showPageSummary' => TRUE,
'panel' => [
'type' => GridView::TYPE_PRIMARY,
'heading' => '',
],
'persistResize' => false,
]);
The reason why it wasn't working was that the model class being imported was in lowercase. Apparently, I entered the wrong value in gii. It was just in the other servers that it was case-sensitive.
Could be either a problem with case (Uppercase, lowercase) in the namespace or in the class name or backslash path problem (/, \). In this case the class is not found and Yii shows this message.