View Single Post
Old 12-05-2012, 09:25 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Using the filter_vars from PHP5.2+ you can compact what you are doing into a simpler to modify validation process.
Here's an example of what (should) work for you based on most of what you have here. Some things didn't make sense like the year, but otherwise I pretty much just copied over the regexp as you have provided.
PHP Code:
<?php

if (count($_POST) <= 0)
{
    exit();
}

$aFilters = array(
    
'first_name' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z- ]{3,20}$/i'),
    ),
    
'last_name' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z- ]{3,20}$/i'),
    ),
    
'phone' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[0-9-]{7,20}$/i'),
    ),
    
'email' => FILTER_VALIDATE_EMAIL,
    
'year1' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1900'max_range' => 2100),
    ),
    
'make1' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z0-9 -]{2,25}$/i'),
    ),
    
'model1' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z0-9 -]{2,25}$/i'),
    ),
    
'vehicle_type_id1' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1),
    ),
    
'pickup_city' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z0-9 -]{3,20}$/i'),
    ),
    
'pickup_state_code' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1),
    ),
    
'dropoff_city' => array(
        
'filter' => FILTER_VALIDATE_REGEXP,
        
'options' => array('regexp' => '/^[a-z0-9 -]{3,20}$/i'),
    ),
    
'dropoff_state_code' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1),
    ),
    
'vehicle_runs' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1),
    ),
    
'ship_via_id' => array(
        
'filter' => FILTER_VALIDATE_INT,
        
'options' => array('min_range' => 1),
    ),
    
'estimated_ship_date' => array(
        
'filter' => FILTER_CALLBACK,
        
'options' => 'strtotime',
    )
);


$aFailureNotices = array (
  
'first_name' => 'Invalid First Name',
  
'last_name' => 'Invalid Last Name',
  
'phone' => 'Invalid Phone Number',
  
'email' => 'Invalid Email Address',
  
'year1' => 'Invalid Year',
  
'make1' => 'Invalid Make',
  
'model1' => 'Invalid Model',
  
'vehicle_type_id1' => 'Invalid Vehicle Type ID',
  
'pickup_city' => 'Invalid Pickup City',
  
'pickup_state_code' => 'Invalid Pickup State Code',
  
'dropoff_city' => 'Invalid Dropoff City',
  
'dropoff_state_code' => 'Invalid Dropoff State Code',
  
'vehicle_runs' => 'Invalid Vehicle Runs option',
  
'ship_via_id' => 'Invalid Ship Via ID',
  
'estimated_ship_date' => 'Invalid Estimate Ship Date',
);


// Do the work now.
// Filter the input array
$filtered filter_input_array(INPUT_POST$aFilters);

function 
isEmpty($s)
{
    return empty(
$s);
}

// Pick out the empty ones (null is actually not provided, false is failed)
$aBadEntries array_filter($filtered'isEmpty');

// Get a collection of error messages
$aErrorMessages array_intersect_key($aFailureNotices$aBadEntries);
$aErrorMessages will contain key => messages of the failures. Give that a try.
Note that I haven't made extensive use of the filter functionality myself; I still have an old school validation library I wrote years ago for this purpose, and haven't had a chance to update it.

Edit:
Oh btw, there's nothing that says you cannot compact down the validation even further. If you have a ruleset that's generic (such as for names and cities which will likely contain the same characters realistically), you can create that string separately and simply use the variable as the options. There are always ways to trim it down.

Last edited by Fou-Lu; 12-05-2012 at 09:28 PM..
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
otty (12-06-2012)