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
Related
When I want to get the variable from the form the post action doesn't load .
This is my view:
<?php
$form = ActiveForm::begin();
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
if ($model->load(Yii::$app->request->post())){
exit(var_dump('everything is ok'));
}else {
exit(var_dump('nothing is right'));
}
The result is 'nothing is right'.
Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below
$model->load(Yii::$app->request->post(),'');
So your complete form should look like
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end();?>
EDIT
and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load
if (Yii::$app->request->isPost) { //this should be here before the rest of the code
if ($model->load(Yii::$app->request->post(), '')) {
exit(var_dump('everything is ok'));
} else {
exit(var_dump('nothing is right'));
}
}
This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.
So your post Yii::$app->request->post() returns:
array(
'username' => 'value of username',
)
And your $model->load looks for
array(
'modelName' => array(
'username' => 'value of username',
)
)
To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
Or the wrong way, modify your input and inside username use:
<input type="text" name="modelName[username]" placeholder="FullName">
Of course where I put username you must put your real model name.
Finally I find the solution
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>
<?php ActiveForm::end();?>
thank you for all
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'=>'')
); ?>
I'm new to Yii framework. I'm using two radiobuttonlist in my advanced search form. I'm using the below lines to create radiobuttonlist.
<div class="row">
<?php #echo $form->label($model,'ReviewedDate'); ?>
<?php echo $form->radioButtonList($model, 'ReviewedDate',
array('1' => 'Reviewed', '' => 'Not Reviewed')
); ?>
</div>
<div class="row">
<?php echo $form->radioButtonList($model, 'Approved',
array('0' => 'Rejected', '1' => 'Approved')
); ?>
</div>
Now, I want to create a seperate radiobutton(4 buttons - Approved, Rejected, Reviewed, Not Reviewed) from the radiobuttonlist and Not reviewed has to be the selected by default when I go to that page.At a time only one button can be selected. How can I do this
Try:
<div class="row">
<?php #echo $form->label($model,'ReviewedDate'); ?>
<?php echo $form->radioButtonList($model, 'ReviewedDate',
array('1' => 'Reviewed', '' => 'Not Reviewed','2'=>'Approved','3'=>'Reviewed','4'=>'rejected'
); ?>
</div>
<script> $( document ).ready(function() {
$('your id /selector radio button').prop('checked',true);
}); </script>
I have a simple CFormModel subclass that is used as a form to change a password. The class has only two attributes (password1, password2 - for confirmation -). Here is the code:
class ChangePasswordForm extends CFormModel {
public $password1;
public $password2;
public function rules() {
return array(
array('password1, password2', 'required'),
array('password2', 'compare', 'compareAttribute'=>'password1'),
array('password1, password2', 'safe'),
);
}
public function attributeLabels() {
return array(
'password1' => 'Enter new password',
'password2' => 'Confirm new password',
);
}
}
In a controller view file, I use a CActiveForm object to render this form:
<?php
$form=$this->beginWidget('CActiveForm',
array(
'id'=>'change-pwd-form',
'action'=>array('site/changePasswordPost'),
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)
);
echo $form->errorSummary($changePwdForm);
?>
<fieldset><legend>Change your password</legend>
<div class="row">
<?php
echo $form->labelEx($changePwdForm, 'password1');
echo $form->passwordField($changePwdForm, 'password1');
?>
</div>
<div class="row">
<?php
echo $form->labelEx($changePwdForm, 'password2');
echo $form->passwordField($changePwdForm, 'password2');
?>
</div>
<div class="row">
<?php
echo CHtml::submitButton('Change password');
?>
</div>
</fieldset>
<?php
$this->endWidget();
?>
In the controller action, I render the view above, passing a ChangePasswordForm object:
$changePwdForm = new ChangePasswordForm;
$this->render('changePassword', array('userid'=>$userid, 'userType'=>$t,
'changePwdForm'=>$changePwdForm));
Yii::app()->end();
The problem is that while the view is rendered, no Javascript code is generated. Nothing. So clicking on the submit button, although it should not let me do it (since the two password fields are required), it does submit. What is wrong with the code above and no Javascript is created?
Add $form->error() for fields that you need to validate. I suppose you perform ajax validation in controller, if not - search by performAjaxValidation here
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>