dongliang9682 2014-06-30 13:35
浏览 36
已采纳

验证失败时会抛出错误:尝试获取非对象的属性

let me explain a bit about my problem. I have a form, where you can edit a user data and i have a validation as a service, but when the validation fails it throws me an error: "Trying to get property of non object" in userEdit.blade.php

Here's my routes.php:

Route::get('users/{id}/edit', array('before' => 'auth|admin', 'uses' => 'UserController@editUser'));
Route::post('users/{id}/edit', array('before' => 'auth|admin', 'uses' => 'UserController@editUserPost'));

UserController.php:

public function editUser($id) {
    $user = $this->user->find($id);
    return View::make('auth.editUser')->with('user', $user);
}

public function editUserPost() {
    // Validation
    $validator = new UserEditFormValidator(Input::all());

    if ($validator->fails()) { 
        return Redirect::action('UserController@editUser')->withErrors($validator->errors()); // THIS BUGGED
    }

    $input = array(
        'id' => Input::get('id'),
        'name' => Input::get('name'),
        'active' => (Input::has('active')) ? true : false,
        'password' => Input::get('password')
    );

    $this->user->update($input);
        return Redirect::action('UserController@userList')->with('success', 'Your profile has been updated');
    }

And editUser.blade.php:

{{ Form::open(array('url' => (Request::is('users/*')) ? 'users/' . $user->id . '/edit' : 'my/edit')) }}
        {{ Form::hidden('id', $user->id) }}

            <div class="form-group">
                {{ Form::label('name', 'Your name') }}
                {{ Form::text('name', $user->name, array('class' => 'form-control')) }}
            </div>

            <div class="form-group">
                {{ Form::label('password', 'Your password') }}
                {{ Form::password('password', array('class' => 'form-control')) }}
            </div>      
            <div class="form-group">
                {{ Form::label('password_confirmation', 'Your password one more time') }}
                {{ Form::password('password_confirmation', array('class' => 'form-control')) }}
            </div>

            {{ Form::submit('Save', array('class' => 'btn btn-default')) }}

{{ Form::close() }}

Everything looks fine for me, but i can't understand why i am getting "Trying to get property of non-object" in userEdit.blade.php because of $user->id

  • 写回答

1条回答 默认 最新

  • douqiandai4327 2014-06-30 14:12
    关注

    It doesn't look like you're sending the user_id back to the page when it encounters errors.

    The redirect should look like this return Redirect::action('UserController@editUser', array('id' => 1))->withErrors($validator->errors());

    The route defined needs the user id to initialize the user to show in the View.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度