Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to make Laravel 4's Validator class work outside the framework

I'm trying to use Laravel'4 Eloquent outside of the framework, since the Illuminate/Database package as been made availabile stand-alone via composer.

Eloquent itself is working fine, but I'm blocked trying to implement validation rules.

I've tried first with some pre-built library like Ardent and then with my own code but the result it's the same, I got this fatal-error:

Fatal error: Call to a member function make() on a non-object in vendor\illuminate\support\Illuminate\Support\Facades\Facade.php on line 177

The problem always start when I call Validator::make();

$validator = Validator::make(
    $this->attributes,
    array('name' => 'required')
);

Looking in debug it seems that static::resolveFacadeInstance('validator'); is called but it just return null.

I'm not familiar about how a Facades are meant to work, can anyone point me in the right direction? Thank you!

This is my composer.json:

{
    "require": {
        "illuminate/database": "~4.0",
        "illuminate/validation" : "~4.0",
        "laravelbook/ardent": "dev-master"
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "dev"
}
like image 601
Ingro Avatar asked Dec 01 '25 00:12

Ingro


1 Answers

For people who may need more information I'll report here the answer given to me on laravel's github repo by bencorlett (link):

// Store the factory instance somewhere, Maybe like:

class Validator {

    protected static $factory;

    public static function instance()
    {
        if ( ! static::$factory)
        {
            $translator = new Symfony\Component\Translation\Translator('en');
            static::$factory = new Illuminate\Validation\Factory($translator);
        }

        return static::$factory;
    }

    public static function __callStatic($method, $args)
    {
        $instance = static::instance();

        switch (count($args))
        {
            case 0:
                return $instance->$method();

            case 1:
                return $instance->$method($args[0]);

            case 2:
                return $instance->$method($args[0], $args[1]);

            case 3:
                return $instance->$method($args[0], $args[1], $args[2]);

            case 4:
                return $instance->$method($args[0], $args[1], $args[2], $args[3]);

            default:
                return call_user_func_array(array($instance, $method), $args);
        }
    }
}

$validator = Validator::make($data, $rules);
like image 156
Ingro Avatar answered Dec 02 '25 14:12

Ingro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!