Welcome Guest, Not a member yet? Register   Sign In
Diffrerent rorrs with one validator
#1

He everyone, for example i need validate some dates and compare,

Can i did it in one custom validator, but i need different errors, while validate.

public function validDateCompare (string $dateStart, string $dateEnd): bool {

$DateStart = (new DateTime($dateStart));
$DateEnd = (new DateTime($dateEnd));


if($DateStart > $DateEnd) {
return false; // 'One date greater than one' -> send this message
}

if($DateStart == $DateEnd) {
return false; // 'Dates can be same' -> send this message
}


return true;

}

Ot is possible with two different validators?
Reply
#2

Hi,
You can display the desired error for any condition. For more information, see the link below:

https://github.com/datamweb/codeIgniter-...hp#L33-L43
Reply
#3

(This post was last modified: 12-11-2022, 03:26 PM by motoroller.)

(12-11-2022, 02:23 PM)datamweb Wrote: Hi,
You can display the desired error for any condition. For more information, see the link below:

https://github.com/datamweb/codeIgniter-...hp#L33-L43

public function validDateCompare (string $dateStart, string $dateEnd, string &$error = null): bool {

// Формируем объекты даты
$DateStart = (new DateTime($dateStart));
$DateEnd = (new DateTime($dateEnd));

if($DateStart > $DateEnd) {
$error = 'New message of erros';
return false;
}

return true;

}


any way i got error from Name of validator (validDateCompare )

(12-11-2022, 03:09 PM)motoroller Wrote:
(12-11-2022, 02:23 PM)datamweb Wrote: Hi,
You can display the desired error for any condition. For more information, see the link below:

https://github.com/datamweb/codeIgniter-...hp#L33-L43

public function validDateCompare (string $dateStart, string $dateEnd, string &$error = null): bool {

        // Формируем объекты даты
        $DateStart = (new DateTime($dateStart));
        $DateEnd  = (new DateTime($dateEnd));

        if($DateStart > $DateEnd) {
            $error = 'New message of erros';
            return false;
        }

        return true;

    }


any way i got error from Name of validator (validDateCompare )

Sulution Code

public function validDatesReport (string $value, string $fields, array $Data, &$error = null): bool {

$DateStart = (new DateTime($Data['dateStart']));
$DateEnd = (new DateTime($Data['dateEnd']));


if($DateStart > $DateEnd) {
$error = lang('Validation.validDateCompare');
return false;
}

if($DateStart->diff($DateEnd)->format('%a') > 186) {
$error = lang('Validation.validDateDifference');
return false;
}

return true;

}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB