Created
August 30, 2017 13:02
-
-
Save kenmasters/873990e730b34a4eb8f0a9a228b8900d to your computer and use it in GitHub Desktop.
Creating a custom gravityform field (Canada SIN [Standard Insurance Number])
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*******/ | |
class GF_Field_SIN extends GF_Field { | |
public $type = 'sin'; | |
public function get_form_editor_field_title() { | |
return esc_attr__( 'SIN', 'gravityforms' ); | |
} | |
public function get_form_editor_button() { | |
return array( | |
'group' => 'advanced_fields', | |
'text' => $this->get_form_editor_field_title(), | |
); | |
} | |
public function get_form_editor_field_settings() { | |
return array( | |
'conditional_logic_field_setting', | |
'prepopulate_field_setting', | |
'error_message_setting', | |
'label_setting', | |
'admin_label_setting', | |
'rules_setting', | |
'duplicate_setting', | |
'description_setting', | |
'css_class_setting', | |
); | |
} | |
public function is_conditional_logic_supported() { | |
return true; | |
} | |
function validate( $value, $form ) { | |
if ( $this->isRequired ) { | |
$first = rgpost( 'input_' . $this->id . '_1' ); | |
$middle = rgpost( 'input_' . $this->id . '_2' ); | |
$last = rgpost( 'input_' . $this->id . '_3' ); | |
$sinLength = strlen($first.$middle.$last); | |
if ( empty($first) || empty($middle) || empty($last) || $sinLength != 9 ) { | |
$this->failed_validation = true; | |
$this->validation_message = empty( $this->errorMessage ) ? esc_html__( 'Invalid SIN# format', 'gravityforms' ) : $this->errorMessage; | |
} | |
} | |
} | |
// public function get_input_property( $input_id, $property_name ) { | |
// $input = GFFormsModel::get_input( $this, $this->id . '.' . (string) $input_id ); | |
// return rgar( $input, $property_name ); | |
// } | |
public function get_field_input( $form, $value = '', $entry = null ) { | |
$is_entry_detail = $this->is_entry_detail(); | |
$is_form_editor = $this->is_form_editor(); | |
$form_id = $form['id']; | |
$field_id = intval( $this->id ); | |
$first = $last = $email = ''; | |
if ( is_array( $value ) ) { | |
$first = esc_attr( rgget( $this->id . '.1', $value ) ); | |
$last = esc_attr( rgget( $this->id . '.2', $value ) ); | |
$email = esc_attr( rgget( $this->id . '.3', $value ) ); | |
} | |
$disabled_text = $is_form_editor ? "disabled='disabled'" : ''; | |
$class_suffix = $is_entry_detail ? '_admin' : ''; | |
$first_tabindex = GFCommon::get_tabindex(); | |
$last_tabindex = GFCommon::get_tabindex(); | |
$email_tabindex = GFCommon::get_tabindex(); | |
$required_attribute = $this->isRequired ? 'aria-required="true"' : ''; | |
$invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"'; | |
$first_markup = '<div class="wrap-sin"><input class="sin-num"' . $required_attribute . ' maxLength=3 type="text" name="input_' . $field_id . '.1" id="input_' . $field_id . '_' . $form_id . '_1" value="' . $first . '" ' . $first_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '></div>'; | |
$last_markup = '<div class="wrap-sin"><input class="sin-num"' . $required_attribute . ' maxLength=3 type="text" name="input_' . $field_id . '.2" id="input_' . $field_id . '_' . $form_id . '_2" value="' . $last . '" ' . $last_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '></div>'; | |
$email_markup = '<div class="wrap-sin"><input class="sin-num"' . $required_attribute . ' maxLength=3 type="text" name="input_' . $field_id . '.3" id="input_' . $field_id . '_' . $form_id . '_3" value="' . $email . '" ' . $email_tabindex . ' ' . $disabled_text . ' ' . $required_attribute . ' ' . $invalid_attribute . '></div>'; | |
$css_class = $this->get_css_class(); | |
return "<div style='overflow:hidden;' class='ginput_complex{$class_suffix} ginput_container {$css_class} gfield_trigger_change' id='{$field_id}'> | |
{$first_markup} | |
{$last_markup} | |
{$email_markup} | |
</div>"; | |
} | |
public function get_css_class() { | |
return trim("gf_sinnum ginput_container_sinnum"); | |
} | |
public function get_form_editor_inline_script_on_page_render() { | |
// set the default field label for the field | |
$script = sprintf( "function SetDefaultValues_%s(field) { | |
field.label = '%s'; | |
field.inputs = [new Input(field.id + '.1', '%s'), new Input(field.id + '.2', '%s'), new Input(field.id + '.3', '%s')]; | |
}", $this->type, $this->get_form_editor_field_title(), '', '', '' ) . PHP_EOL; | |
return $script; | |
} | |
public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) { | |
if ( is_array( $value ) ) { | |
$first = trim( rgget( $this->id . '.1', $value ) ); | |
$last = trim( rgget( $this->id . '.2', $value ) ); | |
$email = trim( rgget( $this->id . '.3', $value ) ); | |
$return = $first; | |
$return .= ! empty( $return ) && ! empty( $last ) ? " $last" : $last; | |
$return .= ! empty( $return ) && ! empty( $email ) ? " $email" : $email; | |
} else { | |
$return = ''; | |
} | |
if ( $format === 'html' ) { | |
$return = esc_html( $return ); | |
} | |
return $return; | |
} | |
} | |
GF_Fields::register( new GF_Field_SIN() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment