Skip Navigation

[Résolu] numeric fields with commas causing a problem

This support ticket is created Il y a 7 années et 7 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 réponses, has 2 voix.

Last updated by Minesh Il y a 7 années et 7 mois.

Assisted by: Minesh.

Auteur
Publications
#436080

I have multiple forms on a page. When I try to enter a numeric value (1,000) with a comma, it creates errors across all forms. If the form is the only one on the page, then it just puts up a validation error "enter numeric data".

I think it's still related to the problem seen before with multiple forms on a page.
https://toolset.com/forums/topic/more-then-one-cred-form-on-a-page-conflict-with-each-other/

#436086

I was going to just strip commas from the data before posts, but now I realize that the automatic validation runs before the validation hook, so I can alter the data before it tries to validate.

So at this point I don't have a server side solution. Is there another way?
I'll have to use JS if not, to alter it before saving.

add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data)
{
  	if ( isset($_POST['wpcf-trans-credit']) ){
      $a = $_POST['wpcf-trans-credit'];
    	$a = str_replace(',', '', $a);;
      $_POST['wpcf-trans-credit'] = $a;
  	}
}

When it reloads the form, the value now has the commas stripped, but the form still didn't post because of the validation error it created.

#436359

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

What if you use CRED hook: "cred_before_save_data"
=> https://toolset.com/documentation/user-guides/cred-api/#cbsd

#436456

That doesn't work, because It has to happen before validation, and there isn't a "before_validation" hook.

The validation is what's causing the error, because it's returning a "enter numeric value" response, even though it doesn't create the error via javascript like the rest of the errors are.

I know it was mentioned that the " multiple forms on one page" had been corrected in the latest version of CRED, but I think there are still issues with it. Error messages show up in all forms with something like this.

For now I have implemented a javascript solution to format the number (without comma) after you leave the input field. It is working well. I would just also like to see the server side fixed for it.

Here's the jquery plugin I'm using if anyone else needs it.
hidden link

Thanks

#437295

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

After string replace, You should try to convert the string to number using intval() or floatval() functions.

More info:
=> http://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php

#437722

Can you verify this works?

The built-in validation runs before the validation hook, so I don't think this will work. It's validating the data before you can touch it (server side).

I believe this needs logged as a bug within CRED.

#437923

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

You are right. Numeric field do not allow comma as of now. Please allow me to check with our developers and I'll get in touch with you as soon as possible.

#437951

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

I got the confirmation that numeric filed will only allow numeric input that means no comma or point or other special characters allowed as input to this field type.

We need to keep in mind, that Numeric Field can be used in Views for sorting or filtering and due to that reason whatever we change, must also keep working fine in that case.

If you want to handle your numeric field in a different way, you should try to replace your cred field with cred generic field.
=> https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

#438025

Ok, that's understandable, but the validation needs to catch it client side then.

If i put 1,00 it will throw a client side validation error saying "enter numeric".

But If i put 1,000 , it will not throw any error because it sees it as a legitimate number, until it's posted, and then the validation throws an error. If it would just catch it client side, then that would fix the issue.

I started using a jquery plugin to fix the issue. It formats the number to ####.## once the focus leaves the input field.

You mentioned that periods are not allowed either, but I have never had any problems with periods. I use these fields for currency input (ie $234.00), and so far the jquery plugin is working well to fix the issue.

#438139

Minesh
Supporter

Languages: Anglais (English )

Timezone: Asia/Kolkata (GMT+05:30)

Glad to know that jQuery plugin helpful in order to fix your issue.

Kindly open a new ticket with your each new question. This will help other users searching on the forum.

Thank you for understanding.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.