Support the ongoing development of Laravel.io →
posted 10 years ago
Validation

Hi, I've been at this for a day, and I must be starting to confuse myself.

My issue is that I generate a view with a variable number of text boxes, based upon the number of years that were input in a previous form. Because most of the form elements are not fixed, I am also trying to generate rules and custom error messages dynamically in the controller.

The validation works OK on the fixed elements and output the correct messages. However, with the dynamic elements, if I deliberately enter a mistake on the form and submit it, the correct custom error messages will show up in the correct places, but I get a "Success" message flashed up instead of "Errors have been detected".

Not sure what is going on, but it seems like the error is being detected, but the validation seems to get by-passed and the rest of the operations in the controller are executed as normal.

It's got to be something very simple.

The code is below. Extract from the controller first, then the error code from the view.

		$pmt = Input::get('payment');

                $error = 0;

                        for($z = 0; $z < count($pmt); $z++) {
                        	$rules['payment.'.$z] = array("required", "min:0", "max:50000000", "numeric", "regex:/^[\d]{1,8}\.{0,1}[\d]{0,4}$/");
                        }
			
			$rules['payment_fixed'] = array("numeric", "regex:/^[\d]{1,8}\.{0,1}[\d]{0,4}$/");
			$rules['payment_percentage'] = array("numeric", "max:100", "regex:/^[\d]{1,8}\.{0,1}[\d]{0,4}$/");
		
			for($z = 0; $z < count($pmt); $z++) {
				$messages += array(

					'payment.'.$z.'.required' => 'The payments for year number '.($z+1).' must be entered',
					'payment.'.$z.'.min' => 'The minimum payment for year number '.($z+1).' is 0',
					'payment.'.$z.'.max' => 'The maximum payment for year number '.($z+1).' is 50000000',
					'payment.'.$z.'.numeric' => 'Only numeric values can be entered for for year number '.($z+1),
					'payment.'.$z.'.regex' => 'Values for year number '.($z+1).' must be entered as a monetary value'
				
				);
                        }



		$validation = Validator::make(Input::all(), $rules, $messages);
		
		if ($validation->fails())
                {
		
                Session::flash('message', 'Errors have been detected. Please review the page and re-validate.');


                } else {

		Session::flash('message', 'Payments Successfully Updated.');

		}

Extract from view

@if($errors->has('payment.'.$i)) 
{{ $errors->first('payment.'.$i, '<span class="error">:message</span>') }}
@endif

Thanks in advance for any suggestions. Neil.

Last updated 3 years ago.
0

I've been having trouble getting error messages back to individual iterations of the arrays and may break down and post about it here later but i believe i've had your problem before and i think it is due to where your validation is taking place in relation to your iterating through the arrays(your loops in the controller). what i did was dd(); on different values to determine when the app knows what about certain variables. that should help you find the flaw in the logic. sorry i can't be more specific.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.