dwightwatson / validating

Automatically validating Eloquent models for Laravel
MIT License
968 stars 76 forks source link

call to a member function until() on null #126

Closed leite closed 8 years ago

leite commented 9 years ago

Hi,

Nice lib, I'm having a trouble setting up validation in an eloquent model, my enviroment as follows:

core/Model.php

namespace core;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Model extends Eloquent {

  use \Watson\Validating\ValidatingTrait;

  function errors () { return $this->getErrors(); }
}

the so called model in models/Users.php

namespace models;

class Users extends core\Model {
  public $timestamps    = true;
  protected $primaryKey = 'pk_user';
  protected $table      = 'users';
  protected $hidden     = array('password');
  protected $fillable   = array('name', 'nick', 'email');
  protected $rules      = array(
    'name'  => 'required|alpha_num|max:40',
    'nick'  => 'required|max:20',
    'email' => 'required|max:40|email'
  );
}

this is how I set up eloquent in core/Bootstrap.php


/* ... */
$capsule = new Illuminate\Database\Capsule\Manager();
$capsule->addConnection($config);
$capsule->setEventDispatcher(
  new Illuminate\Events\Dispatcher(new Illuminate\Container\Container)
);
$capsule->setAsGlobal();
$capsule->bootEloquent();
/* ... */

this is the composer.json

{
  "minimum-stability": "dev",
  "require": {
    "slim/slim":             "2.6.2",
    "zordius/lightncandy":   "dev-master",
    "mustangostang/spyc":    "0.5.1",
    "illuminate/database":   "5.0.27",
    "watson/validating":     "~1.0",
    "illuminate/events":     "5.0.*"
  },
  "autoload": {
    "psr-4": {
      "" : [
        "app/", "app/models/", "app/core/"
      ]
    }
  }
}

the problem is - everytime a call the seeder a get this error:

PHP Fatal error:  Call to a member function until() on null in /home/leite/studies/slim/vendor/illuminate/support/Facades/Facade.php on line 213

db/seeds/2015_04_27_21192206_users_seed.php

class UsersSeed {
  function run () {
    Users::truncate();

    $bcrypt = new core\Bcrypt(13);
    $user   = new Users();

    $user->name     = 'admin';
    $user->nick     = 'admin';
    $user->email    = 'admin@pudim.com.br';
    $user->password = $bcrypt->hash('m3u4u41m');

    $user->save();
  }
}

for the record:

~ ☺ php -v
PHP 5.6.1 (cli) (built: Oct  5 2014 23:44:08) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
dwightwatson commented 8 years ago

This would be because the library uses Laravel facades to dispatch the model events. Without using the Laravel container (I believe) the calls on the facades aren't forwarded to the right objects.

We could take a look at trying to replace the Facades in the package, but I'm not sure how we would be able to do that in a simple way. Unfortunately I don't have a good solution for using this package outside of Laravel. Happy to accept a PR that fixes this but it's not an issue for most of the users. Sorry!

dwightwatson commented 8 years ago

Actually, there was some discussion about dropping facades in #132, so might be worthwhile keeping an eye on that thread.

Also sorry for taking so long to respond, I must have missed the notification for this issue.

karneaud commented 8 years ago

wonder what @leite did to overcome this hurdle....I too am using Slim PHP

karneaud commented 8 years ago

Just adding this here. I found this package https://github.com/itsgoingd/slim-services ....but not sure how to incorporate the validation into it to make it work...any idea how your package can be integrated here using this?

leite commented 8 years ago

nice @karneaud, I did nothing hehehe ... my app have validation on the front side and some double checks for uniqueness in the backend.

As you can see I am extending core\Model ... I guess you could create something like this and take advantage of slim-services

namespace core;

class Model extends Illuminate\Database\Eloquent\Model {

  private $_errors;

  function errors () {
    return $this->_errors;
  }

  static function boot () {

    parent::boot();

    static::saving(function ($model) {
      $validator = \Slim\Slim::getInstance()
        ->validator->make($model->attributes, $model->rules);
      if ($validator->fails()) {
        $model->_errors = $validator->errors();
        return false;
      }
      return true;
    });
  }

}

I would love to see the someone take some action about this (I'm kind busy at time), as to the author, please leave that issue open.

karneaud commented 8 years ago

I've been trying to see how I can adapt it but must admit my knowledge with facade pattern is young. Was not able to get it to work