clear inputed data after ajaxsubmit and updating clistview - yii

in views index.php i have
<?php
$this->widget('zii.widgets.CListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'id'=>"post_list",
));
?>
<div id="addtodo">
<?php $this->renderPartial('ajax_page', array('model' => $model, ));?>
</div>
and in ajax_page.php
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'post-todo',
'enableAjaxValidation'=>false,
)); ?>
<?php
echo $form->textFieldRow(
$model,
'title',
array(
'class' => 'input-medium span6 addtodo',
'placeholder'=>'New todo task',
'prepend' => '<i class="glyphicon glyphicon-plus"></i>',
)
);
?>
<div class="row buttons">
<?php echo CHtml::ajaxSubmitButton ("Post",
CHtml::normalizeUrl(array('todo/add')),
array("success"=>'js:function(data){$.fn.yiiListView.update("post_list",{});}'),
array('class'=>'')
); ?>
</div>
<?php $this->endWidget(); ?>
So, All work fine. but after updating CListview, inputed text keeping in input form .
How to clear input form after updating CListview with Ajax?

You can do this by using the javascript .reset() method. You will need to change your ajax button to this;
<?php echo CHtml::ajaxSubmitButton ("Post",
CHtml::normalizeUrl(array('todo/add')),
array("success"=>'js:function(data){
$.fn.yiiListView.update("post_list",{});
$("#post-todo")[0].reset();
}'),
array('class'=>'')
); ?>

Related

change value in datepicker on selection of combo value in yii

I have a combo box in a form. According to the selected value from combo ,value of date must be changed in datepicker.How to do this?
code goes like this:
<div class="row col2">
<?php $records = CHtml::listData(CodeValue::model()->findAll(array('order' => 'code_lbl','condition'=>"code_type= 'visit_type'")), 'code_id', 'code_lbl');?>
<?php echo $form->labelEx($model,'visit_type'); ?>
<?php echo $form->dropDownList($model,'visit_type',$records,array('empty' => 'Select Visit Type')); ?>
<?php echo $form->error($model,'visit_type'); ?>
</div>
<div class="row col2">
<?php echo $form->labelEx($model,'next_visited_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker',array(
'model' => $model,
'attribute'=>'next_visited_date',
//'flat'=>true,//remove to hide the datepicker
'options'=>array(
'showAnim'=>'drop',//'slide','fold','slideDown','fadeIn','blind','bounce','clip','drop'
'dateFormat' => 'yy-mm-dd',
'showButtonPanel' => true, // show button panel
),
'htmlOptions'=>array(
'style'=>''
),
));
?>
<?php echo $form->error($model,'next_visited_date'); ?>
</div>
I have to change the visit date according to visit type selected.
You'll have to use ajax for this:
Change your drop down to use ajax, something like:
<?php echo $form->dropDownList($model,'visit_type',$records,array(
'empty' => 'Select Visit Type',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('controller/myAction'),//your controller and action name
'update'=>'#Model_next_visited_date', //replace Model with the model name
'success'=> 'function(data) {
$("#Model_next_visited_date").empty(); //replace Model with the model name
$("#Model_next_visited_date").val(data); //replace Model with the model name
} '
)));
?>
and create a new action:
public function actionMyAction()
{
//you will receive drop down value in $_POST['Model']['visit_type']
// Place your date logic here.....
}
Hope that helps

Duplicate insert when ajaxvalidation is true

