Bakti Wijaya Bakti Wijaya - 2 months ago 28
PHP Question

Validate multiple conditions in Yii2

I have this kind of validation on

Leave Employee
:


  1. Validate if
    startdate
    less than
    applydate
    (done).

  2. Cannot apply leave if
    maximumquota
    less than 0 (done).

  3. If
    status = approved
    ,
    maximumquota - daysleave
    (done).



I want to add another validation on applying when
startdate
inputted exists and is approved.
startdate = startdate + daysleave
, on form
status=rejected
.

I already made an algorithm like this:

if(startdate='startdate' between startdate(Y-m-(d+'daysleave')->where('status'=='approved')
{
$_post = 'status'=rejected,
'applydate=date('Y-m-d'),
'startdate';
)
} else {

}


This is my Create controller:

if ($model->load(Yii::$app->request->post())){

if($model->daysleaveleft(Yii::$app->user->id,date('Y',strtotime($model->startdate)))>0)
{
//Edited
if($model->startdate < max($s))
{
$model->status='2';
}//
if($model->startdate < $model->applydate)
{
echo "Error !";
}
else
{
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
}
else{ $model->status='2';
}

}
else {
return $this->render('create', ['model' => $model,]);
}
}


And my Leave model:

public function DaysLeaveLeft($id,$year) {
$models = Leave::find()->select(['daysleave'=>'sum(daysleave)'])->where(['status'=>'approved','EmpId' => $id,'year(startdate)'=>$year])->One();
$Setting = Setting::find()->where(['Var'=>'MaxLeave'])->One();
return $Setting->Val - $models['daysleave'];
}

Answer

The step i used to make this works
1. Make an SQL Query to get End of Leave days,for example .
if startdate=03-08-2016,then enddate= startdate + Leave Days

ALTER FUNCTION enddays(@Id bigint)
RETURNS VARCHAR(10)
AS BEGIN
DECLARE @st DATE, @long INT,@Var VARCHAR(10)='0',@cekId INT =0
SELECT @cekId=id,@st=startdate,@long=leavedays FROM leave WHERE id=@Id  
IF @cekId > 0 BEGIN
SELECT @long +=COUNT(dt)
FROM calendar WHERE cast(dt as date) BETWEEN cast(@st as dt) and  cast(DATEADD(day,@long,@st) as date)
set @Var = CAST(CAST(DATEADD(DAY,@long,@st) AS DATE) AS VARCHAR(10))
END
RETURN @Var
END

2. And then make a procedure

ALTER procedure endleave
@id bigint
as
BEGIN
SELECT dbo.endays(l.id) from leave l
WHERE GETDATE() < CAST(dbo.enddays(l.id) as date) 
and EmpId=@id
and status= 1
END
  1. add this on leavecontroller.

    $id=Yii::$app->user->id; $query="exec endleave @id=$id"; $command=Yii::$app->db->createCommand($query); $s = $command->queryAll();

  2. Edit the controller.

    if ($model->load(Yii::$app->request->post())) { if($model->daysleaveleft(Yii::$app->user->id,date('Y',strtotime($model->startdate)))>0) { //Edited if($model->startdate < max($s)) { $model->status='2'; }// if($model->startdate < $model->applydate) { echo "Error !"; } else { $model->save(); return $this->redirect(['view', 'id' => $model->id]); } } else{ $model->status='2'; }

                    } 
         else {
            return $this->render('create', ['model' => $model,]);
    }
    }
    

Hope this helps

Comments