I would use the Validation class which you can read up about here: http://laravel.com/docs/4.2/validation
$validator = Validator::make(
array(
'name' => 'Dayle',
'password' => 'lamepassword',
'email' => '[email protected]'
),
array(
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
)
);
First array is inputs and second is rules.
so far I have the following
$percents = Input::get('percent');
$descriptions = Input::get('description');
$rules = array(
'percent' => 'required'
'description' => 'required'
);
$validator = Validator::make(
array(
'percent' => Input::get('percent'),
'description' => Input::get('description'),
),
array(
'percent' => 'required',
'description' => 'required',
)
);
if ($validator->fails())
{
// handle the failure here!
}
Hope this helps!
percents = Input::get('percent');
descriptions = Input::get('description');
foreach ($percents as $key => $percent) {
$fields['percent'.$key] = $percent;
$rules['percent'.$key] = 'required|numeric|between:0,100';
}
$validator = Validator::make($fields, $rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
and descriptions validations????
Worked as follows, but would like to improve the code
//Form
{{ Form::text("percents[{$key}]") }}
{{ Form::textarea("descriptions[{$key}]") }}
//Controller
$percents = Input::get('percents');
$descriptions = Input::get('descriptions');
foreach ($porcentajes as $key => $porcentaje)
{
$fields['percents'.$key] = $porcentaje;
$fields['descriptions'.$key] = $descriptions[$key] ;
$rules['percents'.$key] = 'required|numeric|between:0,100';
$rules['descriptions'.$key] = 'required';
}
$validator = Validator::make($fields, $rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
//View
{{ str_replace('percents'.$key, 'Percent', $errors->first('percents'.$key, '<p class="text-danger">:message</p>')) }}
{{ str_replace('descriptions'.$key, 'Description', $errors->first('descriptions'.$key, '<p class="text-danger">:message</p>')) }}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community