[Laravel] Validator / Custom Validator Password

Post Reply
tthlan
Quản trị viên
Posts: 76
Joined: Tue Aug 23, 2016 8:13 am

[Laravel] Validator / Custom Validator Password

Post by tthlan »

Validator / Custom Validator Password with Laravel

Image
use Auth;
use DB;
use View;
use Validator;

use Illuminate\Support\Facades\Hash;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Auth\Passwords\PasswordBroker;

Class UserControl extend Control

function postchangePassword

Code: Select all

public function postchangePassword(Request $request)
	{		
		$validator = $this->admin_credential_rules($request_data);
		if ($validator->fails()) {

			$error = [];

			foreach ($validator->getMessageBag()->toArray() as $key => $value) {

				array_push($error, $value[0]);
			}

			return View::make('auth/passwords/changepassword', array(		
				'error' => $error,
				'message' => $message,
			));
		} else {
			$current_password = Auth::User()->password;
			if (Hash::check($request_data['current-password'], $current_password)) {
				$user_id = Auth::User()->id;
				$obj_user = User::find($user_id);
				$obj_user->password = Hash::make($request_data['password']);;
				$obj_user->save();

				$message = 'Update password successful!';

				return View::make('auth/passwords/changepassword', array(				
					'error' => $error,
					'message' => $message,
				));
			} else {
				$error = array('Please enter correct current password');

				return View::make('auth/passwords/changepassword', array(					
					'error' => $error,
					'message' => $message,
				));
			}
		}
	}
function admin_credential_rules

Code: Select all

/**
	 * Rule validate for change password
	 *
	 * @param $data
	 *
	 * @return Validator
	 */
	protected function admin_credential_rules(array $data)
	{
		Validator::extend('specialCheck', function ($field, $value, $parameters) {

			$anyUppercase = (preg_match('/\p{Lu}/u', $value) != 0);
			$anyDigit = (preg_match('/\d/u', $value) != 0);
			$anySpecial = (preg_match('/[^a-zA-Z\d]/', $value) != 0);

			return ($anyUppercase && $anyDigit && $anySpecial);
		});

		$messages = [
			'current-password.required' => 'Please enter current password',
			'password.required' => 'Please enter password',
			'password.min' => 'The password length required minimum of 8 characters',
			'special_check' => 'Please enter with contain A-a, at least with one special character, one number'
			//'password_confirmation.min' => 'Please enter re-password minium length is 6 characters',
		];

		$rules = [
			'current-password' => 'required',
			'password' => 'required|string|min:8|same:password',
			'password_confirmation' => 'required|string|same:password',
			'password' => 'specialCheck'
		];

		$validator = Validator::make($data, $rules, $messages);

		return $validator;
	}
Post Reply