I'm struggling with my date form. This is my View:
<div class="form-group">
<label for="date">Date</label>
<input type="date" name="date">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'date') : '' ?></span>
</div>
I'm sending date to my controller and I wanted to display an error if the input date is current or before the current date. Here is my controller:
$sysDate = date("Y/m/d");
$validation = $this->validate([
'date' => [
'rules' => 'required|greater_than[' . $sysDate . ']',
'errors' => [
'required' => 'Appointment Date is required',
'greater_than' => 'Date should be in the future'
]
]
]);
if(!$validation){
return view('dashboard/index', ['validation' => $this->validator]);
}
else{
echo 'Valid';
}
After doing this, I'm getting an error even if I sent a date in the next days. Is there any solution for this or should I make a custom validation rule?
So last night, I dug up a lot and have found answers for my own questions. If you viewer are having the same problem as I then I guess my own answer here will help.
First I look out for CodeIgniter\Validation\FormatRules
to look for the compiled validation rules. Then looking at the given rules, I understood that I wanted to make. Here I made a function inside the FormatRules.php
instead of making other custom validation rules file and coded my own rule:
public function future_date(string $str = null): bool
{
$curdate = date("Y/m/d");
$date1 = date_create($curdate);
$date2 = date_create($str);
if($date1 < $date2)
{
return true;
}
else{
return false;
}
}
`
Then in my controller, I made this validation line:
'rules' => 'required|future_date'