I have a form that I validate in a controller.
I use messageBag because there is a complicated validation I need to do.
I need to do some calculations and give a list of errors.
The problem is, I don't get the old('value') of the form fields when I use that method.
My code in the controller:
$errors = new MessageBag();
$validator = Validator::make(Input::all(), [
"blended" => "required|numeric|min:0.1",
],
[
'blended.required' => 'Please enter a valid weight',
'blended.min' => 'Please enter the weight you blended',
'blended.numeric' => 'Invalid weight blended',
]);
if( $validator->fails() )
{
$errors->add('blended', 'Please enter a valid weight');
alert()->overlay('Error!', 'Errors: Wrong weight was given', 'error');
return Redirect::route('roast.index');
}
$blend_id = $request->blend_id;
$blend_weight = $request->blended * 1000;
// Blend - Get coffees and their weights for the blend
$blend_coffees = $this->getBlendCoffeesWeight($blend_id, $blend_weight);
foreach($blend_coffees as $cof) {
$have = 'none';
$coffee_id = $cof['coffee_id'];
$roasted = $this->getTotalRoasted($coffee_id);
$packed = $this->getTotalCoffeePacked($coffee_id);
$blended = $this->getTotalCoffeeBlended($coffee_id);
$total_weight_packed =
$shop_usage = $this->getTotalShopUsage($coffee_id);
$weight_left = $roasted - $packed['total'] - $blended - $shop_usage;
//Not enough to pack and not Blend Anyway
if($weight_left < $cof['weight'] && $request->blend_anyway == 0) {
$coffee_name = Coffee::find($coffee_id);
$coffee_name = $coffee_name->name;
$need = $this->convertWeight($cof['weight']);
$have = $this->convertWeight($weight_left);
$errors->add('weight', "You need: $need of $coffee_name but you have: $have");
} else {
//Creating an array for the table updates later
$blends[] = array('coffee_id'=>$coffee_id, 'blended'=>$blend_coffees[$coffee_id]['weight']);
}
}
if($errors->any()) {
alert()->error('Error!', 'You cannot blend please check the errors');
return Redirect::route('roast.index')->withErrors($errors);
How can I get the old values for the fields in the form?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire