Skip to content

Validating

fkrzski edited this page Feb 2, 2022 · 1 revision

Required variables

FOO=BAR
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'FOO' => 'required'
]);

Alphanumerics

ALNUM1="alpha"
ALNUM2='12 45'
ALNUM3=alpha12345
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'ALNUM1' => 'alnum',
    'ALNUM2' => 'alnum',
    'ALNUM3' => 'alnum',
]);

Letters

LETTERS1="letters"
LETTERS2='lett ers'
LETTERS3=letters
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'LETTERS1' => 'letters',
    'LETTERS2' => 'letters',
    'LETTERS3' => 'letters',
]);

Integers

INTEGER1="1"
INTEGER2='2'
INTEGER3=3
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'INTEGER1' => 'integer',
    'INTEGER2' => 'integer',
    'INTEGER3' => 'integer',
]);

Floats

FLOAT1="1.1"
FLOAT2='2.432'
FLOAT3=.033
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'FLOAT1' => 'float',
    'FLOAT2' => 'float',
    'FLOAT3' => 'float',
]);

Booleans

BOOL='true'
BOOL2=false
BOOL3=yes
BOOL4=no
BOOL5=on
BOOL6="off"
BOOL7=1
BOOL8=0
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'BOOL1' => 'boolean',
    'BOOL2' => 'boolean',
    'BOOL3' => 'boolean',
    'BOOL4' => 'boolean',
    'BOOL5' => 'boolean',
    'BOOL6' => 'boolean',
    'BOOL7' => 'boolean',
    'BOOL8' => 'boolean',
]);

Multiple rules

MULTIPLE="1.1"
$dotenv = new Dotenv('path\to\your\file\.env'); 
$dotenv->start();
$dotenv->validator()->validate([
    'MULTIPLE' => 'required|float',
]);