File upload with Yii's ActiveForm - file-upload

I am trying to use Yii's ActiveForm to create a basic registration page with an image upload field. However, I am running into problems. I am using the following code to create the form tags:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'activity_form',
'enableAjaxValidation'=>true,
'stateful'=>true,
'enctype'=>'multipart/form-data'
));
The above code produces the following error message in Yii:
Property "CActiveForm.enctype" is not defined
I've also tried:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'activity_form',
'enableAjaxValidation'=>true,
'stateful'=>true,
array('enctype'=>'multipart/form-data')));
as well as:
$form=$this->beginWidget('CActiveForm', array(
'id'=>'activity_form',
'enableAjaxValidation'=>true,
'stateful'=>true),
array('enctype'=>'multipart/form-data')));
But neither of these work.
Any ideas as to what could be wrong? Can I use beginWidget to create a multipart form with file upload capabilities? What's the format I should follow for this? I can't seem to find any answers in the documentation or the forums.
Thanks!

Never mind. I found the solution to this. The trick is to use htmlOptions like so:
$this->beginWidget('CActiveForm', array(
'id'=>'activity_form',
'enableAjaxValidation'=>true,
'stateful'=>true,
'htmlOptions'=>array('enctype' => 'multipart/form-data')
));

Related

using yii rest api with default URL GET format rather than PATH format

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.
So in my config/main.php i have the following setting
'urlManager' => array (
'urlFormat' => 'path',
'rules' => array (
'student/<id:\d+>/<title:.*?>' => 'student/view',
'students/<tag:.*?>' => 'student/index',
array (
'apistudent/register',
'pattern' => 'api/<model:\w+>',
'verb' => 'POST'
),
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
)
),
I also have a controller named ApiStudentController with a method called actionRegister().
So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.
The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?
I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file
$app = Yii::createWebApplication($env->configWeb); //store the app
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one.
if (isset($_GET['r'])) {
Yii::app()->urlManager->setUrlFormat('get');
}
$app->run(); //run the app
I dont know whether this solves your problem. But this can give you an idea. Happy Coding!

Yii CListView's friendly url using history.js

I'm trying to make friendly urls using ClistView with history.js enabled.
right now my urls look like this
localhost/ModuleName/controllerName/PageName/index/?PageName_sort=price.desc&PageName_page=3
I'm trying to make it look nicer (?PageName_sort=price.desc&PageName_page=3) to something like this or something similar
localhost/moduleName/controllerName/PageName/sort/price/desc/page/3
i would like to remove the /index/ too
My action Controller looks like this
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('PageName',array(
'pagination'=>array(
'pageVar'=>'page'
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
My UrlManager looks like this
'PageName/page/<page:\d+>'=>'PageName/',
any idea who this can be done? or what i'm missing? Thanks
I'm not sure about getting to the url as you've specified. You can however get your url's in the following format in at least Yii 1.1 without adding additional rules to urlManager.
localhost/moduleName/controllerName/index/PageName_sort/price.desc/PageName_page/3
This is because CController::createUrl() is used to create the pagination links in CPagination::createPageUrl()

app can't find module?

I am working on a project. It was fine until I installed new windows on my PC. But now project is same and when I access
Yii::app()->controller->module
It returns null. It also returns null for: Yii::app()->controller->module->id. When I viewed config file it had admin module in it. I don't know why is it's returning null. Can't find a way out.
Module in config file is like:
'modules' => array(
// uncomment the following to enable the Gii tool
'admin',
'gii' => array(
'generatorPaths' => array(
'bootstrap.gii'
),
'class' => 'system.gii.GiiModule',
'password' => '1234',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters' => array('127.0.0.1', '::1'),
),
),
When you access Yii::app()->controller->module then it will return the module that current controller belongs to. It returns null if the controller does not belong to any module. Please make sure the current controller you accessed which is belong to any module you configured.
You can see this link: http://www.yiiframework.com/doc/guide/1.1/en/basics.module to work with Module.
If you want to see which modules are loaded then use:
print_r(Yii::app()->getModules());
You are not 'in' a module and want to get a specific module (like admin module): you can do
Yii::app()->getModule('admin');

how to create a link to a module in yii ?

Hello and thanks for reading.
I was wondering how i can link to a file from my main site to a module
eg ;
if i am in protected/views on a file and i want to link to protected/modules/user/profile where i call an action.
Now i get an error that states that action user does not exists.
How can i create a link to a module following the following yii sintax ;
<?php echo CHtml::link('Link Text',array('link to module',
'param1'=>'value1')); ?>
<?php echo CHtml::link(Yii::app()->createurl('Module/Controller/Action'),array(
'param1'=>'value1')); ?>
this may help with combining CHtml::link with a link to a module and it's controller/action combination:
echo CHtml::link("click here",
Yii::app()->createurl('/module/controller/action', array(
'id' => $model->id // Params
))
);
i managed to figure it out i tried to put this and it works ''/user/profile''
anybody has other ideas let me know

Yii multiselect widget

I have the following code that produces a jquery ech multiselect menu widget in Yii. (Eric Hynds multiselect).
I have downloaded the extension and unzipped in extensions folder
and my folder structure is My_lokal_Yii/protected/extensions/EchMultiSelect/EchMultiSelect
this is my view file code..
<?php $this->widget('ext.EchMultiselect.EchMultiselect', array(
'model' => $model,
'dropDownAttribute' => 'color',
'data' => $list,
'dropDownHtmlOptions'=> array(
'style'=>'width:378px;',
),
));?>
and in config/main.php i have imported the extension as
'import'=>array(
'application.models.*',
'application.components.*',
'application.extensions.EchMultiselect.*'
),
But i am getting error that "Alias "ext.EchMultiselect.EchMultiselect" is invalid. Make sure it points to an existing PHP file and the file is readable."
I have given all the permissions for the EchMultiselect file.
Please help me in this.. Thank you.. I tried a lot but did't get the exact result..
my folder structure is
My_lokal_Yii/protected/extensions/EchMultiSelect/EchMultiSelect
but in your config/main.php, there is:
... 'application.extensions.EchMultiselect.*'
------------------------------------^
It lacks the capital letter "s" and therefore the exact path/naming to/of your extension, I had the same "problem" with it ;-) , maybe it helps...
Greetings