Validate Certain Feilds that Are Filled

Hi, I have a script that echo’s out products rows to be filled as part of an order system. By default there are six products rows but more can be added through the URL if necessary.

I want to check that if some of the products rows are filled, then to all of the fields in that row are filled. I don’t want it however to check products rows that are blank as they’re not needed.

I hope I’ve explained well enough :slight_smile:

<?php foreach (range(1,$items)as $num){ ?>
<!-- BOF Product Info -->
<div class="productInfo">

<div class="prodContain">
<span class="fontField bold">Quantity</span>
<input type="text" name="products[<?=$num?>][quantity]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['quantity'] ?>" class="prodName" style="width: 50px;" />
</div>

<div class="prodContain">
<span class="fontField bold">Product Description</span>
<input type="text" name="products[<?=$num?>][name]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['name'] ?>" class="prodName" style="width: 280px" />
</div>

<div class="prodContain">
<span class="fontField bold">Serial Number</span>
<input type="text" name="products[<?=$num?>][serial]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['serial'] ?>" class="prodName" style="width: 110px" />
</div>

<div class="prodContain">
<span class="fontField bold">Stock Number</span>
<input type="text" name="products[<?=$num?>][stock]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['stock'] ?>" class="prodName" style="width: 110px" />
</div>

<div class="prodContain">
<span class="fontField bold">Item Price</span>
<input type="text" name="products[<?=$num?>][price]" <?=$disabled?> value="<?php echo $_POST['products'][$num]['price'] ?>" class="prodName" style="width: 70px;" />
</div>
<!-- BOF New/Used -->
<div style="float: left; width: 650px; margin-top: 5px; color: #444444;"><b>Product Condition</b> 
<input type="radio" name="products[<?=$num?>][cond]" value="New" <?php if ($_POST['products'][$num]['cond']=="New") echo "checked"; ?> /> New
<input type="radio" name="products[<?=$num?>][cond]" value="Used" <?php if ($_POST['products'][$num]['cond']=="Used") echo "checked"; ?> /> Used
</div>
<!-- EOF New/Used -->
</div>
<!-- EOF Product Info -->
<?php } ?>

Not enough for this dim Geordie I’m afraid. :stuck_out_tongue:

Can you elaborate on the underlined points above? The first doesn’t seem to read right to me, and how do you know they’re ‘not needed’. ?

Hey bud, apologies I didn’t explain well, was in a bit of a rush to shoot off to work. I’ve attached a picture below which I think should illustrate what it is I’m trying to do. I’m wanting to validate only a field ‘block’ that has been filled (two have been filled in my example). Any completely empty blocks I want it to ignore them. Hope this helps :slight_smile:

You could use a callback and array filter. In this example, the filter_product function returns a boolean to indicate whether or not the array item should remain. The function could do anything really, maybe check if $product[‘price’] was over £1 for example.

This example simply checks to see if any of the array items are empty, if so, remove the product.


function filter_product($product){
  foreach($product as $key => $value){
    if(true === empty($product[$key])){
      return false;
    }
  }
  return true;
}

$products = array(
  array(
    'name'  => 'foo',
    'price' => 45.99
  ),
  array(
    'name'  => 'bar',
    'price' => null
  ),
  array(
    'name'  => '',
    'price' => 9.99
  ),
  array(
    'name'  => 'ying',
    'price' => 5.99
  ),
  array(
    'name'  => 'yang',
    'price' => 0
  ),
);

$products = array_filter(
  $products,
  'filter_product'
);

var_dump(
  $products
);

/*
  array(2) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "foo"
      ["price"]=>
      float(45.99)
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(4) "ying"
      ["price"]=>
      float(5.99)
    }
  }
*/

Hi bud, that’s perfect, just what I was after. That will prove very useful. Thanks again dude :smiley:

Hey bud, just a quick one to add to this, how could I check if each field in a product row has been filled? In my above example I’ve two products filled but say the user forgot to enter ‘Name’ how I could I return a message to say ‘Missing information’?

The provided solution would not be able to do this, it simply filters. Following the same theme though, you could use array_walk and a callback.


<?php
function validate_product(& $product){
  foreach($product as $key => $value){
    if(true === empty($product[$key])){
      $product['errors'][] = ucfirst($key) . ' is required.';
    }
  }
}

$products = array(
  array(
    'name'  => 'foo',
    'price' => 45.99
  ),
  array(
    'name'  => 'bar',
    'price' => null
  ),
  array(
    'name'  => '',
    'price' => 9.99
  ),
  array(
    'name'  => 'ying',
    'price' => 5.99
  ),
  array(
    'name'  => 'yang',
    'price' => 0
  ),
);

array_walk(
  $products,
  'validate_product'
);

var_dump(
  $products
);

/*
  array(5) {
    [0]=>
    array(2) {
      ["name"]=>
      string(3) "foo"
      ["price"]=>
      float(45.99)
    }
    [1]=>
    array(3) {
      ["name"]=>
      string(3) "bar"
      ["price"]=>
      NULL
      ["errors"]=>
      array(1) {
        [0]=>
        string(18) "Price is required."
      }
    }
    [2]=>
    array(3) {
      ["name"]=>
      string(0) ""
      ["price"]=>
      float(9.99)
      ["errors"]=>
      array(1) {
        [0]=>
        string(17) "Name is required."
      }
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(4) "ying"
      ["price"]=>
      float(5.99)
    }
    [4]=>
    array(3) {
      ["name"]=>
      string(4) "yang"
      ["price"]=>
      int(0)
      ["errors"]=>
      array(1) {
        [0]=>
        string(18) "Price is required."
      }
    }
  }
*/

Hey bud, that’s just the sort of thing I was after. Thanks for taking the trouble to help :slight_smile: