Skip to content

Instantly share code, notes, and snippets.

@spivurno
Last active December 3, 2021 14:57
Show Gist options
  • Save spivurno/e7ea0631603b6bb7fa9a737cfd601f30 to your computer and use it in GitHub Desktop.
Save spivurno/e7ea0631603b6bb7fa9a737cfd601f30 to your computer and use it in GitHub Desktop.
Gravity Perks // GP Nested Forms // Require Unique Value Between Parent & Child
<?php
/**
* WARNING! THIS SNIPPET MAY BE OUTDATED.
* The latest version of this snippet can be found in the Gravity Wiz Snippet Library:
* https://github.com/gravitywiz/snippet-library/blob/master/gp-nested-forms/gpnf-require-unique-values.php
*/
/**
* Gravity Perks // GP Nested Forms // Require Unique Value Between Parent & Child
*
* Throw a validation error if a value is present a child entry that has been entered on the parent form.
*
* Example: Let's say you're using Nested Forms to register users for an event. The user submitting the form enters
* their contact information in the parent form and registers attendees via a Nested Form field. You may want to prevent
* the registering user from entering themselves as an attendee via the Nested Form field. This will allow you to catch
* these kinds of errors and throw a validation message on submission.
*
* @version 0.1
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/documentation/gravity-forms-nested-forms/
*
* Plugin Name: GP Nested Forms - Require Unique Value
* Plugin URI: http://gravitywiz.com/documentation/gravity-forms-nested-forms/
* Description: Throw a validation error if a value is present a child entry that has been entered on the parent form.
* Author: Gravity Wiz
* Version: 0.1
* Author URI: http://gravitywiz.com
*/
class GPNF_Required_Unique {
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'parent_form_id' => 1951,
'parent_field_id' => 5,
'nested_form_field_id' => 1,
'child_form_field_id' => 3,
'validation_message' => __( 'This value has been entered on the child form. Please update the value of this field or remove the offending child entry.' ),
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_field_validation', array( $this, 'validate' ), 10, 4 );
}
public function validate( $result, $value, $form, $field ) {
if( ! $this->is_applicable_form( $form ) || ! $this->is_applicable_field( $field ) ) {
return $result;
}
foreach( $form['fields'] as $field ) {
if( $field->get_input_type() != 'form' || $field->id != $this->_args['nested_form_field_id'] ) {
continue;
}
$child_entry_ids = explode( ',', rgpost( 'input_' . $field->id ) );
foreach( $child_entry_ids as $child_entry_id ) {
$child_entry = GFAPI::get_entry( $child_entry_id );
$child_value = rgar( $child_entry, $this->_args['child_form_field_id'] );
if( $child_value == $value ) {
$result['is_valid'] = false;
$result['message'] = $this->_args['validation_message'];
break;
}
}
}
return $result;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['parent_form_id'] ) || $form_id == $this->_args['parent_form_id'];
}
public function is_applicable_field( $field ) {
$field_id = isset( $field->id ) ? $field->id : $field;
return empty( $this->_args['parent_field_id'] ) || $field_id == $this->_args['parent_field_id'];
}
}
# Configuration
new GPNF_Required_Unique( array(
'parent_form_id' => 1951,
'parent_field_id' => 5,
'nested_form_field_id' => 1,
'child_form_field_id' => 3,
//'validation_message' => 'Oops!',
) );
@spivurno
Copy link
Author

spivurno commented Dec 3, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment