php - Making/forcing the Validation to fail by adding custom error Not Working in Laravel -


i trying make validation fail. have tried following:

usercontroller.php

public function add_user(request $request) { $messsages = array(     'email.unique' => 'email exists',     'email.required' => 'email missing',     'valid_from.date_format' => 'valid date invalid. mm/dd/yyyy e.g. 12/13/2014',     'valid_to.date_format' => 'valid date invalid. mm/dd/yyyy e.g. 12/13/2014',     'valid_to.required' => 'valid date missing',     'cnic.min' => 'cnic 15 characters long. not forget hyphens 35212-xxxxxxx-x',     'cnic.max' => 'cnic 15 characters long. not forget hyphens 35212-xxxxxxx-x',     'cnic.regex' => 'cnic include hyphens 35212-xxxxxxx-x' );  $rules = array(     'name' => 'required',     'email' => 'required|email|unique:users',     'valid_from' => 'required|date_format:m/d/y',     'valid_to' => 'required|date_format:m/d/y',     'phone' => 'numeric',     'cnic' => [     'min:15',     'max:15',     'regex:/\d{5}-\d{7}-\d{1}/'     ] );  $validator = validator::make($request->all(), $rules, $messsages);  //check year $y_from = substr($request->valid_from, -4); $y_from = (int)$y_from; $y_to = substr($request->valid_to, -4); $y_to = (int)$y_to; if($y_from >= 2038 || $y_to >= 2038) //here add custom error {     $validator->getmessagebag()->add('maxyear', 'invalid date. maximum value of year = 2038');     //$validator->errors()->add('maxyear', 'invalid date. maximum value of year = 2038'); //doesnt work above }  if ($validator->fails()) { //doesnt work     $messages = $validator->messages();     return redirect::route('useradd')->witherrors($messages); }  $user = new user; $user->name = $request->name; /* save other model data */  $user->save(); session::flash('usercreated', 'user created successfully');  return redirect('/pending_users'); } 

the $validator->fails() goes true somehow , validation not fail..

i have tried solution like:

if($y_from >= 2038 || $y_to >= 2038) {     //$validator->getmessagebag()->add('maxyear', 'invalid value year in date. max year = 2038');     //$validator->errors()->add('maxyear', 'invalid date. maximum value of year = 2038');     $rules['unreal_input'] = 'required';      $messages['unreal_input.required'] = 'invalid date. maximum value of year = 2038'; }  $validator = validator::make($request->all(), $rules, $messsages); 

it ugly in view gives default error "unreal_input required field". help!!

update

it typo used $messsages 3 s

you changes add $rules['unreal_input'] , $messages['unreal_input.required']

however having typo, see $messsages vs $messages. in 1 case there 3x "s" other case there 2x different array.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -