【问题标题】:Laravel 4 error: Call to a member function passes() on a non-objectLaravel 4错误:在非对象上调用成员函数传递()
【发布时间】:2013-08-09 17:35:05
【问题描述】:

在 Laravel 4 中使用 Route::post 时遇到问题。

这是我的 User.php(laravel 模型)代码:

class User extends Eloquent implements UserInterface, RemindableInterface {

  public static function validate($input)
  {
    $rules = array(
    'email' => 'Required|Between:3,64|Email|Unique:users',
    'password' => 'Required|AlphaNum|Between:4,8|Confirmed',
    'password_confirmation' => 'Required|AlphaNum|Between:4,8'
    );

    $v = Validator::make($input, $rules);
  }
}

这是我的 routes.php 代码:

Route::post('register', function()
{
  $v = User::validate(Input::all());

      if ($v->passes()){
      $u = new User();
      $u->email = Input::get('email');
      $u->password = Hash::make(Input::get('password'));
      $u->save();
      Auth::login($u);

      return Redirect::to('createprofile');
    }
    else{
      return Redirect::to('register')->withErrors($v->getMessageBag());
    }
});

这是我的 register_user.blade.php 代码:

@section('content')
    {{ Form::open(array('url' => '/register', 'method' => 'post')) }}
    {{ Form::text('email') }}
    {{ Form::label('email', 'Your Email') }}</br>
    {{ Form::password('password'); }}
    {{ Form::label('password', 'Your Password') }}</br>
    {{ Form::password('password_confirmation'); }}
    {{ Form::label('password_confirmation', 'Confirm Your Password') }}</br>
    {{ Form::submit('Go') }}
  {{ Form::close() }}
@stop

问题似乎是当表单提交到 Route::post 时它无法识别

$v = User::validate(Input::all()) 

作为一个有效的对象,而不是让我调用一个非对象的成员函数 pass()。

var_dump($v)

等于 null。

有人知道这里的问题是什么吗? User::validate() 是从 User 模型调用函数的正确方法吗?

【问题讨论】:

    标签: laravel laravel-4 member-functions


    【解决方案1】:

    您忘记返回您的验证器实例;

    class User extends Eloquent implements UserInterface, RemindableInterface {
    
      public static function validate($input)
      {
        $rules = array(
        'email' => 'Required|Between:3,64|Email|Unique:users',
        'password' => 'Required|AlphaNum|Between:4,8|Confirmed',
        'password_confirmation' => 'Required|AlphaNum|Between:4,8'
        );
    
        return Validator::make($input, $rules);
      }
    }
    

    【讨论】:

    • 谢谢伙计。实际上,我刚刚意识到这一点,并想说它已经解决了,但无论如何感谢您的快速回复;)
    • 请查看this,需要帮助。
    猜你喜欢
    • 2016-11-20
    • 2015-02-11
    • 2015-09-15
    • 2017-07-05
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多