简体   繁体   中英

How to use a validator in Laravel 5.0?

I am using laravel 5.0. I want using a laravel validator where my MENU_NAME column in 'dbo.MS_MENU' is unique. I am using sql server database. I have made a validator code like in the below, but I still got an error

QueryException in Connection.php line 624: SQLSTATE[23000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Violation of UNIQUE KEY constraint 'IX_MS_MENU'. Cannot insert duplicate key in object 'dbo.MS_MENU'. The duplicate key value is (Role). (SQL: EXEC dbo.M_INSERT_MENU_PARENT '0', '121', 'Role', 'Menu coba', '0', 'SDF')

    $validator = Validator::make($request->all(), [
        'MENU_NAME' => 'unique:dbo.MS_MENU']);

    if ($validator->fails()) {
        return redirect ('Menu')->withErrors($validator)->withInput();
    }

pls try with this one

$validator = Validator::make($request->all(), [
    'MENU_NAME' => 'unique:dbo.MS_MENU,MENU_NAME']);

You need to use table name and also column name

Hello check following code,

 //Validation using ajax    
public function checkvalidation(Request $request) {

    $data['SUCC_FLAG'] = 0;
    $formdata = $request->only('uname', 'umail', 'dob', 'cno', 'pwd', 'cpwd');
    $rules = ['uname' => 'required|min:2|alpha', 'umail' => 'required|email', 'dob' => 'required|date', 'cno' => 'required|digits:10', 'pwd' => 'required|min:3|max:6', 'cpwd' => 'required|same:pwd'];
    $validator = Validator::make($formdata, $rules);
    if ($validator->fails()) {
        $data['msg'] = $validator->errors();
    } else {
        $data['SUCC_FLAG'] = 1;
        $data['msg'] = "success";
    }
    return json_encode($data);
}

this is sample code i use for my project you have to change according to your use or project.

for use validator you have to include use Validator; in header

i hope this works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM