php 重力特权// GP嵌套形式//需要父和子之间的唯一值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力特权// GP嵌套形式//需要父和子之间的唯一值相关的知识,希望对你有一定的参考价值。
<?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!',
) );
以上是关于php 重力特权// GP嵌套形式//需要父和子之间的唯一值的主要内容,如果未能解决你的问题,请参考以下文章