I'm running into a bug where my model is saving multiple times whenever I submit a gii-generated field. I've created an error log so that I can see what is being called multiple times, and I have found out that the function actionCreate is the part of my code that being called three times (Though sometimes two). When I fill out a form an click submit, the error log shows that the actionCreate function is being called three times.
The controller form looks like this
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$model=new Account;
error_log("How many times do I call actionCreate");
// To-Do make the user account creation update via ajax
// $this->performAjaxValidation($model);
if(isset($_POST['Account']))
{
$model->attributes=$_POST['Account'];
if($model->save())
{
echo 'do we reach here';
$this->redirect(array('index','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
My form, as well, looks like this
<?php
/* #var $this AccountController */
/* #var $model Account */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'account-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>true,
)); ?>
<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,'name'); ?>
<?php echo $form->textField($model,'name',array('size'=>40,'maxlength'=>40)); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'mobile_comp'); ?>
<?php echo CHtml::dropDownList(CHtml::activeName($model,'mobile_comp'), $select,
$model->providerOptions, array('empty'=>'(Select Your Provider')); ?>
<?php echo $form->error($model,'mobile_comp'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'msisdn'); ?>
<?php echo $form->textField($model,'msisdn'); ?>
<?php echo $form->error($model,'msisdn'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'pin'); ?>
<?php echo $form->textField($model,'pin'); ?>
<?php echo $form->error($model,'pin'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'company'); ?>
<?php echo $form->textField($model,'company',array('size'=>40,'maxlength'=>40)); ?>
<?php echo $form->error($model,'company'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'balance'); ?>
<?php echo $form->textField($model,'balance',array('size'=>40,'maxlength'=>40)); ?>
<?php echo $form->error($model,'balance'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
The error was in my form where I had ajaxValidation=>true instead of ajaxValidation=>false
I was accidentally calling the function ActionCreate multiple times with one field .
Becuase you've commented in line:
// To-Do make the user account creation update via ajax
// $this->performAjaxValidation($model);
Function $this->performAjaxValidation($model); validates form data if requested via Ajax and returns validation results as JSON and stops application execution ahead.
In your case if Ajax validation is happening then there is nothing to check for validation and problem is saving your model.
Just un-comment $this->performAjaxValidation($model);
ALSO: Please add code of function $this->performAjaxValidation($model)
Related
I have a form like this as yii active record :
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'arsip-perihal-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'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,'nama_arsip'); ?>
<?php echo $form->textField($model,'nama_arsip',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'nama_arsip'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'kode_primary'); ?>
<?php echo $form->textField($model,'kode_primary',array('size'=>60,'maxlength'=>255)); ?>
<?php echo $form->error($model,'kode_primary'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Keterangan'); ?>
<?php echo $form->textField($model,'Keterangan'); ?>
<?php echo $form->error($model,'Keterangan'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Can I use a HTML tag, An example textbox
First name: <input type="text" name="fname"><br>
if it can, how to use it? how to set default from $model like the form above?
Thank You,
When you use $form->textField, CActiveForm generates a textbox with ModelName[attributeName] name. So you can provide name for your textbox with this structure.
<input type="text" name="ModelName[fname]">
Note, "ModelName" is your model class, for example, "User".
i just need to do a registration form, i'm battling with completing this task with CActiveForm. Basically its just inserting a new db record on form submit. This is what i have,
MyView
<!--begin a form-->
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'user-registration-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
<!--error handling-->
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
<?php echo $form->error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
<?php echo $form->error($model,'lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'age'); ?>
<?php echo $form->textField($model,'age'); ?>
<?php echo $form->error($model,'age'); ?>
</div>
<?php $this->endWidget(); ?>
<!--end a form-->
My Controller that renders the above view, this where i'm stuck, I also created a model called User(haven't done any code in it, default)
class RegisterController extends Controller
{
public function actionIndex()
{
$model = User::
$this->render('index', array('model'=>$model));
}
}
From my research i found there is something like, jst dnt know how to use it
link
$post=new Post;
$post->title='sample post';
$post->content='post body content';
$post->save();
Thanks in advance
You need to do in your actionIndex:
public function actionIndex()
{
$model = new User;
if(isset($_POST['User']))
{
$model->attributes = $_POST['User'];
if($model->save())
//Do any stuff here. for example redirect to created user view.
}
$this->render('index', array('model'=>$model));
}
I recommend you to read the Building a blog system with Yii tutorial. This is very good resource for learning yii better and also learn you the most important parts of any web application.
I am developing this website that requires me to combine two models in one view where they have one to many relationship between them. The models name is Home and Image meaning Home has many Images but Image only has one Home.
I have manged to combine The view together but the problem that i encountering is to get all of the images. For example i have 6 images i want to display them or if i have 5 images i want to display them.
Home Controller UpdateMethod
public function actionUpdate($id)
{
$home=$this->loadModel($id);
$image=Image::model()->findByAttributes(array('homeId'=>$home->id));
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Home'],$_POST['Image'])){
$home->attributes=$_POST['Home'];
$image->attributes=$_POST['Image'];
$valid=$home->validate();
$valid=$image->validate() && $valid;
if($valid){
if($home->save()){
$image->save();
}
}
}
$this->render('update',array(
'home'=>$home,
'image'=>$image,
));
}
My _form.php to join them together
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'home-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($home); ?>
<div class="row">
<?php echo $form->labelEx($image,'imageUrl'); ?>
<?php echo $form->textField($image,'imageUrl',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($image,'imageUrl'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'recentEvents'); ?>
<?php echo $form->textField($home,'recentEvents',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($home,'recentEvents'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($home,'introduction'); ?>
<?php echo $form->textArea($home,'introduction',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($home,'introduction'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($home->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
Update I had FindByattribues instead of FindAllByAttribues in the model so now it is returning an array. Now how to process that array in the view?
Okay i figured it out by myself posting this to maybe help someone who needs it. I the view i did the following.
<?php
foreach($image as $image){
?>
<div class="row">
<?php echo $form->labelEx($image,'imageUrl'); ?>
<?php echo $form->textField($image,'imageUrl',array('size'=>60,'maxlength'=>100)); ?>
<?php echo $form->error($image,'imageUrl'); ?>
</div>
<?php
}
?>
I am new to yii and I am working on creating drop down .I faced the error please suggest me.. Here his my drop down code
<?php
/* #var $this CreateproductController */
/* #var $model Createproduct */
/* #var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'createproduct-form',
'method'=>'post',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.E0E0E0 EFF0F2
'enableAjaxValidation'=>false,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row" style="background-color: #F7F7F7; height:30px;">
<?php echo $form->labelEx($model,'Product Type'); ?>                
<?php echo CHtml::dropDownList($model,'pr_name',$model->getTypeOptions() ); ?>
<?php echo $form->error($model,'pr_type'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save',array('class'=>'button1')); ?>
</div>
<?php $this->endWidget(); ?>
</div>
the getTypeOptions() method is has below and constant used inside that method and declared
const CAT_EMPTY=0;
const CAT_PHONE=1;
const CAT_ROUTERS=2;
const CAT_ACCESSORIES=3;
const CAT_SERVICES=4;
public function getTypeOptions()
{
return array(
self::CAT_EMPTY=>'Select_Category',
self::CAT_PHONE=>'Phones',
self::CAT_ROUTERS=>'Routers',
self::CAT_ACCESSORIES=>'Accessories',
self::CAT_SERVICES=>'Services',
);
}
Help me out..
Problem in usage of CHtml::dropDownList
you should use like this:
<?php echo CHtml::dropDownList("model_name[pr_name]",'selected_value',$model->getTypeOptions() ); ?>
Alternatively, you can use $form widget dropDownList function
<?php echo $form->dropDownList($model, 'pr_name',$model->getTypeOptions() ); ?>
I tried to bring the comment form in the post view list where user can put a comment .
my code which I am write for the above problem ...
<h5>Add your Comment</h5>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
</div>
<?php else: ?>
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment
)); ?>
<?php endif; ?>
"The _form contain....."
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>true,
)); ?>
<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,'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,'author'); ?>
<?php echo $form->textField($model,'author',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'author'); ?>
</div>
It gives the error "Undefined variable: comment "
You need to define $comment. You are trying to pass a model to the form. This is usually a model of a database table. It looks like you are using active form. That means you are using the Active Record model in Yii. You should have a model that covers your comment table. If you need to know how to create a model you can find out how to use Gii here.
If you already have a comment model then you just need to define the model. Something like:
$comment = new Comment();
$this->renderPartial('/comment/_form',array('model'=>$comment));
It looks like this is a view that sometimes calls another view. You could define the $comment variable in the controller that calls the original view. You would just have to pass the comment variable into the original view as well as the second one.
Without knowing exactly where the error occurs, it seems to me that the most logical location is in this snippet:
<?php $this->renderPartial('/comment/_form',array(
'model'=>$comment
)); ?>
The solution would then probably be to replace $comment by 'Comment' (or something similar, I'm not really familiar with Yii).