Im having this weird problem that causes my model to double insert into the database when ajaxvalidation is on for my form. I dont understand why this happens, but this keeps on conflicting with my custom validation and I cannot move on. Can someone enlighten me on the problem.
Model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('category, ppmp_id', 'numerical', 'integerOnly'=>true),
array('category','required'),
array('q1, q2, q3, q4', 'numerical'),
array('category', 'myTestUniqueMethod'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('item_id, category, q1, q2, q3, q4, ppmp_id', 'safe', 'on'=>'search'),
);
}
public function myTestUniqueMethod($attribute,$params)
{
$sql = 'SELECT *
FROM ppmp_item
WHERE ppmp_id='.$this->ppmp_id.'and category='.$this->category;
$unique = Yii::app()->db->createCommand($sql)->queryRow();
if ($unique)
{
$this->addError('category', "Category already added to PPMP");
}
}
View:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'ppmp-item-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'category'); ?>
<?php
$data=PhilgepsCategory::model()->findAll();
$data=CHtml::listData($data,'id','class_name');
echo $form->dropDownList($model,'category',$data,array('options' => array('1'=>array('selected'=>true))));
?>
<?php echo $form->error($model,'category'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q1'); ?>
<?php echo $form->textField($model,'q1'); ?>
<?php echo $form->error($model,'q1'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q2'); ?>
<?php echo $form->textField($model,'q2'); ?>
<?php echo $form->error($model,'q2'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q3'); ?>
<?php echo $form->textField($model,'q3'); ?>
<?php echo $form->error($model,'q3'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'q4'); ?>
<?php echo $form->textField($model,'q4'); ?>
<?php echo $form->error($model,'q4'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
Controller:
public function actionCreate($ppmp,$bal)
{
$model=new PpmpItem;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['PpmpItem']))
{
$model->attributes=$_POST['PpmpItem'];
$model->ppmp_id = $ppmp;
$valid=$model->validate();
if($valid)
if($model->save())
$this->redirect(array('ppmp/view','id'=>$ppmp));
}
$this->render('create',array(
'model'=>$model,
'ppmp'=>$ppmp,
'bal'=>$bal,
));
}
The problem is that you shouldn't execute save() method when validating, because it will save when doing the ajax validation and when sending the data trough the form, so, you should do something like this:
...
...
if(isset($_POST['ajax']) && $_POST['ajax']==='ppmp-item-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
$valid=$model->validate();
if($valid)
if($model->save())
...
...
This is the same that gii uses to validate using ajax.

Yii CJuiDialog isn't being recognized

I've went through this tutorial http://www.yiiframework.com/wiki/561/ajax-login-form-with-validation-errors-inside-jquery-modal-dialog/ It appears to be functioning properly, but instead of the form being in the modal dialog, it just renders it right on the page like zii.widgets.jui.CJuiDialog isn't even there.
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'login-dialog',
'options'=>array(
'title'=>'Login',
'autoOpen'=>false,
),
));?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user_login_form',
'enableAjaxValidation'=>false,
'enableClientValidation'=>true,
'method' => 'POST',
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
)); ?>
<h1>
Login</h1>
<p>
Please fill out the following form with your login credentials:</p>
<p class="note">
Fields with <span class="required">*</span> are required.</p>
<div id="login-error-div" class="errorMessage" style="display: none;">
</div>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array("onfocus"=>"$('#login-error- div').hide();")); ?>
<?php //echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array("onfocus"=>"$('#login-error- div').hide();")); ?>
<?php //echo $form->error($model,'password'); ?>
<p class="hint">
Hint: You may login with <tt>demo/demo</tt>.
</p>
</div>
<div class="row rememberMe">
<?php echo $form->checkBox($model,'rememberMe'); ?>
<?php echo $form->label($model,'rememberMe'); ?>
<?php echo $form->error($model,'rememberMe'); ?>
</div>
<div class="row submit">
<?php echo CHtml::ajaxSubmitButton(
'Sign In',
array('/site/login.GetLogin'),
array(
'beforeSend' => 'function(){
$("#login").attr("disabled",true);
}',
'complete' => 'function(){
$("#user_login_form").each(function(){ this.reset();});
$("#login").attr("disabled",false);
}',
'success'=>'function(data){
var obj = jQuery.parseJSON(data);
// View login errors!
// alert(data);
if(obj.login == "success"){
$("#user_login_form").html("<h4>
Login Successful! Please Wait...</h4>
");
parent.location.href = "/";
}
else{
$("#login-error-div").show();
$("#login-error-div").html("Login failed! Try again.");$("#login-error-div").append("
");
}
}'
),
array("id"=>"login","class" => "btn btn-primary")
); ?>
</div>
<?php $this->endWidget(); ?>
</div>
<!-- form -->
<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
Its suppose to render like this above so when i click the link below it opens
echo CHtml::link('Login', array('/site/login.GetLogin'), array('onclick'=>'$("#login-dialog").dialog("open"); return false;'));
Its built in a widget, if you have a look at the tutorial. Here's the complete widget
<?php
class loginProvider extends CWidget{
public static function actions(){
return array(
'GetLogin'=>'application.components.actions.getLogin',
);
}
public function run(){
$this->renderContent();
}
protected function renderContent(){
echo '<span style="float:right;">';
if(Yii::app()->user->isGuest){
echo CHtml::link('Login', array('/site/login.GetLogin'), array('onclick'=>'$("#login-dialog").dialog("open"); return false;'));
echo '</span>';
$this->getController()- >renderPartial('application.components.views.login',array('model'=>new LoginForm));
}
else
echo CHtml::link('Logout ('.Yii::app()->user->name.')', array('site/logout'), array('visible'=>!Yii::app()->user->isGuest));
echo '</span>';
}
}

Yii , hidden dropdown

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>

Yii Many_Many Relationship and Form

// 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')); ?>