Welcome Guest, Not a member yet? Register   Sign In
New feature in validation
#1

(This post was last modified: 01-27-2023, 10:27 PM by enix-app.)

I want to make validation simplicity as possible with one (rules) object.

In my case, I want to use only one array of rules and then selects some keys I need:

PHP Code:
<?php

$rules 
= [
    'username' => 'required',
    'username_max_length' => 'required|max_length[20]',
    'password' => 'required|min_length[10]',
    'passconf' => 'required|matches[password]',
    'email'    => 'required|valid_email',
    '....'    => '....',
    // ...........
];

$validation Services::validation();

$validation->withKeyRules($rules'username|password|passconf');
$data = [
    'username' => '',
];

$validate $validation->run($data);

$validation->withKeyRules($rules, ['username_max_length|password|passconf']);
$data = [
    'username_max_length' => 'my_name_too_long',
];

$validate2 $validation->run($data); 

And below may patches:

PHP Code:
<?php

interface ValidationInterface {

    /**
    *  Validate with spesific key from array rules.
    */
    public function withKeyRules(array $rulesnull|string|array $keys = []);
}

class 
Validation extends Validation implements ValidationInterface {

    /**
    *  @param rules array required
    */
    public function withKeyRules(array $rulesnull|string|array $keys = [])
    {
        $this->rules $rules;

        // Skip this if $keys is empty.
        if(empty($keys)) return $rules;

        // Splits $keys to array
        if(is_string($keys)) {
            $keys explode("|"trim($keys));
        }

        // Eliminate unused key
        foreach($this->rules as $key => $value) {
            if(! in_array($key$keys)) {
                unset($this->rules[$key]);
            }
        }

        return $this;
    }


For a while you can use the code above to extends core service validation.

PHP Code:
<?php

class Services extends BaseService
{

    /**
    * The Validation class provides tools for validating input data.
    *
    * @return ValidationInterface
    */
    public static function validation(?ValidationConfig $config nullbool $getShared true)
    {
        if ($getShared) {
            return static::getSharedInstance('validation'$config);
        }

        $config ??= config('Validation');
        return new EnixValidation($configAppServices::renderer());
    }

}

interface 
EnixValidationInterface {

    /**
    *  Validate with spesific key from array rules.
    */
    public function withKeyRules(array $rulesnull|string|array $keys = []);
}

class 
EnixValidation extends Validation implements EnixValidationInterface {

    /**
    *  @param rules array required
    */
    public function withKeyRules(array $rulesnull|string|array $keys = [])
    {
        $this->rules $rules;

        // Skip this if $keys is empty.
        if(empty($keys)) return $rules;

        // Splits $keys string to array.
        if(is_string($keys)) {
            $keys explode("|"trim($keys));
        }

        // Eliminate unused key.
        foreach($this->rules as $key => $value) {
            if(! in_array($key$keys)) {
                unset($this->rules[$key]);
            }
        }

        return $this;
    }


OOT: I think we needs a new plaform for forum, with simple markdown to use.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB