Skip to content

Commit

Permalink
Add number format
Browse files Browse the repository at this point in the history
  • Loading branch information
shervElmi committed Feb 4, 2025
1 parent dae5ec6 commit 776bc47
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ private static function should_allow_input_attribute( $key ) {
*/
private static function add_pattern_attribute( $field, array &$add_html ) {
$format_value = FrmField::get_option( $field, 'format' );
$has_format = $format_value && 'currency' !== $format_value;
$has_format = $format_value && ! FrmCurrencyHelper::is_currency_format( $format_value );
$format_field = FrmField::is_field_type( $field, 'text' );

if ( $field['type'] === 'phone' || ( $has_format && $format_field ) ) {
Expand Down
10 changes: 10 additions & 0 deletions classes/helpers/FrmCurrencyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public static function get_currency( $currency ) {
return $currency;
}

/**
* Checks if the given format is a valid currency format.
*
* @param string $format_value The format value to check.
* @return bool
*/
public static function is_currency_format( $format_value ) {
return ! empty( $format_value ) && in_array( $format_value, array( 'currency', 'number' ), true );
}

/**
* Get a list of all supported currencies.
*
Expand Down
4 changes: 2 additions & 2 deletions classes/models/FrmEntryValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ public static function validate_field_types( &$errors, $posted_field, $value, $a

public static function validate_phone_field( &$errors, $field, $value, $args ) {
$format_value = FrmField::get_option( $field, 'format' );
if ( $field->type === 'phone' || ( $field->type === 'text' && $format_value && 'currency' !== $format_value ) ) {

if ( $field->type === 'phone' || ( $field->type === 'text' && ! FrmCurrencyHelper::is_currency_format( $format_value ) ) ) {
$pattern = self::phone_format( $field );

if ( ! preg_match( $pattern, $value ) ) {
Expand Down Expand Up @@ -351,7 +351,7 @@ public static function spam_check( $exclude, $values, &$errors ) {
private static function maybe_normalize_formatted_numbers( $field, &$value ) {
if (
is_callable( 'FrmProCurrencyHelper::normalize_formatted_numbers' )
&& 'currency' === FrmField::get_option( $field, 'format' )
&& FrmCurrencyHelper::is_currency_format( FrmField::get_option( $field, 'format' ) )
) {
$value = FrmProCurrencyHelper::normalize_formatted_numbers( $field, $value );
}
Expand Down
13 changes: 12 additions & 1 deletion classes/views/frm-fields/back-end/format-dropdown-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@
)
);

FrmHtmlHelper::echo_dropdown_option(
__( 'Currency', 'formidable' ),
false,
array(
'value' => '',
'class' => 'frm_show_upgrade frm_noallow',
'data-upgrade' => __( 'Format currency field', 'formidable' ),
'data-medium' => 'format-currency-field',
)
);

if ( 'text' === $field_type ) {
FrmHtmlHelper::echo_dropdown_option(
__( 'Custom', 'formidable' ),
! empty( $format ) && 'currency' !== $format,
! empty( $format ) && ! FrmCurrencyHelper::is_currency_format( $format ),
array(
'value' => 'custom',
'data-dependency' => '#frm-field-format-custom-' . $field_id,
Expand Down
5 changes: 3 additions & 2 deletions js/formidable_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5439,7 +5439,7 @@ function frmAdminBuildJS() {
const fieldId = formatElement.dataset.fieldId;
const formatInput = document.getElementById( `frm-field-format-custom-${fieldId}` ).querySelector( '.frm_format_opt' );

if ( 'international' === formatInput.value || 'currency' === formatInput.value ) {
if ( 'international' === formatInput.value || 'currency' === formatInput.value || 'number' === formatInput.value ) {
formatInput.setAttribute( 'value', '' );
}
}
Expand Down Expand Up @@ -6702,7 +6702,8 @@ function frmAdminBuildJS() {
const valueMap = {
none: '',
international: 'international',
currency: 'currency'
currency: 'currency',
number: 'number'
};

formatTypes.forEach( formatType => {
Expand Down

0 comments on commit 776bc47

Please sign in to comment.