Josh Mountain
Josh Mountain

Reputation: 1950

Validating unknown input fields in Laravel

I am having trouble wrapping my head around how to validate a form I have created. The form I have looks something like this:

<input id="box-1-nickname" name="box-1-nickname" class="form-control" type="text" placeholder="Required">

<select id="box-1-destination" name="box-1-destination" class="form-control">
    <option value="store">Storage Facility</option>
    <option value="ship">Ship</option>
</select>

<input class="box-height form-control" id="box-1-height" name="box-1-height" type="number" placeholder="in Inches">
<input class="box-width form-control" id="box-1-width" name="box-1-width" type="number" placeholder="in Inches">
<input class="box-depth form-control" id="box-1-depth" name="box-1-depth" type="number" placeholder="in Inches">
<input class="box-weight form-control" id="box-1-weight" name="box-1-weight" type="number" placeholder="in Pounds">

<label class="radio-inline">
    <input id="box-1-size-retail" name="box-1-size" type="radio" value="retail" checked>
    Retail box (18" x 18" x 22")
</label>
<label class="radio-inline">
    <input id="box-1-size-custom" name="box-1-size" type="radio" value="custom">
    I'll use my own box
</label>

The part that complicates it a bit is the fact that a user can 'add a box' which will duplicate these form fields and increment the box ID by 1. After adding/removing a few boxes and submitting the form the Input::all() might come back looking something like this:

array (size=29)
  'box-1-nickname' => string 'Something' (length=9)
  'box-1-destination' => string 'store' (length=5)
  'box-1-height' => string '1' (length=1)
  'box-1-width' => string '2' (length=1)
  'box-1-depth' => string '3' (length=1)
  'box-1-weight' => string '4' (length=1)
  'box-1-size' => string 'retail' (length=9)
  'box-4-nickname' => string 'Another' (length=7)
  'box-4-destination' => string 'ship' (length=4)
  'box-4-height' => string '33' (length=2)
  'box-4-width' => string '1' (length=1)
  'box-4-depth' => string '22' (length=2)
  'box-4-weight' => string '33' (length=2)
  'box-4-size' => string 'custom' (length=6)
  'box-6-nickname' => string 'Stuff' (length=5)
  'box-6-destination' => string 'store' (length=5)
  'box-6-height' => string '34' (length=2)
  'box-6-width' => string '76' (length=2)
  'box-6-depth' => string '44' (length=2)
  'box-6-weight' => string '2' (length=1)
  'box-6-size' => string 'retail' (length=9)
  'box-8-nickname' => string 'Things and others' (length=17)
  'box-8-destination' => string 'ship' (length=4)
  'box-8-height' => string '5' (length=1)
  'box-8-width' => string '66' (length=2)
  'box-8-depth' => string '5' (length=1)
  'box-8-weight' => string '33' (length=2)
  'box-8-size' => string 'custom' (length=6)
  '_token' => string 'BIXSdz16ccJLaOmTxh2ShW5C16W1g0xmpJ10xnwC' (length=40)

I'm struggling to find a way to validate these box inputs since I don't really know how many box fields would be submitted or what their input names would be. Any suggestions would be very welcome.

Upvotes: 1

Views: 1023

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29278

Changing the names of your input variables might be the way to go here. Instead of having box-1-... and box-2-... etc, why not have names like this:

name="box-nickname[]"
name="box-destination[]"
name="box-height[]"
name="box-width[]"
name="box-depth[]"
name="box-weight[]"

If you had 2 of these boxes sent to the server, calling Input::all() would look something like this:

'box-nickname' => array(
  "0" => "Value of Box Nickname 1",
  "1" => "Value of Box Nickname 2",
  ...),
'box-destination' => array(
  "0" => "Value of Box Destination 1",
 ... 

You get the idea right? Validating this becomes simple, the rules only need to be applied to each array of values, and not every numerical box sent:

$rules = array(
  "box-nickname" => "Required|Max:6",
  "box-destination" => "Required|Numeric"
  ...
);

And you can change the rules to whatever you would like. The only difficult part to this is displaying validator messages on redirecting back to the page. Therefore, I would also recommend pairing this with client-side validation, which handles dynamically generated inputs better than Laravel can when using Redirect::to()->withInput()->withErrors($validator).

Hope that provided some insight to your issues!

Upvotes: 4

Related Questions