laravel ã¯ããã¾ãã
å ¬å¼ããã¥ã¡ã³ããè³¼å ¥ããã®ã¯å æã ã£ãããããããã¯ãããã
ã¯ããã¦æ¸ããã³ã¼ã
è¦ããè¦ã¾ãã§æ¸ãããã°ã¤ã³ãã©ã¼ã ãããã action_login 㨠action_dologin ã§åããã®ãviewã«æ¸¡ããã¨ã´ãã§ã´ãã§ãã¦ãã¾ããã
controllers/auth.php
<?php class Auth_Controller extends Base_Controller { public function action_login() { $input = array('username' => ''); return View::make('auth.login') ->with('input', $input) ->with('errors', new Laravel\Messages()); } public function action_dologin() { $input = Input::all(); $rules = array( 'username' => 'required', 'password' => 'required', ); $validation = Validator::make($input, $rules); if ($validation->fails()) { return View::make('auth.login') ->with('input', $input) ->with('errors', $validation->errors); } return "authed!"; } }
views/auth/login.blade.php
{{ Form::open('auth', 'post') }} {{ Form::text('username', $input['username']) }} @if ($errors->has('username')) {{ $errors->first('username') }} @endif {{ Form::password('password') }} @if ( $errors->has('password')) {{ $errors->first('password') }} @endif {{ Form::submit() }} {{ Form::close() }}
æ¸ãç´ããã³ã¼ã
ããã¥ã¡ã³ãã¨ãã½ã¼ã¹ã¨ãèªã¿ç´ãã¦ãæ¸ãç´ããã®ããããaction_login ããããªãã¼ã·ã§ã³å¤±ææã®ã³ã¼ãããªããªããã£ãããã¾ããã
controllers/auth.php
<?php class Auth_Controller extends Base_Controller { public function action_login() { return View::make('auth.login'); } public function action_dologin() { $input = Input::all(); $rules = array( 'username' => 'required', 'password' => 'required', ); $validation = Validator::make($input, $rules); if ($validation->fails()) { return Redirect::back() ->with_input() ->with_errors($validation); } return "authed!"; } }
views/auth/login.blade.php
{{ Form::open('auth', 'post') }} {{ Form::text('username', Input::old('username')) }} {{ $errors->first('username') }} {{ Form::password('password') }} {{ $errors->first('password') }} {{ Form::submit() }} {{ Form::close() }}
ããã£ããã¨
ããªãã¼ã·ã§ã³ã¨ã©ã¼ã§åã®ãã¼ã¸ã«æ»ãã¨ãã¯ãRedirect::back()
ããã㦠with_input(), with_errors() ã使ã
Input::old() ã§ååå
¥åããå¤ãå¾ããã
ãã®ããã« with_input() ãã¦ãã
view ã«ç©ºã® errors ã渡ãå¿
è¦ã¯ãªã
Viewコンストラクタ 㧠$this->date['errors'] = new Messages() ãã¦ããã
ã¨ã©ã¼ã¡ãã»ã¼ã¸ã®ãã¼ã¯ errors ã¨ããã®ã laravel ã®æµåã£ã½ã
ããªãã¼ã·ã§ã³ã¡ãã»ã¼ã¸è¡¨ç¤ºã¯ {{$errors->first('fieldname')}}
ã¨ã©ã¼ããªããã°ä½ãããªããã if ãããªã