Im trying to generate a basic csv file with some data. When I use an alert inside the ajax call it will show me the data(so there is data passing), but when I click the button it will not generate an CSV file. Im new to yii2 so im still learning.
UPDATED
I have changed the files
//view export/index.php
Pjax::begin();
$form = ActiveForm::begin([
'action' => yii\helpers\Url::to(['cms-export/index']),
'options' => ['data' => ['pjax' => true]],
'layout' => 'horizontal',
'fieldConfig' => [
'horizontalCssClasses' => [
'label' => 'col-sm-2',
'offset' => 'col-sm-offset-2',
'wrapper' => 'col-sm-5',
'hint' => 'col-sm-5',
],
],
]);
echo $form->field($model, 'language')->dropDownList([//some list]);
echo $form->field($model, 'filename')->textInput()
echo Html::submitButton('Submit', ['class' => 'btn btn-primary'])';
ActiveForm::end();
Pjax::end();
//model
public function generateCsv(){
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="sample.csv"');
$data = [datacomeshere];
$fp = fopen('php://output', 'w');
foreach ( $data as $line ) {
fputcsv($fp, $line, ';');
}
fclose($fp);
}
//controller
public function actionIndex()
{
$model = new Export();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// validation works, but method does not work
\common\models\Export::generateCsv();
}
return $this->render('index' , ['model' => $model]);
}
When I click the button it will show me an 500 error in the jquery file
xhr.send( options.hasContent && options.data || null );
I would suggest the following approach, removing the whole JS code:
Make the link a real one. It makes sense to have a GET request, since you just getting data with the call.
<div class="modal-button-row">
Export
</div>
Modify the action now (probably in CmsExportController) and use the Yii download capability:
public function actionDownload() {
$csv = Export::generateCsvSomehow(); // this should return a csv string
return \Yii::$app->response->sendContentAsFile($csv, 'sample.csv', [
'mimeType' => 'application/csv',
'inline' => false
]);
}
More infos in guide: here.
You also need to remove Pjax, since it will do its own stuff with links and forms via JS! Or you have to configure Pjax, e.g. with $formSelector, which goes beyond the scope of this question.
One problem is definitely that the regular action call always creates headers, cookies and some content (even an empty string, e.g. if you have forgotten the return statement with $this->render(...)) that gets sent to the browser. I supsect that you get some Headers already sent error. So this has to be suppressed so that your CSV code takes control.
Try the following:
public function actionIndex() {
$model = new Export();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
\common\models\Export::generateCsv();
Yii::$app->response->isSent = true;
} else {
return $this->render('index' , ['model' => $model]);
}
}
Btw: Use method: GET in your form if no data gets changed with that call. This is a HTTP standard. POST is used when something gets added or changed.
However, I would recommend my other approach using Response::sendContentAsFile(). This should work with your ActiveForm as well. As noted in that answer you have to remove or configure Pjax.
Related
I have a form that inside looks like that:
<?= $form->field($model, 'comment')->textarea(['rows' => 6]) ?>
<?= $form->field($presentation, 'attendance')->textInput(['maxlength' => true]) ?>
<?= $form->field($presentation, 'couples')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'hire_price')->textInput(['maxlength' => true]) ?>
Then I collect received data with controller, like that:
$model = new PresentationPlaceHistory();
$presentation = new Presentations();
if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {
$pres = $presentation->findOne($model->presentation_id);
$pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
$pres->couples = $presentation->load(Yii::$app->request->post('couples'));
$pres->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else {
return $this->render('create', [
'model' => $model,
'presentation' => $presentation,
]);
}
But in the effect, it saves all, beside 'attendance' and 'couples', which are set to 0 (before this form they were null).
It doesn't matter what number I put, I get zero. It doesn't even have to be number, because validation doesn't work at all.
Change following two lines:
$pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
$pres->couples = $presentation->load(Yii::$app->request->post('couples'));
// to this
$pres->attendance = $presentation->attendance;
$pres->couples = $presentation->couples;
You have already loaded $presentation values here $presentation->load(Yii::$app->request->post()) and should be available to access directly.
Kinda messy code u got here. First of all, I suggest you not to use load() method, but its my personal preference. Read more about load.
This method returns boolean, that is why you are getting 0 in model properties. In general, your code should look more like:
$model->load(Yii::$app->request->post());
if ($model->save()) {
$pres = $presentation->findOne($model->presentation_id);
$pres->attendance = $presentation->attendance;
//etc ...
$pres->save()
}
I dont know what is the point of your code, but this looks kinda pointless. Try to work with attributes, it is an array of all model properties. Or assign manually wanted properties of the model.
problem with your controller code, change following two lines
if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {
$post_data= Yii::$app->request->post();
$pres = $presentation->findOne($model->presentation_id);
/* change these two lines */
$pres->attendance = $post_data['Presentations']['attendance'];
$pres->couples =$post_data['Presentations']['couples'];
/* change these two lines */
$pres->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
'presentation' => $presentation,
]);
}
my view.php
<?php
echo
Html::beginForm(['contactpersons/update'], 'post',['id' => 'update-form']) .
'<input type="hidden" name="id" value="'.$model->id.'">
<a href="javascript:{}" onclick="document.getElementById(\'update-form\').submit();
return false;">Update</a>'.
Html::endForm();
?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
])
?>
my controller is
public function actionView($id)
{
$model = $this->findModel($id);
return $this->render('view', ['model' => $model]);
}
How to modify this for getting my view page without id value in the url.
Thanks in advance.
You could change it like this
public function actionView($id = null) {
$model = null;
if ($id !== null) {
$model = $this->findModel($id);
}
return $this->render('view', ['model' => $model]);
}
However in your view you execute the following code: $model->id
this won't work when the model isn't set yo anything. So you could create a new model ($model = new ModelClass()) when the $id is null.
Sidenote: this doesn't look like an view action but more like an edit action, so maybe change your action to actionEdit().
You can send data to your browser by two methods - POST and GET. So, if you want to hide id parameter, then you need to send your id as POST parameter, which is bad solution - it's hard to implement, because POST is usally sends when you submit a form.
This is the situation: I'm new on Yii2 and wanted to use some file uploader widget within ActiveForm.. so far I've found this excelent one: \kartik\widget\FileInput
With this widget I can manage file upload and then, when enter in edit mode, show the previous uploaded image with the oportunite to replace it.
The problem is that if I press the "Update" button of the form without modifying the image yii says that the image "can't be empty" because I've set the 'required' rule in my model.
After an awful afternoon and a more productive night, I've encountered a solution that worked for me..
The main problem was that file input don't send its value (name of the file stored in database) when updating. It only sends the image info if browsed and selected through file input..
So, my workaround was creating another "virtual" field for managing file upload, named "upload_image". To achieve this I simple added a public property with this name to my model class: public $upload_image;
I also add the folowing validation to rules method on Model class:
public function rules()
{
return [
[['upload_image'], 'file', 'extensions' => 'png, jpg', 'skipOnEmpty' => true],
[['image'], 'required'],
];
}
Here, 'upload_image' is my virtual column. I added 'file' validation with 'skipOnEmpty' = true, and 'image' is the field on my database, that must be required in my case.
Then, in my view I configured 'upload_image' widget like follows:
echo FileInput::widget([
'model' => $model,
'attribute' => 'upload_image',
'pluginOptions' => [
'initialPreview'=>[
Html::img("/uploads/" . $model->image)
],
'overwriteInitial'=>true
]
]);
In 'initialPreview' option I asign my image name, stored in '$model->image' property returned from database.
Finally, my controller looks like follow:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->load(Yii::$app->request->post());
if(Yii::$app->request->isPost){
//Try to get file info
$upload_image = \yii\web\UploadedFile::getInstance($model, 'upload_image');
//If received, then I get the file name and asign it to $model->image in order to store it in db
if(!empty($upload_image)){
$image_name = $upload_image->name;
$model->image = $image_name;
}
//I proceed to validate model. Notice that will validate that 'image' is required and also 'image_upload' as file, but this last is optional
if ($model->validate() && $model->save()) {
//If all went OK, then I proceed to save the image in filesystem
if(!empty($upload_image)){
$upload_image->saveAs('uploads/' . $image_name);
}
return $this->redirect(['view', 'id' => $model->id]);
}
}
return $this->render('update', [
'model' => $model,
]);
}
I have encountered another solution by creating scenarios. In your case I would modify the rules like this:
public funtion rules() {
[['image'], 'file'],
[['image'], 'required', 'on'=> 'create']
}
So the fileupload field will be required only in create action. In update action I have this code:
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$newCover = UploadedFile::getInstance($model, 'image');
if (!empty($newCover)) {
$newCoverName = Yii::$app->security->generateRandomString();
unlink($model->cover);
$model->cover = 'uploads/covers/' . $newCoverName . '.' . $newCover->extension;
$newCover->saveAs('uploads/covers/' . $newCoverName . '.' . $newCover->extension);
}
if ($model->validate() && $model->save()) {
return $this->redirect(['view', 'id' => $model->post_id]);
} else {
// error saving model
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
In the update scenario the image filed is not required but the code checks if nothing was uploaded and doesn't change the previous value.
My form file:
<?= $form->field($model, 'image')->widget(FileInput::classname(), [
'options' => ['accept'=>'image/*'],
'pluginOptions'=>[
'allowedFileExtensions'=>['jpg', 'gif', 'png', 'bmp'],
'showUpload' => true,
'initialPreview' => [
$model->cover ? Html::img($model->cover) : null, // checks the models to display the preview
],
'overwriteInitial' => false,
],
]); ?>
I think is a little more easier than a virtual field. Hope it helps!
Try preloading the file input field with the contents of that field. This way, you will not lose data after submitting your form.
I looked through kartik file-input widget (nice find, btw) and I came across a way to do this
// Display an initial preview of files with caption
// (useful in UPDATE scenarios). Set overwrite `initialPreview`
// to `false` to append uploaded images to the initial preview.
echo FileInput::widget([
'name' => 'attachment_49[]',
'options' => [
'multiple' => true
],
'pluginOptions' => [
'initialPreview' => [
Html::img("/images/moon.jpg", ['class'=>'file-preview-image', 'alt'=>'The Moon', 'title'=>'The Moon']),
Html::img("/images/earth.jpg", ['class'=>'file-preview-image', 'alt'=>'The Earth', 'title'=>'The Earth']),
],
'initialCaption'=>"The Moon and the Earth",
'overwriteInitial'=>false
]
]);
You may also want to relax the required rule in your model for that field, so it does not complain on validation. You may choose to prompt the user through subtler means.
From Krajee:
http://webtips.krajee.com/advanced-upload-using-yii2-fileinput-widget
Create, delete, update: really easy, look no further.
(1) I've set the 'required' rule in my model too.
(2) To work on Wampserver:
Yii::$app->params['uploadPath'] = Yii::$app->basePath . '/web/uploads/';
Yii::$app->params['uploadUrl'] = Yii::$app->urlManager->baseUrl . '/uploads/';
i've been using and studying Collin Williams template plugin (http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation) and i've already posted this issue on CI's forum but i think the last post was last year maybe its not being monitored by Colllin or wat but i guess i'll just have to post this here maybe you guys can help.
Original Post on CI Forum
Hello Collin,
I’ve been studying your template plugin lately, as i was following your guide,
i came across this line of code
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
it was a bit confusing whether in the view files, like
mymastertemplate.php for example, how do i accessthe $data array, does
it have to be $content defined by that first param. a region, or by
$name and $birthdate? ... cuz’ it says there $content will display the
data array? its a bit confusing. Hope you could enlighten me.
Basically thats my problem.
On Template.php library we can see function write_view(). Now, focus on $data = NULL. Now then finds a file of existed data on APPPATH.'views/'.$suggestion.'.php' so I think that $args[0] should be a file which is loaded and break it, than loaded a view template on $data.
function write_view($region, $view, $data = NULL, $overwrite = FALSE)
{
$args = func_get_args();
// Get rid of non-views
unset($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
if (count($args) > 1)
{
foreach ($args as $suggestion)
{
if (file_exists(APPPATH .'views/'. $suggestion . EXT) or file_exists(APPPATH .'views/'. $suggestion))
{
// Just change the $view arg so the rest of our method works as normal
$view = $suggestion;
break;
}
}
}
$content = $this->CI->load->view($view, $data, TRUE);
$this->write($region, $content, $overwrite);
}
In another way, $data should be as array which will response for View template data on Codeigniter library (standard view of CI: $this->CI->load->view(...))
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
On template file '/user/profile.php' use as example:
HTML/PHP template file profile.php:
Your name: <?php echo $data["name"]; ?>
Your name: <?php echo $data["birthdate"]; ?>
And as I see, a CONTENT var must be an ARRAY due to documentation...
$template['default']['regions'] = array(
'header' => array(
'content' => array('<h1>Welcome</h1>','<p>Hello World</p>'), ### <----- AS EXAMPLE
'name' => 'Page Header',
'wrapper' => '<div>',
'attributes' => array('id' => 'header', 'class' => 'clearfix')
)
);
Regions must be defined as template, so if you didn't have header region that didn't work:
$template['default']['regions'] = array(
'header',
'content',
'footer',
);
!!!!!
Simply, he can't acces private access variable _ci_cached_vars which is stored data like $name. RELATED TOPIC: CodeIgniter shared data between calls to load->view
I am using $this->Js->submit to pass a value to my controller asynchronously and than update a div (id = #upcoming). Somehow I cannot save/retrieve the value of the field 'test' which is passed to my controller. Firebug tells me that the correct value is passed. What am I doing wrong?
View file (playlist.ctp):
echo $this->Form->create('Add', array('url' => array('controller' => 'Gods', 'action' => 'add')));
echo $this->Form->input('test');
echo $this->Js->submit('Addddd', array(
'url' => array(
'controller' => 'Gods',
'action' => 'add'
),
'update' => '#upcoming'
));
echo $this->Form->end();
echo $this->Js->writeBuffer(array('inline' => 'true'));
Controller action:
public function add()
{
$this->autoLayout = false;
$this->layout = 'ajax';
$link = $this->request->data['Add']['test'];
$this->set('test',$link);
}
And its view file (add.ctp):
<?php
echo $test;
?>
Thanks for your help!
have you tried pr($link) in the controller method? Or just send it to a log file if you prefer that. That way you can see if the data is received.
If so, I think there is nothing returned because of
$this->autoLayout = false;
Try it without this. It will still call the ajax layout instead of the default.
Otherwise you have to manualy call the render function
$this->render('add');
EDIT
As explained in the comments below, make sure your views are in the right place. (that would be the view folder associated with the controller)