Skip to content

Commit

Permalink
Add support for setting default values when a field is empty in a form
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Mar 23, 2024
1 parent d403566 commit 96ea490
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 18 additions & 4 deletions classes/PodsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,24 @@ public function build_dfv_field_data( $args ) {
$attributes = $this->build_dfv_field_attributes( $attributes, $args );
$attributes = array_map( 'esc_attr', $attributes );

$default_value = '';
$field_value = isset( $args->value ) ? $args->value : null;

if ( 'multi' === pods_v( $args->type . '_format_type' ) ) {
$default_value = [];
if ( empty( $field_value ) ) {
$default_value = '';

$is_multi = 'multi' === pods_v( $args->type . '_format_type', 'single' );

if ( $is_multi ) {
$default_value = [];
}

if ( null === $field_value || 1 === (int) pods_v( 'default_empty_fields', $options, 0 ) ) {
$field_value = PodsForm::default_value( $default_value, $args->type, pods_v( 'name', $options, $args->name ), $options, $args->pod, $args->id );

if ( $is_multi ) {
$field_value = explode( ',', $field_value );
}
}
}

// Build DFV field data.
Expand All @@ -474,7 +488,7 @@ public function build_dfv_field_data( $args ) {
'fieldItemData' => $this->build_dfv_field_item_data( $args ),
'fieldConfig' => $this->build_dfv_field_config( $args ),
'fieldEmbed' => true,
'fieldValue' => isset( $args->value ) ? $args->value : PodsForm::default_value( $default_value, $args->type, pods_v( 'name', $options, $args->name ), $options, $args->pod, $args->id ),
'fieldValue' => $field_value,
];

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Pods/Admin/Config/Field.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 96ea490

Please sign in to comment.