Jump to content

laravel form validation


gongpex

Recommended Posts

Hello everyone,

 

long time no see, I hope all you are fine,

 

I tried to create laravel form validation using this code :

 

on : app\http\Requests\ValidationRequest.php

<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Input;
use App\Http\Requests\Request;
use DB;

class ValidationRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
	    $username = Input::get('username');
		$password = Input::get('password');
		$tables = DB::table('admin')->where('password',$password)->where('username',$username)->count();
	
		if(count($errors) > 0) {
                   return [
                         'username' => 'required|min:3',
			 'password' => 'required',		 
                   ];
		} else if($tables == 0){
		   return ['db' => 'dberr'];
		}

    }
	
	public function messages()
{
    return [
        'username.required' => 'Username is required',
		'username.min' => 'Username at least 3 character length',
		'password.required'  => 'Password is required',
		'db.dberr' => 'Username or Password is wrong',
    ];
}
}

this my routes :

Route::get('post', 'AdminController@index');
Route::post('signin', 'AdminController@store');

Route::get('post/home', 'AdminController@home');
Route::get('post/addcategory', 'AdminController@addcategory');

this is my controller : (AdminController.php)

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\ValidationRequest;
use App\Http\Controllers\Controller;
use DB;

  class AdminController extends Controller {
	 
    public function index()
    {
        return view('admin.admin',['db'=>null]);
    }

	
	/* laravel message and form validation */
	public function rules() {}
	public function messages() {}
	
	public function store(ValidationRequest $request){
	    
		//echo $tables;
	}
	
  }
  
  
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

and this is my view :

<body>
  
    <form method="post" action="{{URL::to('signin')}}">
		   <div class="f_wrapper">
		    {{ $errors->first('db') }}
		    <div class="admin_panel">Store login</div>
			 <div> </div>
		     <div class="f_col">Admin Username</div><div class="f_col"><input name="username" type="text" class="inbox"></div>
			 <div class="f_col_w"><span class="number">{{ $errors->first('username') }}</span></div>
			 
			 <div class="f_col">Password</div><div class="f_col"><input name="password" type="password" class="inbox"></div>
			 <div class="f_col_w"><span class="number">{{ $errors->first('password') }}</span></div>
			 
			 <div class="f_col"> </div><div class="f_col"><a><input type="submit" value="Sign In"></a></div>
             <div class="f_col_w" style="height:10px;"> </div>
		   </div>   
    </form>

</body>

I had tried to make login form using code on above but it's didn't works,

 

please someone help me

 

Thanks

Edited by gong
Link to comment
Share on other sites

ugh sorry yesterday I was drunk when post on this forum,

 

alright this is about how to make custom validator on laravel.

 

I had renew my code after search on google :

 

on controller (AdminController.php)


namespace App\Http\Controllers;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\CreateUserRequest;
use App\Http\Controllers\Controller;
use DB;

public function postForm(CreateUserRequest $request) { return 'Success!'; }

on routes :

Route::post('signin', 'AdminController@postForm');

and on view :

 <form method="post" action="{{URL::to('signin')}}">
      <div class="f_wrapper">
	 @if ($errors->any())
	    <ul>{!! implode('', $errors->all('<li style="color:red">:message</li>')) !!}</ul>
	 @endif
		    
      <div class="f_col">Admin Username</div><div class="f_col"><input name="username" type="text" class="inbox"></div>
      <div class="f_col_w"><span class="number">{{ $errors->first('username') }}</span></div>
</form>

and this is on appServiceProvider.php :

<?php

namespace App\Providers;

use Validator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
       Validator::extend('foo', function($attribute, $value, $parameters) {
            return substr($value, 0, 3) == '+44';
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

and this is on http request :

<?php

namespace App\Http\Requests;

use Illuminate\Support\Facades\Input;
use App\Http\Requests\Request;
use DB;

class CreateUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
	    return [
	      'username' => 'required|foo',
	    ];
    }

}

This custom validator expect if you put number on input field it's should be start +44,

 

here is the error : if I put field empty, the error message will be shown --this is ok (use laravel default validator),

but if I input random number not start with +44, it will cause error like this :

 

BadMethodCallException in Validator.php line 2694:
Method [validateFoo] does not exist.
Please someone help me
thanks
Note : please ignore post that I've made before, because I made it when I was drunk.....
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...