Jump to content

Validate huge fields


jkkenzie

Recommended Posts

I need to validate the POST fields below except a few hidden inputs like User_id & category: They are not huge but i would not like to write for each a line of code like if(empty($_popst['field'])) ...

How can simplify this by checking only if they are empty and display a message that lists all fields that were not filled?

 

array('user_id'=>$data['Id'], 'surname'=>$_POST['surname'], 'firstname'=>$_POST['firstname'], 'middlename'=>$_POST['middlename'], 'id_number'=>$_POST['id_number'], 'pin_number'=>$_POST['pin_number'], 'street'=>$_POST['street'], 'estate'=>$_POST['estate'], 'hse_number'=>$_POST['hse_number'], 'town'=>$_POST['town'], 'tele'=>$_POST['tele'], 'mobi'=>$_POST['mobi'], 'work_street'=>$_POST['work_street'], 'work_building'=>$_POST['work_building'], 'company'=>$_POST['company'], 'work_town'=>$_POST['work_town'], 'work_tele'=>$_POST['work_tele'], 'work_fax'=>$_POST['work_fax'], 'cont_surname'=>$_POST['cont_surname'], 'cont_firstname'=>$_POST['cont_firstname'], 'cont_middlename'=>$_POST['cont_middlename'], 'cont_street'=>$_POST['cont_street'], 'cont_building'=>$_POST['cont_building'], 'cont_company'=>$_POST['cont_company'], 'cont_home_tele'=>$_POST['cont_home_tele'], 'cont_office_tele'=>$_POST['cont_office_tele'], 'cont_mobi'=>$_POST['cont_mobi']);

 

Link to comment
https://forums.phpfreaks.com/topic/247361-validate-huge-fields/
Share on other sites

I'd make two arrays:

$non_required_fields = array( 'user_id'=>$data['Id'] );
$required_fields = array(
	'surname'=>$_POST['surname'],
	'firstname'=>$_POST['firstname'], 
	'middlename'=>$_POST['middlename'],
	'id_number'=>$_POST['id_number'], 
	'pin_number'=>$_POST['pin_number'], 
	'street'=>$_POST['street'], 
	'estate'=>$_POST['estate'], 
	'hse_number'=>$_POST['hse_number'], 
	'town'=>$_POST['town'], 
	'tele'=>$_POST['tele'], 
	'mobi'=>$_POST['mobi'], 
	'work_street'=>$_POST['work_street'], 
	'work_building'=>$_POST['work_building'], 
	'company'=>$_POST['company'], 
	'work_town'=>$_POST['work_town'], 
	'work_tele'=>$_POST['work_tele'], 
	'work_fax'=>$_POST['work_fax'], 
	'cont_surname'=>$_POST['cont_surname'], 
	'cont_firstname'=>$_POST['cont_firstname'], 
	'cont_middlename'=>$_POST['cont_middlename'], 
	'cont_street'=>$_POST['cont_street'], 
	'cont_building'=>$_POST['cont_building'], 
	'cont_company'=>$_POST['cont_company'], 
	'cont_home_tele'=>$_POST['cont_home_tele'], 
	'cont_office_tele'=>$_POST['cont_office_tele'], 
	'cont_mobi'=>$_POST['cont_mobi']
);

foreach ( $required_fields as $key => $fields )
{
if ( isset( $_POST[$key] ) )
{
	continue;
}
else
{
	die("$key needs to be field out.");
}
}

$array = array_merge($non_required_fields, $required_fields ); // got them back together.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.