Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://www.gravityhelp.com/forums/topic/querying-external-dataset-to-validate-number-input
- // Chris Hajer September 12, 2012
- // dataset looks like this:
- /*
- SBCID;Church;Assn;Messengers
- 12345;Foo;Bar;2
- 67890;Bar;Foo;10
- */
- // validate SBC ID
- add_filter('gform_validation_3', 'sbc_validate_code');
- function sbc_validate_code($validation_result){
- // This calls the sbcid.csv file and sends it to the processor function above
- $csvdata = sbc_csv_in_array( "/home/filelocation/sbcid.csv", ";", "\"", true );
- if(!sbc_is_code_valid($csvdata, $_POST['input_1'])){
- $validation_result['is_valid'] = false;
- foreach($validation_result['form']['fields'] as &$field){
- // field 1 is the field where we want to show the validation message
- if($field['id'] == 1){
- $field['failed_validation'] = true;
- $field['validation_message'] = 'The SBC ID you entered is invalid: please try again.';
- break;
- }
- }
- }
- return $validation_result;
- }
- // This creates the array from the data file in the function below
- function sbc_csv_in_array($url,$delm=";",$encl="\"",$head=false) {
- $csvxrow = file($url); // ---- csv rows to array ----
- $csvxrow[0] = chop($csvxrow[0]);
- $csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
- $keydata = explode($delm,$csvxrow[0]);
- $keynumb = count($keydata);
- if ($head === true) {
- $anzdata = count($csvxrow);
- $z=0;
- for($x=1; $x<$anzdata; $x++) {
- $csvxrow[$x] = chop($csvxrow[$x]);
- $csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
- $csv_data[$x] = explode($delm,$csvxrow[$x]);
- $i=0;
- foreach($keydata as $key) {
- $out[$z][$key] = $csv_data[$x][$i];
- $i++;
- }
- $z++;
- }
- }
- else {
- $i=0;
- foreach($csvxrow as $item) {
- $item = chop($item);
- $item = str_replace($encl,'',$item);
- $csv_data = explode($delm,$item);
- for ($y=0; $y<$keynumb; $y++) {
- $out[$i][$y] = $csv_data[$y];
- }
- $i++;
- }
- }
- return $out;
- }
- // This searches the array to see if the code is valid and returns TRUE or FALSE
- function sbc_is_code_valid($csvdata, $thiscode) {
- foreach ($csvdata as $key => $value) {
- if ($thiscode == $value['SBCID'])
- return TRUE;
- else
- return FALSE;
- }
- }
- // doing this here because the redirect URL does not support variables or shortcodes
- add_filter('gform_confirmation_3', 'valid_sbcid_confirmation', 10, 4);
- function valid_sbcid_confirmation($confirmation, $form, $lead, $ajax){
- // customize this URL - I send the code in the query string
- $success_url = 'http://example.com/next-form/?sbcid=' . $lead[1];
- $confirmation = array('redirect' => $success_url);
- return $confirmation;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement