Skip to content

Commit 05ea6d2

Browse files
jukrak1sul1
authored andcommitted
Allow only fields that are set in form to be submitted
Fix styling errors
1 parent 82b12ab commit 05ea6d2

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

inc/wplf-form-validation.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,42 @@ function wplf_validate_required_empty( $return ) {
6060

6161
return $return;
6262
}
63+
64+
/**
65+
* Check that submission has only fields that are set in form
66+
*/
67+
add_filter( 'wplf_validate_submission', 'wplf_validate_additional_fields' );
68+
function wplf_validate_additional_fields( $return ) {
69+
// skip this validation if submission has already failed
70+
if ( ! $return->ok ) {
71+
return $return;
72+
}
73+
74+
// get all fields from form
75+
$form_fields = explode( ',', get_post_meta( $_POST['_form_id'], '_wplf_fields', true ) );
76+
77+
// add all default fields
78+
$default_fields = array( 'referrer', '_referrer_id', '_form_id' );
79+
80+
// combine fields
81+
$all_fields = array_merge( $form_fields, $default_fields );
82+
83+
// make sure fields from all_fields are the only ones present in $_POST
84+
$additional_fields = array();
85+
foreach ( $_POST as $key => $value ) {
86+
if ( ! in_array( $key, $all_fields ) ) {
87+
// field was not in form fields
88+
$additional_fields[] = $key;
89+
}
90+
}
91+
$additional_fields = array_filter( $additional_fields ); // get rid of the empty keys
92+
93+
if ( ! empty( $additional_fields ) ) {
94+
$return->ok = 0;
95+
$return->error = __( 'Additional fields are present.', 'wp-libre-form' );
96+
$return->additional_fields = $additional_fields;
97+
}
98+
99+
return $return;
100+
}
101+

0 commit comments

Comments
 (0)