Skip to content

Instantly share code, notes, and snippets.

@gicolek
Last active June 27, 2024 12:23
Show Gist options
  • Save gicolek/37362734f1eeeb1e4547 to your computer and use it in GitHub Desktop.
Save gicolek/37362734f1eeeb1e4547 to your computer and use it in GitHub Desktop.
GF Validate Field Hook for
<?php
// change the number suffix to the one corresponding to your form and field's id!
add_filter( 'gform_field_validation_1_1', 'wp_doin_validate_password_field', 10, 4 );
/**
* @hook gform_field_validation_4
*
* This function is assigned to the old password field, and then iterates over all form fields
* if it hits a field with the pass-confirm class assigned it then compares it's value
* with the value of the old password field and validates it. If the passwords match
* it returns valid result!
*/
function wp_doin_validate_password_field( $result, $value, $form, $field ) {
// iterate over gravity forms fields
foreach ( $form['fields'] as &$field ) {
// continue unless confirm password field was found
if ( strpos( $field['cssClass'], 'repeat-pass' ) === false ) {
continue;
}
// verify password field have the same value
$confirm_value = rgpost( "input_{$field['id']}" );
if ( $value !== $confirm_value ) {
// set up validation variables
$validation_result['is_valid'] = false;
$field['failed_validation'] = true;
$field['validation_message'] = 'The passwords do not match.';
// return new validation result
$validation_result['form'] = $form;
return $validation_result;
} else {
continue;
}
}
return $result;
}
// change the number suffix to the one corresponding to your form and field's id!
add_filter( 'gform_field_validation_1_2', 'wp_doin_validate_old_password_field', 10, 4 );
/**
* This function is assigned to the old password field and checks if the old password
* provided is the one registered with the current user account
*/
function wp_doin_validate_old_password_field($result, $value, $form, $field) {
// iterate over gravity forms fields
foreach ( $form['fields'] as &$field ) {
// continue unless confirm password field was found
if ( strpos( $field['cssClass'], 'old-pass' ) === false ) {
continue;
}
$user = wp_get_current_user();
// verify password field have the same value
$confirm_value = rgpost( "input_{$field['id']}" );
if ( !wp_check_password( $value, $user->data->user_pass, $user->ID ) ) {
// mark the result as invalid
$result["is_valid"] = false;
$result["message"] = "Wrong password provided.";
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment