PHP code example of tamedevelopers / validator

1. Go to this page and download the library: Download tamedevelopers/validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tamedevelopers / validator example snippets


public function save(Request $request){

    $form = new Validator();
    or
    $form = form();
}

$form->noInterface(function($response){

    if($response->has('amount')){
        // exec code
    }
});

$form->after(function(){
    // execute code
});

->save(function($response){
    //
    $data = $response->only(['password', 'username']);
});

->save(function($response){
    
    $data = $response->except(['_token']);
});

->save(function($response){
    
    if($response->has('remeber_me')){
        // execute code
    }
});

->save(function($response){

    $data = $response->getForm();
});

->save(function($response){
    
    $data = [
        'name' => 'Lorem Name',
        'user_id' => rand(10000, 99999),
    ];

    $param = $response->merge($data, [
        'password' => md5($param['password'])
    ]);
});

->save(function($response){

    $data = $response->onlyData(['email', 'password'], [
        'email'     => '[email protected]', 
        '_token'    => md5('token'), 
        'age'       => 17,
        'password'  => 'test',
    ]);

    ---
    Only ['email', 'password'] will be returned.
});

->save(function($response){
    
    $data = $response->exceptData(['_token'], [
        'email'     => '[email protected]', 
        '_token'    => md5('token'), 
        'password'  => 'test'
    ]);

    ---
    Return all array element, except ['_token']
});

$form->rules([
    "string:country:==:0"   => 'Please Select a Country',
    "email:email"           => 'Please enter a valid email address',
])->save(function($response){

    $param = $response->param;


    $param->country;
    $param['country']

    ---
    As you can see, we're able to access data in both ways without errors
})

$form->toObject([
    'food' => 'Basmati Rice'
]);

$form->toArray([
    'food' => 'Basmati Rice'
]);

$form->toJson([
    'food' => 'Basmati Rice'
]);