Laravel 4 mandatory parameters error

▼魔方 西西 提交于 2019-12-24 16:02:28

问题


Hello im working with laravel 4 in a proyect and it´s not the first time but i have a problem that ive never had before.

Some mandatory parameters are missing ("key") to generate a URL for route "register".

I dont know how to fix it i've tryed everything i came up with but no results. These are my routes:

Route::get('register/{key}',array('before'=>'guest','as'=>'register','uses' => 'UserController@create'));
Route::post('register/{key}',array('before'=>'csrf','uses'=>'UserController@store'));

And my controller: class UserController extends BaseController {

public function getRegistro($key = null)
{
    $invites = Invite::lists('key');
    if (in_array($key, $invites)) {
        return View::make('home.register')
        ->with('title','Register')
        ->with('key',$key);
    }else{
         Notification::error('Not a valid key to register.');
        return Redirect::route('home');
    }
}

public function postStore()
{
    $validator = Validator::make(
        array(
            'username' => Input::get('username'),
            'password'=>Input::get('password'),
            'password_confirmation'=>Input::get('password_confirmation'),
            'email'=>Input::get('email'),
            'ubi'=>Input::get('ubi'),
            'bio'=>Input::get('bio'),
        ),
        array(
            'username' => 'required|unique:users|',
            'password'=> 'required|min:6|confirmed',
            'password_confirmation'=> 'required|min:6',
            'email'=> 'required|unique:users|email',
            'ubi'=> 'required|min:5',
            'bio'=>'required|min:10',
        )
    );
    if ($validator->fails())
    {
        Notification::error($validator->messages());
        return Redirect::route('register');
    }else{
        $user = new User;

        $user->username = Input::get( 'username' );
        $user->email = Input::get( 'email' );
        $user->password = Input::get( 'password' );
        $user->save();
    }

That problem happens when i send an empty form but when i put info for example my username as Bruce i get this error:

Method [validateBruce] does not exist.

Please help


回答1:


Your named route (register) has a required parameter ({key}) and when you're posting your form if it fails you are trying to redirect to that named route with no key.

return Redirect::route('register');

You need to supply a key to redirect to, example:

return Redirect::route('register', array('your-key'));

If this parameter is not required then you can place a question mark ? before the closing curly brace.

Route::get('register/{key?}', ...);

The parameter is now optional.

As for your second error, I have no idea. Are you using some sort of model validator that validates when you save? I'd try removing the extra pipe | from the username rules array.



来源:https://stackoverflow.com/questions/17628766/laravel-4-mandatory-parameters-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!