src/Traits/CrudModels.php
public static function validateModel(array $config, \Illuminate\Http\Request $request = null) {
        if (is_null($request)) {
            $request = request();
        }
        $rules = [];
        $modeloM = class_basename($config["modelo"]);
        $modelo = strtolower($modeloM);
        if (isset($config['rules'])) {
            if (is_array($config['rules'])) {
                $rules = $config['rules'];
            }
        }
        if (count($rules) == 0) {
            $objModelo = new $config['modelo'];
            if (isset($objModelo->rules)) {
                if (is_array($objModelo->rules)) {
                    $rules = $objModelo->rules;
                }
            }
        }
        $auxIdCambio = $request->get($config["id"]);

        $rules = \Sirgrimorum\CrudGenerator\CrudGenerator::translateArray($rules, ":model", function($string) use ($auxIdCambio) {
                    return $auxIdCambio;
                }, "Id");
        if (count($rules) > 0) {
            $customAttributes = [];
            foreach ($rules as $field => $datos) {
                if (array_has($config, "campos." . $field . ".label")) {
                    $customAttributes[$field] = array_get($config, "campos." . $field . ".label");
                }
            }
            $error_messages = [];
            if (isset($config['error_messages'])) {
                if (is_array($config['error_messages'])) {
                    $error_messages = $config['error_messages'];
                }
            }
            if (count($error_messages) == 0) {
                $objModelo = new $config['modelo'];
                if (isset($objModelo->error_messages)) {
                    if (is_array($objModelo->error_messages)) {
                        $error_messages = $objModelo->error_messages;
                    }
                }
            }
            $error_messages = array_merge(trans("crudgenerator::admin.error_messages"), $error_messages);
            $validator = \Illuminate\Support\Facades\Validator::make($request->all(), $rules, $error_messages, $customAttributes);
            return $validator;
        }
        return false;
    }
public static function validateModel(array $config, \Illuminate\Http\Request $request = null) {
        if (is_null($request)) {
            $request = request();
        }
        $rules = [];
        $modeloM = class_basename($config["modelo"]);
        $modelo = strtolower($modeloM);
        if (isset($config['rules'])) {
            if (is_array($config['rules'])) {
                $rules = $config['rules'];
            }
        }
        if (count($rules) == 0) {
            $objModelo = new $config['modelo'];
            if (isset($objModelo->rules)) {
                if (is_array($objModelo->rules)) {
                    $rules = $objModelo->rules;
                }
            }
        }
        $auxIdCambio = $request->get($config["id"]);

        $rules = \Sirgrimorum\CrudGenerator\CrudGenerator::translateArray($rules, ":model", function($string) use ($auxIdCambio) {
                    return $auxIdCambio;
                }, "Id");
        if (count($rules) > 0) {
            $customAttributes = [];
            foreach ($rules as $field => $datos) {
                if (array_has($config, "campos." . $field . ".label")) {
                    $customAttributes[$field] = array_get($config, "campos." . $field . ".label");
                }
            }
            $error_messages = [];
            if (isset($config['error_messages'])) {
                if (is_array($config['error_messages'])) {
                    $error_messages = $config['error_messages'];
                }
            }
            if (count($error_messages) == 0) {
                $objModelo = new $config['modelo'];
                if (isset($objModelo->error_messages)) {
                    if (is_array($objModelo->error_messages)) {
                        $error_messages = $objModelo->error_messages;
                    }
                }
            }
            $error_messages = array_merge(trans("crudgenerator::admin.error_messages"), $error_messages);
            $validator = \Illuminate\Support\Facades\Validator::make($request->all(), $rules, $error_messages, $customAttributes);
            return $validator;
        }
        return false;
    }
public static function validateModel ( array $ config , \ Illuminate \ Http \ Request $ request = null ) { if ( is_null ( $ request ) ) { $ request = request ( ) ; } $ rules = [ ] ; $ modeloM = class_basename ( $ config [ "modelo" ] ) ; $ modelo = strtolower ( $ modeloM ) ; if ( isset ( $ config [ 'rules' ] ) ) { if ( is_array ( $ config [ 'rules' ] ) ) { $ rules = $ config [ 'rules' ] ; } } if ( count ( $ rules ) == 0 ) { $ objModelo = new $ config [ 'modelo' ] ; if ( isset ( $ objModelo -> rules ) ) { if ( is_array ( $ objModelo -> rules ) ) { $ rules = $ objModelo -> rules ; } } } $ auxIdCambio = $ request -> get ( $ config [ "id" ] ) ; $ rules = \ Sirgrimorum \ CrudGenerator \ CrudGenerator :: translateArray ( $ rules , ":model" , function ( $ string ) use ( $ auxIdCambio ) { return $ auxIdCambio ; } , "Id" ) ; if ( count ( $ rules ) > 0 ) { $ customAttributes = [ ] ; foreach ( $ rules as $ field => $ datos ) { if ( array_has ( $ config , "campos." . $ field . ".label" ) ) { $ customAttributes [ $ field ] = array_get ( $ config , "campos." . $ field . ".label" ) ; } } $ error_messages = [ ] ; if ( isset ( $ config [ 'error_messages' ] ) ) { if ( is_array ( $ config [ 'error_messages' ] ) ) { $ error_messages = $ config [ 'error_messages' ] ; } } if ( count ( $ error_messages ) == 0 ) { $ objModelo = new $ config [ 'modelo' ] ; if ( isset ( $ objModelo -> error_messages ) ) { if ( is_array ( $ objModelo -> error_messages ) ) { $ error_messages = $ objModelo -> error_messages ; } } } $ error_messages = array_merge ( trans ( "crudgenerator::admin.error_messages" ) , $ error_messages ) ; $ validator = \ Illuminate \ Support \ Facades \ Validator :: make ( $ request -> all ( ) , $ rules , $ error_messages , $ customAttributes ) ; return $ validator ; } return false ; }
Runs a validation of a request based on a model and its configuration array
@param array $config The configuration array
@param Request $request Optional the request. If null, it will use request() function
@return mix Retuns the validator or false if there are no rules
Runs a validation of a request based on a model and its configuration array