i want to keep the photo column for the registration page so that user can select the image and display in that column for this i am using ImageSelect extension in yii framework but it is not working suggest me how to keep the image while registering the user
Here is my code for ImageSelect
<?php echo $form->labelEx($model,'profileImage'); ?>
<?php
$this->widget('ext.imageSelect.ImageSelect', array(
'path'=>Yii::app()->baseUrl . '/images/Penguins.jpg',
'alt'=>'alt text',
'uploadUrl'=> 'profileinfo/upload',
'htmlOptions'=>array()
));
?>
my controller code is
public function actionUpload()
{
$file = CUploadedFile::getInstanceByName('file');
// Do your business ... save on file system for example,
// and/or do some db operations for example
$file->saveAs(Yii::app()->baseUrl . '/images/'.$file->getName());
// return the new file path
echo Yii::app()->baseUrl.'/images/'.$file->getName();
}
Related
I am using Yii2 framwork. My file upload function worked well when I upload a single img, while when I click the posed article and I only want to update the post again(Suppose I didn't want to update the img, I only want to update other contents). But I found my uploaded file were replaced with an empty value(varchar type) when I click view. my uploaded img can't show out.
I do tried to fixed by myself as below, But the existing file value can't be saved when I click the submit button.
<?php
if (($post->file) != "") {
echo $form->field($post, 'file')->textInput()->hint('My currently uploaded file')->label('My Photo') ;
}
else{
echo $form->field($post, 'file')->fileInput()->hint('To upload a new file')->label('My Photo') ;
}
?>
When I click submit button, my existing file was gone.
Is there any good way to fix it.
Thanks for your opinions in advance.
Use another variable in your model for upload a file.
For example use file_field name for get file from submitted and store in file field.
class PostModel extends Model
{
/**
*
* #var UploadedFile
*/
public $file_field;
public function rules() {
return [
['file_field', 'file'],
];
}
}
echo $form->field($post, 'file_field')->fileInput()->hint('To upload a new file')->label('My Photo') ;
$post->file_field = UploadedFile::getInstance($post, 'file_field');
For upload new file check the file_field:
if ($post->file_field) {
// $post->file old file
// Save $post->file_field and store name in $post->file
}
Add a rule to your model rules:
[['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png, jpg'],
and check for an empty upload in your controller:
if (Yii::$app->request->isPost) {
$ok = true;
// process your other fields...
...
// process image file only if there is one
$post->file= UploadedFile::getInstance($post, 'file');
if ($post->file && $post->upload()) {
}
if ($ok) {
return $this->redirect(...);
}
}
See Yii2 docs and the Yii2 guide for detailed infos about file upload.
I Need Help in Yii, i get problem in my form, the problem is if i put id in textfield then client validation not showing for that field but another field no problem. This is my code in form
<div class="row">
<?php echo $form->labelEx($model,'latar_belakang'); ?>
<?php echo $form->textField($model,'latar_belakang',array('id' => 'latarbelakang','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'latar_belakang'); ?>
<?php echo $form->labelEx($model,'rumusan_masalah'); ?>
<?php echo $form->textField($model,'rumusan_masalah',array('id' => 'rumusanmasalah','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'rumusan_masalah'); ?>
</div>
for your knows i use id in textfield for calculaten field with jquery which i put in form . this is my jquery code in form
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScript('totalValues',"
function totalValues()
{
var lb = $('#latarbelakang').val() ;
var rm = $('#rumusanmasalah').val();
var tot = parseInt(lb) + parseInt(rm);
$('#nilaiakhir').val( tot );
}
", CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('changeTotals', "
$('#latarbelakang').change( function(){ totalValues(); } );
$('#rumusanmasalah').change( function(){ totalValues();} );
", CClientScript::POS_END);
What i must do ? for solving this problem ?.
Thanks B4..
By default Yii generates ids of fields dynamically using model name and field name.
For example for 'password' field of 'User' model the id would be 'User_password'
You can inspect a field(to which you have not given your custom id) with firebug and see the auto generated id. also yii maps error messages with those auto generated ids in response. that is why your error messages are not showing.
what you can do is to try to call that js function with yii generated id.
The problem is if I want to update the other fields and not the image. It passes the validation and doesn't update any of the fields.
But if I update the image and other fields it updates. Or if i update the image it updates.
View:
<?php echo $form->labelEx($model,'pimg'); ?>
<?php echo $form->fileField($model, 'pimg',array('id'=>'imgInput',)); ?>
<?php echo $form->error($model,'pimg'); ?>
Controller:
public function actionEdit($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Product']))
{
$model->pimg=CUploadedFile::getInstance($model,'pimg');
$fileName=$model->pimg;
$model->attributes=$_POST['Product'];
if($model->save())
$model->pimg->saveAs('images/'.$fileName);
$this->redirect(array('display','id'=>$model->productid));
}
$this->render('edit',array('model'=>$model,));
}
Model rules:
array('name, category, model, brand, description, price', 'required'),
array('pimg', 'file','types'=>'jpg','on'=>'create', 'allowEmpty'=>false),
array('pimg', 'file','types'=>'jpg','on'=>'update', 'allowEmpty'=>true),
I think the problem is with the controller. I keep getting the error:
Fatal error: Call to a member function saveAs() on a non-object in D:\wamp\www\testfolder\protected\controllers\ProductController.php on line 147
line 147: $model->pimg->saveAs('images/'.$fileName);
Image appears but then image name from db doesn't appear next to the choose file button renders stating no file chosen.
Note that I am new to Yii, and am stuck with this.
The problem is in your actionEdit i.e here
$model->pimg=CUploadedFile::getInstance($model,'pimg');
$fileName=$model->pimg;
$model->attributes=$_POST['Product'];
if($model->save())
$model->pimg->saveAs('images/'.$fileName);
here you should check if the image has been uploaded or not. what happens is that yii is unable to execute this line $model->pimg->saveAs('images/'.$fileName); as no image has been uploaded. so you need to wrap it inside a condition i.e
if(!empty($model->pimg))
{
$model->pimg->saveAs('images/'.$fileName);
}
so your final code should be something like this
$model->attributes=$_POST['Product'];
$model->pimg=CUploadedFile::getInstance($model,'pimg');
if(!empty($model->pimg))
{
$fileName=$model->pimg->name;
$model->pimg->saveAs('images/'.$fileName);
}
if($model->save())
//render here
Update 1
If you want that on update null should not be stored in your database then you can do this
$model=$this->loadModel($id);
$prevImage=$model->pimg; //add this line
and then where you are using this
if(!empty($model->pimg))
{
$model->pimg->saveAs('images/'.$fileName);
}
else{
$model->pimg=$prevIMage;
}
add this else code
I want to edit a record from a database with a Zend Form (Zend Framework 2).
In ZF1 I did in my controller:
$values = $table->getValues();
$form = new MyForm();
$form->populate($values);
$this->view->form = $form;
and in the viewscript:
<?php echo $this->form ?>
In ZF2 I tried in my controller:
$values = $table->getValues();
$form = new MyForm();
$form->populateValues($values); // form->setData($values) does not work either
return array('form' => $form);
and in my viewscript:
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>
but that renders the form, without data however.
what is the correct way to do this?
You need to call prepare() on your form before you open it in your view script. For example:
<?php $form->prepare(); ?>
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>
See the reference guide here
Note the following point:
the prepare() method. You must call it prior to rendering anything in
the view (this function is only meant to be called in views, not in
controllers).
The issue was that $table->getValues() in my code returns an arrayObject whereas populateValues() expects an array.
Trying to load a block with a datepicker from a view.. but it loads only the textfield without datepicking feature. What am I doing wrong?
The create view
<?php echo $form->dropDownList($model,'operation_type',CHtml::listData(OperationType::model()->findAll(),
'id','name'),array(
'class'=>'span3',
'empty'=>'----- Type -----',
'id'=>'idOpType',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('operations/meta'),
//'dataType'=>'json',
'data'=>array('idOpType'=>'js:this.options[this.selectedIndex].innerHTML'),
'success'=>'function(data){
$("#opTypeBlock").html(data);
}',
),
)); ?>
The Controller Action
public function actionMeta(){
$data= new OperationsMeta();
$this->renderPartial('_meta',array('model'=>$data));
}
The view I am trying to load
<p>Select due date</p>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',array(
'attribute'=>'param',
'options'=>array(
'showAnim'=>'fold',
),
'model'=>$model,
'htmlOptions'=>array(
'style'=>'height:20px;',
'class'=>'inline',
'id'=>'datepickerOpType',
),
));
?>
Try
$this->renderPartial('_meta',array('model'=>$data), false, true);
so it loads JS files.
Use renderAjax() which injects into the rendering result with JS/CSS scripts and files which are registered with the view, as stated here.