Hi All,
I had status_id dropDownList on _form below that populate inspection_date and date_dismantled with dates from the value of 2 hiddenFields (today & today+7days) using onChange event.
<?php
/* @var $this InspectionController */
/* @var $model Inspection */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'inspection-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,
'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
<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,'date_inspected'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute' => 'date_inspected',
'model' => $model,
// additional javascript options for the date picker plugin
'options' => array(
'autoSize' => true,
'showAnim' => 'slideDown',
'showOn' => 'button',
'changeMonth' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'style' => 'height:20px;',
),
));?>
<?php echo $form->error($model,'date_inspected'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'status_id'); ?>
<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions(),
array(
'prompt'=>'Select Status',
'onChange'=>'if (this.value == 3)
{
this.form.elements["Inspection[deluge_id]"].value = 1
this.form.elements["Inspection[los_id]"].value = 1
this.form.elements["Inspection[deboard_id]"].value = 1
this.form.elements["Inspection[inspection_date]"].value = ""
this.form.elements["Inspection[date_dismantled]"].value = this.form.elements["today"].value
}
else if (this.value == "")
{
this.form.elements["Inspection[inspection_date]"].value = ""
this.form.elements["Inspection[date_dismantled]"].value = ""
}
else
{
this.form.elements["Inspection[inspection_date]"].value = this.form.elements["today_+7days"].value
this.form.elements["Inspection[date_dismantled]"].value = ""
}'
)); ?>
<?php echo $form->error($model,'status_id'); ?>
</div>
<?php echo CHtml::hiddenField('today', date('Y-m-d')) ?>
<?php echo CHtml::hiddenField('today_+7days', date('Y-m-d', strtotime('+7 days '))) ?>
<div class="row">
<div class="span-4">
<?php echo $form->labelEx($model,'deluge_id'); ?>
<?php echo $form->dropDownList($model,'deluge_id', $model->getDelugeOptions()); ?>
<?php echo $form->error($model,'deluge_id'); ?>
</div>
<div class="span-4 last">
<?php echo $form->labelEx($model,'los_id'); ?>
<?php echo $form->dropDownList($model,'los_id', $model->getLosOptions()); ?>
<?php echo $form->error($model,'los_id'); ?>
</div>
<?php echo $form->labelEx($model,'deboard_id'); ?>
<?php echo $form->dropDownList($model,'deboard_id', $model->getDeboardOptions()); ?>
<?php echo $form->error($model,'deboard_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'inspection_date'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute' => 'inspection_date',
'model' => $model,
// additional javascript options for the date picker plugin
'options' => array(
'autoSize' => true,
'showAnim' => 'slideDown',
'showOn' => 'button',
'changeMonth' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'style' => 'height:20px;',
),
));?>
<?php echo $form->error($model,'inspection_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'date_dismantled'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute' => 'date_dismantled',
'model' => $model,
// additional javascript options for the date picker plugin
'options' => array(
'autoSize' => true,
'showAnim' => 'slideDown',
'showOn' => 'button',
'changeMonth' => true,
'changeYear' => true,
'dateFormat' => 'yy-mm-dd',
),
'htmlOptions' => array(
'style' => 'height:20px;',
),
));?>
<?php echo $form->error($model,'date_dismantled'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
If I want to populate the inspection_date with the value of date_inspected +7 days then what codes should I add or change here:
<?php echo $form->dropDownList($model,'status_id', $model->getStatusOptions(),
array(
'prompt'=>'Select Status',
'onChange'=>'if (this.value == 3)
{
this.form.elements["Inspection[deluge_id]"].value = 1
this.form.elements["Inspection[los_id]"].value = 1
this.form.elements["Inspection[deboard_id]"].value = 1
this.form.elements["Inspection[inspection_date]"].value = ""
this.form.elements["Inspection[date_dismantled]"].value = this.form.elements["today"].value
}
else if (this.value == "")
{
this.form.elements["Inspection[inspection_date]"].value = ""
this.form.elements["Inspection[date_dismantled]"].value = ""
}
else
{
this.form.elements["Inspection[inspection_date]"].value = this.form.elements["today+7days"].value
this.form.elements["Inspection[date_dismantled]"].value = ""
}'
)); ?>
<?php echo $form->error($model,'status_id'); ?>
</div>
<?php echo CHtml::hiddenField('today', date('Y-m-d')) ?>
<?php echo CHtml::hiddenField('today+7days', date('Y-m-d', strtotime('+7 days '))) ?>