Sven van den Boogaart
Sven van den Boogaart

Reputation: 12327

Laravel custom time validation

For my laravel 4.2 project i allow user to set an openingstime for they company. the format they give is 01:01 hours:minutes so the max value is 23:59 . When validating i cant check for numbers or alpha, how should i validate if the input is like the format ? I guess i should make a custom validator, but i dont really understand the docs of laravel, and the stack posts give different solution but also dont seem to follow the docs. link to the docs.

Upvotes: 0

Views: 794

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 152910

I don't know what you find hard about the docs. Try this: (code untested)

Validator::extend('time', function($attribute, $value, $parameters)
{
    $parts = explode(':', $value);
    if(count($parts) > 2){
        return false;
    }
    $hours = $parts[0];
    $minutes = $parts[1];
    if($hours >= 0 && $hours < 24 && $minutes >= 0 && $minutes < 60 && is_numeric($hours) && is_numeric($minutes)){
        return true;
    }
    return false;
});

Upvotes: 1

Related Questions