| 1 |
<li> |
| 2 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_validate_submission','validate_field_submission',10,3); |
| 3 |
function validate_field_submission($validation_errors, $submission, $cf7_key){ |
| 4 |
/* $submission an array of <field-name>=>$value pairs one for each submitted field. |
| 5 |
tabbed/tabled sections fields have arrays as $values. |
| 6 |
tables within tabs have aray of array as values. |
| 7 |
if a value is not valid, return a <field-name>=><error message string> pair in the $validation_errors array. The sbumission process will be cancelled and teh user required to correct the field before re-submitting. |
| 8 |
$cf7_key unique form key to identify your form, $cf7_id is its post_id. |
| 9 |
*/ |
| 10 |
if('{$form_key}'==$cf7_key ){ |
| 11 |
//$validation_errors is an array of field-names=>error messages. |
| 12 |
//these include the simple validation exposed in the CF7 plugin for required fields/special field formats. |
| 13 |
if(isset($validation_errors['location-city']) && $submission['location-city'] === 'Chennai'){ |
| 14 |
$validation_errors['location-city'] = 'location cannot be Chennai!'; |
| 15 |
} |
| 16 |
//for fields within tables, these are stored as arrays, one for each row. |
| 17 |
foreach($validation_errors['my-table-field1'] as $row_index=>$error){ |
| 18 |
if(isset($submission['my-table-field1'][$row_index]) && $submission['my-table-field1'][$row_index]>5){ |
| 19 |
$validation_errors['my-table-field1'][$row_index] = 'value should be less than 5'; |
| 20 |
} |
| 21 |
} |
| 22 |
//for fields in tables that are within tab sections, these are stored as 2-dimensional arrays. |
| 23 |
foreach($validation_errors['my-table-field1'] as $tab_index=>$e_array){ |
| 24 |
foreach($e_array as $row_index => $error){ |
| 25 |
if(isset($submission['my-table-field1'][$tab_index][$row_index]) && $submission['my-table-field1'][$tab_index][$row_index]>5){ |
| 26 |
$validation_errors['my-table-field1'][$tab_index][$row_index] = 'value should be less than 5'; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
return $validation_errors; |
| 32 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('custom form submission validation of any field.','cf7-grid-layout')?> |
| 33 |
</li> |
| 34 |
<li> |
| 35 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_annotate_mail_attach_grid_files','annotate_mail_attachments',10,6); |
| 36 |
/** |
| 37 |
* @param string $label an empty text to filter. |
| 38 |
* @param string $field the name of the file field being attached |
| 39 |
* @param string $file_name file name attached. |
| 40 |
* @param string $row the row index, empty if first row and zero-based otherwise. Null if not a row field type. |
| 41 |
* @param string $tab the tab index, empty if first tab and zero-based otherwise. Null if not a tab field type. |
| 42 |
* @param string $cf7_key unique form key. |
| 43 |
* @return string an annotation note to be appended at the end of your mail body. |
| 44 |
*/ |
| 45 |
function annotate_mail_attachments($label, $field, $file_name, $tab, $row, $cf7_key){ |
| 46 |
/* this filter is used to annotate complex file field submissions such as tables or tabs or tables within tabs. The index of the attachment in the mail is given to better annotate your attachments. Annotations are appended at the end of the mail body and you should take care to add newline/html breaks for your own clarity. The row and tab indexes are provided to help you identify from which file field the attachment is coming from. A null value for $tab/$row is passed for field types which are neither. So table fields would have $row either as an empty string or a zero-based value. Empty are first rows. In case a field is table within a tab, then the field from the first row of the first tab would have both $tab and $row as empty strings. |
| 47 |
*/ |
| 48 |
if('{$form_key}'!==$cf7_key){ |
| 49 |
return $label; |
| 50 |
} |
| 51 |
//for example a file field in a table within a tabbed section. |
| 52 |
$label = '<div>('.$field.'['.$tab.']['.$row.'])</div>'; |
| 53 |
return $label; |
| 54 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('mail annotation for complex array file field attachments.','cf7-grid-layout')?> |
| 55 |
</li> |
| 56 |
<li> |
| 57 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_mailtag_grid_fields','insert_table_in_mail',10,5); |
| 58 |
/** |
| 59 |
* this filter is used to build an html formated string to rpelace a mail tag of a field that is in a table or tab structure. NOTE: this filter is only fired if the mail format is set to html. |
| 60 |
* In case the field is in a table that is within a tab, then the $data field will be an array of arrays. |
| 61 |
* @param string $html an empty html string to filter. |
| 62 |
* @param string $field the name of the file field being attached |
| 63 |
* @param string $data an array of submitted data. |
| 64 |
* @param string $cf7_key unique form key. |
| 65 |
* @param boolean $table_in_tab flag tables fields in tabed sections (data is array of array). |
| 66 |
* @return string an html string to replace the mail tag. |
| 67 |
*/ |
| 68 |
function insert_table_in_mail($html, $field, $data, $cf7_key, $table_in_tab){ |
| 69 |
if('{$form_key}'!==$cf7_key){ //always validate the form being submitted. |
| 70 |
return $html; |
| 71 |
} |
| 72 |
$build = true; |
| 73 |
$pre = $table_in_tab ? '<ul style="list-style-type:none;display:inline-block;float:left;padding:5px">':''; |
| 74 |
$display = $table_in_tab ? '':'inline-'; |
| 75 |
$float = $table_in_tab ? '':'float:left;'; |
| 76 |
$end=''; |
| 77 |
$tabIdx = 0; |
| 78 |
$keys = array_keys($data); |
| 79 |
|
| 80 |
while($build){ |
| 81 |
switch($field){ //if either of fields present in the table... |
| 82 |
case 'field-one': |
| 83 |
$label = 'First'; |
| 84 |
break; |
| 85 |
case 'field-two': |
| 86 |
$label = 'Second'; |
| 87 |
break; |
| 88 |
case 'field-three': |
| 89 |
$label = 'Third'; |
| 90 |
break; |
| 91 |
default: //else this isn't a field we want in the table. |
| 92 |
$build=false; |
| 93 |
break; |
| 94 |
} |
| 95 |
if($build){ |
| 96 |
$html .=$pre.'<ul style="list-style-type:none;border-right:1px solid black;display:'.$display.'block;'.$float.'padding:5px">'; |
| 97 |
$col = $data; |
| 98 |
if($table_in_tab){ |
| 99 |
$col = $data[$keys[$tabIdx]]; |
| 100 |
$tabIdx++; |
| 101 |
$label .='('.$tabIdx.')'; |
| 102 |
$build = $tabIdx < count($data); |
| 103 |
$pre = ''; //reset now. |
| 104 |
if(!$build) $end='</ul>'; //last loop, hence close. |
| 105 |
} |
| 106 |
$html .='<li style="background-color:lightgray;margin:0;padding:3px 5px">'.$label.'</li>'; |
| 107 |
foreach($col as $key=>$value){ |
| 108 |
$html .='<li style="margin:0px;padding:3px 5px">'.$value.'</li>'; |
| 109 |
} |
| 110 |
$html .='</ul>'.$end; |
| 111 |
} |
| 112 |
} |
| 113 |
return $html; |
| 114 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('Tabled/Tabbed mail tags','cf7-grid-layout')?> |
| 115 |
</li> |
| 116 |
<li> |
| 117 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_valid_form_submission','valid_data_submission',10,3); |
| 118 |
function validate_field_submission( $submission, $cf7_key, $form_id){ |
| 119 |
if('{$form_key}'==$cf7_key ){ |
| 120 |
//$submission is an array of all submited data, including files. |
| 121 |
//if you have a file field called upload, you can get the file as |
| 122 |
//$file_path = $submission['upload'][0]; //this can be an array fo arrays if it is a repetitive field. |
| 123 |
} |
| 124 |
}" href="javascript:void(0);"><?=__('Action','cf7-grid-layout')?></a> <?=__('to access valid submit data.','cf7-grid-layout')?> |
| 125 |
</li> |
| 126 |
<li> |
| 127 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_submission_success_message','change_submission_response',10,3); |
| 128 |
/* |
| 129 |
* filter the response message for a successfull submission including HTML markup. |
| 130 |
* @param String $message to submit |
| 131 |
* @param Array $data $field_name=>$value pair of submitted data. |
| 132 |
* @param String $cf7key unique form key |
| 133 |
* @return String a message which can include HTML markup. |
| 134 |
*/ |
| 135 |
function change_submission_response($message, $data, $cf7key){ |
| 136 |
if('{$form_key}'==$cf7key ){ |
| 137 |
$message = 'thank you, please track your request <a href=[dqt]http://google.com[dqt]>here</a>'; |
| 138 |
} |
| 139 |
return $message; |
| 140 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('submission response.','cf7-grid-layout')?> |
| 141 |
</li> |
| 142 |
<li> |
| 143 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_redirect_on_success','redirect_on_success',10,3); |
| 144 |
/* |
| 145 |
* @param String $url to redirect to |
| 146 |
* @param Array $data $field_name=>$value pair of submitted data. |
| 147 |
* @param String $cf7key unique form key |
| 148 |
* @return String a message which can include HTML markup. |
| 149 |
*/ |
| 150 |
function redirect_on_success($url, $data, $cf7key){ |
| 151 |
if('{$form_key}'==$cf7key ){ |
| 152 |
$url = esc_url(home_url('/my-custom-page/')); |
| 153 |
//if you need to redirect to your localhost url (for debug purpose), |
| 154 |
//you will also need to force the url validation that follows this filter. |
| 155 |
add_filter('http_request_host_is_external', function($allow, $host, $filtered_url) use($url){ |
| 156 |
return $filtered_url === $url; |
| 157 |
}); |
| 158 |
} |
| 159 |
return $url; |
| 160 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('redirect on submit.','cf7-grid-layout')?> |
| 161 |
</li> |
| 162 |
|