0
양식에 날짜가있는 입력이 4 개인 경우 양식을 제출하기 전에 해당 날짜에 대한 유효성 검사를 만들고 싶습니다. 이 내 DatetimeHelper.phplaravel에 날짜 검증 기능을 만드시겠습니까?
class DateTimeHelper
{
//This formats a date for insertion into the database
public static function FormatForDB($date, $format = "m/d/Y", $if_null = "0000-00-00", $outputFormat = "date")
{
if (!isset($date)) {
return $if_null;
}
if (trim($date) == "") {
return $if_null;
}
if (trim($date) == "0000-00-00" || trim($date) == "00-00-0000") {
return $if_null;
}
//var_dump(Carbon::createFromFormat($format, $date));die;
try{
$parsedDate = Carbon::createFromFormat($format, $date);
if ($outputFormat == 'date') {
return $parsedDate->toDateString();
} else if ($outputFormat == 'datetime') {
return $parsedDate->toDateTimeString();
} else if ($outputFormat == 'time') {
return $parsedDate->toTimeString();
}
}catch(\Exception $e){
return $if_null;
}
}
//This formats the return date from the database
public static function FormatFromDB($date, $format = "m/d/Y", $if_null = "0000-00-00")
{
if (!isset($date)) {
return $if_null;
}
if (trim($date) == "") {
return $if_null;
}
if (trim($date) == "0000-00-00" || trim($date) == "00-00-0000") {
return $if_null;
}
return Carbon::createFromFormat("Y-m-d", $date)->format($format);
}
}
는이 방법이 전혀 작동하지 않는 것 같다
$invalid_fields = [];
$events_fields =['date_1' => 'date1',
'date_2' => 'date2',
'date_3' => 'date3',
'date_4' => 'date4'];
function input_validate($request)
{
foreach ($request->all() as $key => $value) {
switch ($key) {
case 'date1':
if (!$this->validate_date($value)) {
$this->invalid_fields[] = $this->event_fields[$key].' is not a valid date';
}
break;}
case 'date2':
if (!$this->validate_date($value)) {
$this->invalid_fields[] = $this->event_fields[$key].' is not a valid date';
}
break;}
내 Validator.php
public static function validate_date($date, $format = DATE_ATOM)
{
try{
$parsedDate = Carbon::createFromFormat($format, $date);
return true;
}catch(\Exception $e){
return false;
}
}
내 controller.php입니다. 어떤 도움을 주시면 감사하겠습니다. 모든 컨트롤러에서 validate_date 함수를 사용하여 입력을 확인합니다.