I have a form and I want to upload a file. here is my code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'show-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<fieldset>
<legend>DATI TECNICI</legend>
<div class="row">
<?php echo $form->labelEx($model,'tec_data_file'); ?>
<?php echo $form->fileField($model,'tec_data_file',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'tec_data_file'); ?>
</div>
</fieldset>
<?php $this->endWidget(); ?>
</div><!-- form -->
There was nothing added to the database after submitting, I did a bit of debuging with firebug and figured out that filefield generates a code like this:
<input id="ytShow_tec_data_file" type="hidden" name="Show[tec_data_file]" value="">
<input id="Show_tec_data_file" type="file" name="Show[tec_data_file]" maxlength="45" size="45">
and two data are sent by $_POST for tec_data_file (which is my file field in db). The first var is empty (I think it is related to first hidden input). and the second one contains my file. and when I'm assigning the variables to my model for saving:
$modelPhoto->attributes = $_POST['Photo'];
the tec_data_file gets an empty string! So nothing gets uploaded to my db. Anyone have an idea how to solve this? If you need more i
You need something like:
$model = new Photo;
$model->attributes = $_POST['Photo'];
$model->image = CUploadedFile::getInstance($model,'file');
where file is the name of the field.
I haven't tested this, it's from the documentation.
Related
I am trying to pass the variable to view the page through the siteController, however when I try, I always get the error undefined Variable.
Here is my code in the siteController:
public function actionIndex(){
$Opere = Opere::find()->all();
return $this->render('index', [
'titolo' => $titolo, 'tecnica' => $tecnica, 'misura' => $misura, 'anno' => $anno,
'prezzo' => $prezzo, 'note' => $note, 'url_grandi' => $url_grandi]);
}
and here below is the code on the index page:
<?php foreach ($Opere as $o): //var_dump($o); ?>
<div class='grid-item'>
<img src='<?php $url_grandi;?>' class='grid-item-img'/>
<div class='grid-item-overlay'>
<div class='text'>
<div class='innerText'>
Titolo: <?php $titolo;?><br />
Tecnica: <?php $tecnica;?><br />
Misura: <?php $misura;?><br />
Anno: <?php $anno;?><br />
Prezzo: € <?php $prezzo;?><br />
Note: <?php $note;?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Thank you in advance
I found the solution, I added the incorrect variable in my foreach loop. I was trying to call the variable $titolo, however, in my foreach loop I had specified that the variable $titolo as $o. This is why it did not recognize the variable $titolo.
The following below is the correct code string to output the db data:
<?= $o->titolo;?>
and not:
<?php echo $titolo;?>
hope this helps someone else with a similar problem.
i want to implement this html code into yii format.
<input type="checkbox" name="" class="checkbox1" onclick="" id=""/>
<label class="gender" for="" id="">Female</label>
i tried to implement this code.
$form->checkBox($model,'gender',array('class'=>'gen'),array('template'=>'{label}')
but i don't know how to assign gender css class to label field? could please suggest me how to implement above html code in right way.
Please refer this Different Checkbox Properties. you will get answer from there...
if you want to use Chtml::checkBox...
<?php
echo CHtml::checkBox('gender',
array(''),array('class'=>'checkbox1')); ?>
<label style="width:150px;"class="gender">
<?php echo 'Gender' ;?>
</label>
if you want to use $form->checkBox...
<?php
echo $form->checkBox($model,'gender', array('class'=>'checkbox1'));
?>
<label style="width:150px;"class="gender">
<?php echo 'Gender';?>
</label>
Try it's working i have tried in my form...
CHtml::checkBox() gives you only the input tag
To create a <label> you should use CHtml::label() method. There you can set class property for this tag.
Edited:
I do this:
<?php echo $form->checkBox($model, 'gender', array('class'=>'checkbox1')); ?>
<?php echo $form->labelEx($model,'gender', array('class'=>'genger')); ?>
You just can add labelOptions array value to control label:
echo $form->checkBoxList(
$model,
'attr',
array('ggg','hhh'),
'htmlOptions' => array(
'labelOptions' => array(
//put here what you want
)
)
);
I have a very strange problem. I have a login form in Yii which works fine. After moving the website to another server I get
Error 400 The CSRF token could not be verified
I don't understand why it is working on the development server but not on the new server. here is my code:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
and here is the csrf configuration in my config file:
'enableCsrfValidation' => !isset($_POST['dontvalidate']) ? true : false,
if you need to see the example. Here is the one which is working, and here is the one with the problem
I opened the first example and found this html in source, This is correct.
<input type="hidden" value="df5a0fc9ab86f85cdcdabe6b2ee62e85d3ac0323"
name="YII_CSRF_TOKEN" />
on the second example page, I haven't found any html code as above. That means enableCsrfValidation is set to false in second example.
try debugging $_POST['dontvalidate'] in this page (or in config/main.php ).
i am trying to make a dropdown , but i don't want i to be visible for now , here is the code.
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropdownlist($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name')); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>
How do i make this type = 'hidden' or something like that ?
In other words , i want to keep the field but i don't want it to be shown.
try this..
<div class='row' style='display:none'>
Also, you can define style attribute of your dropDownList.
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
You can try with:
<div class="row">
<?php echo $form->labelEx($model,'lang_id'); ?>
<?php echo $form->dropDownList($model,'lang_id',CHtml::listData(Lang::model()->findAll(), 'id', 'name'), array('style' => 'display: none'); ?>
<?php echo $form->error($model,'lang_id'); ?>
</div>
// Post model
return array(
'categories' => array(self::MANY_MANY, 'Category', 'categories_posts(post_id, category_id)')
);
I already have the setup of tables
posts
id, title, content
categories
id, name
categories_posts
post_id, category_i
The problem I have is that I am getting an error when creating this form:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'title'); ?>
<?php echo $form->textField($model,'title',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'uri'); ?>
<?php echo $form->textField($model,'uri',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'uri'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
<div class="row">
<?php // echo $form->labelEx($model,'content'); ?>
<?php echo CHtml::activeDropDownList($model,'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>
<?php // echo $form->error($model,'content'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
The error is:
Property "Post.category_id" is not defined.
I am quite confused. What should I be doing to correct this?
I use the CAdvancedArBehavior to make this easier. I wouldn't be surprised if something like this gets rolled into the Yii core eventually either:
http://www.yiiframework.com/extension/cadvancedarbehavior/
It lets you write code like this in your controller:
$model->categories = $_POST['Post']['categories'];
I use this with the more-bugggy-than-I-would-like "Relation" widget/extension. You might need to use that to get the POST variables to structure correctly, I'm not sure.
I honestly thought that Yii has something like Kohana or Cake in their AR anyway. I guess I have to manually add them.
foreach ($_POST['category_id'] as $category_id)
{
$categoryPost = new CategoryPost;
$categoryPost->category_id = $category_id;
$categoryPost->post_id = $model->id;
$categoryPost->save();
}
Use in this way:
In controller
create object for category: $modelCat= new Category;
then in
<?php echo CHtml::activeDropDownList(**$modelCat**,'category_id', CHtml::listData(Category::model()->findAll(), 'id', 'name')); ?>