Reputation: 491
I have a table "attendance" and when I perform create action on it ,it works properly like the screen-shot below,
In this form I have used jquery to disable form field 'time in' and 'time out' if the status value is "Absent" or "Leave". my jquery code is below
<script>
$(document).ready(function() {
$("#status_id").on('change', function()
{
if($("#status_id").val()=='Absent' || $("#status_id").val()=='Leave')
{
$("#attendance-time_in").val('');
$('#attendance-time_in').attr('disabled', true);
$("#attendance-time_out").val('');
$('#attendance-time_out').attr('disabled', true);
}
else {
$("#attendance-time_in").val();
$('#attendance-time_in').attr('disabled', false);
$("#attendance-time_out").val();
$('#attendance-time_out').attr('disabled', false);
}
});
});
</script>
and _form.php code is
<?= $form->field($model, 'status', ['inputOptions' => ['class' => 'form-control','id' => 'status_id']])
->dropDownList([ 'Present' => 'Present', 'Absent' => 'Absent', 'Leave' => 'Leave',], ['prompt' => 'Select status']); ?>
<?=
$form->field($model, 'time_in')->widget(TimePicker::classname(), [
'name' => 'time_in',
'options'=>[
'id'=>'attendance-time_in',
],
]);
?>
<?=
$form->field($model, 'time_out')->widget(TimePicker::classname(), [
'name' => 'time_out',
'options'=>[
'id'=>'attendance-time_out',
],
]);
?>
its works fine for create action but when i update any data
like if i update data of arsalan khalid ,and i put absent instead of present the jquery works properly but when i click on update button it does not update the 'time in' and 'time out' attributes while all other attributes gets updated.for 'time in' and 'time out' it takes the previous value that it have i.e 9:40am and 5:52pm but i want it should have null values as i have selected "Absent" in status. AttendanceController Update Action
public function actionUpdate($id) { $model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
Attendance models Rules
public function rules()
{
return [
[[ 'staff_id'], 'integer'],
[['daytime'], 'safe'],
[['staff_id'], 'required'],
[['status','time_in', 'time_out'], 'string', 'max' => 45]
];
}
i am new in yii2. Many thanks in advance.
Upvotes: 3
Views: 475
Reputation: 133400
I think one problem is the html id name fo the fields
In Yii2 the html id of an input field inside an active form is, by default, formed automatically in the format 'classname-fieldname'
(all lowercase).
eg: namimg the model class = "Customer" the filed name is like
<input type="text" id="customer-name" class="form-control" name="Customer[name]" maxlength="16">
Then your field id should named 'attendance-status_id' , 'attendance-timein_id', and so on...
Try using this format for the html id
And second, just for test, modify your controller this way
if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
adding false
to save() in saving process are not involved the validation rules.
Upvotes: 2
Reputation: 1756
What you need to do is create another rule inside your Attendance model.
[['time_in', 'time_out'], 'filter', 'filter' => function ($value) {
if($this->status_id == AbsentStatus->status_id)
{
return null;
}
return $value;
}],
This way, if the status_id is equal to the AbsentStatus, you set the value of time_in and time_out as null.
Edit: you should substitute 'AbsentStatus->status_id' in the condition to the value of the id of your absent status.
Upvotes: 2