MaGnetas
MaGnetas

Reputation: 5108

Validate a timestamp value in laravel 5

I'm writing validator rules for checking if the data is valid before adding a new Eloquent model record.

It's quite clear with strings, decimals and integers.

But what about a timestamp field?

I added the column using timestamp method in one of my DB migrations, it works just fine. The only thing I need is to make sure the passed value will be a valid timestamp before executing the query.

Is there a direct/simple way or should I write a regexp for that?

Thank you

Upvotes: 7

Views: 25035

Answers (5)

PHP Worm...
PHP Worm...

Reputation: 4224

Use this rule

   public function rules()
    {
        return [
            'date_time' => 'date_format:Y-m-d H:i:s'
        ];
    }

Upvotes: 5

MURATSPLAT
MURATSPLAT

Reputation: 4830

I think that validate like this..

$rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

Let's test this rule

public function testSimpleTimeStampValidation()
{

    $data = ['start_at'  => '2015-12-1 12:12:58'];


    $rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

    $validator = \Validator::make($data, $rules);

    $this->assertTrue($validator->passes()); // passed !        
}

Again trying test with unwanted date format..

public function testSimpleTimeStampValidation()
{

    $data = ['start_at'  => '12-1-2015 12:12:58'];


    $rules = ['start_at' => 'date_format:Y-m-d H:i:s'];

    $validator = \Validator::make($data, $rules);

    $this->assertTrue($validator->passes()); // Failed !        
}

It looks this rules works very well..

more details Data Format Validation in L5

Upvotes: 9

vps
vps

Reputation: 420

Just create a new validation rule in laravel to validate the timestamp...

Validator::extend('isTimeStamp', function($attribute, $value, $parameters)
{
    return ((string) (int) $value === $value) 
           && ($value <= PHP_INT_MAX)
           && ($value >= ~PHP_INT_MAX);
});

You can now use isTimeStamp validation rule to validate timestamp.

Upvotes: -1

Joe
Joe

Reputation: 15802

You can attempt to use Carbon to parse the timestamp for you then run your validation in nice human-readable methods:

$date = Carbon::createFromTimeStamp( (int) $timestamp );
$min = Carbon::create( 2014, 12, 31, 23, 59, 59 );
$max = Carbon::create( 2015, 12, 31, 23, 59, 59 );
return $date->gt( $min ) && $date->lte( $max );

For actually validating the raw timestamp itself though, there's not much I can come up with other than just doing the same thing as above with raw numbers:

return 1420030799 < $timestamp && $timestamp <= 1451566799;

but that's much more difficult to read and maintain than the Carbon method.

Upvotes: 0

Pᴇʜ
Pᴇʜ

Reputation: 57703

There is no validation rule for timestamps in laravel. If you need to validate timestamps you can use somthing like that:

function isValidTimeStamp($timestamp)
{
    return ((string) (int) $timestamp === $timestamp) 
        && ($timestamp <= PHP_INT_MAX)
        && ($timestamp >= ~PHP_INT_MAX);
}

If you generate the timestamp in php there is no need to validate it. If it is a user input you could use the date validation rule of the laravel validator before you convert it into a timestamp.

Upvotes: -1

Related Questions