lundi 5 décembre 2016

Laravel 5.1. Indexed array's validation

I have input form, where user choose the quantity of item's details for his preorder. Ex: Julia's preorder : drink's - water 1 bottle, milk 1 glass, bulletproof coffee 1 cup.

@foreach ($item->detail()->orderBy('sequence')->get() as $detail)
<td><input name="estimate_quantity[]"></td>
@endforeach

I want to validate my quantity, only integers greater than zero or equal to it. So I made rule

public function rules()
{
      $rules = [
          'estimate_quantity' => 'required|array',
      ];
      $estimate_quantity = $this->request->get('estimate_quantity');
      foreach ($estimate_quantity as $index => $value){
          $rules["estimate_quantity.$index"] = 'integer|min:0';
      }
      return $rules;
}

It doesn't work. If I entered alpha character, it will be an error

ErrorException in helpers.php line 468:
htmlentities() expects parameter 1 to be string, array given

1.What is the right way to do this validation? Making it in controller doesn't look's nice.

2.If i will make my custom rule in separate file, where better to store it? Create app/Validations folder?

3.What magic happening after rules execution and before controller method's execution?

I'm pretty new in Laravel and programming as well, sorry for this easy questions.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire