Jump to content

wireEncodeJSON removes opening 0 from contact number


guy
 Share

Recommended Posts

Hi,

We're pushing a few fields to JSON and were looking to use wireEncodeJSON to remove empty values. Just found an issue with the treatment of numbers. As they are interpreted as integers the prepending 0 is stripped. i.e.

0800 1234

becomes:

800 1234

we've moved over to using json_encode for now which is fine, just wanted to see if this was a neccessary behaviour.

Link to comment
Share on other sites

In my quick test it works as it should. Maybe I don't understand.

088 1234 isnt a number but s string because of the space.

$value = array('test' => '088 1234', 'empty' => '');
print_r(wireEncodeJSON($value));
Outputs
{"test":"088 1234"}
Link to comment
Share on other sites

We're getting the phone number from a text input:

$field = $modules->get("InputfieldText");

once submitted we're processing the form:

$form->processInput($input->post);

Sanitising the inputs, and then whitelisting the values using: $input->whitelist() along with returning a JSON version using wireEncodeJSON()

The whitelisted version is outputting correctly with the 0, the wireEncodeJSON version drops it. I assume it's because it is passing it from the input as an integer whereas in your test it's a string. Might this be the case?

Link to comment
Share on other sites

Works fine here:

$form = $modules->get("InputfieldForm");
$form->action = "./";
$f = $modules->get("InputfieldText");
$f->attr('name','phone');
$form->add($f);
$f = $modules->get("InputfieldSubmit");
$form->add($f);

if($input->post->submit){
  $form->processInput($input->post);
  $input->whitelist('phone', $form->get("phone")->value);
  print_r(wireEncodeJSON(array('phone' => $input->whitelist('phone') )));
}
echo $form->render();

Outputs:  {"phone":"0888 1234"}

Link to comment
Share on other sites

I would guess that wireEncodeJSON() is converting your string value to an integer. It will do this if the value matches ctype_digit("$value"); That means a value like "0888" would get converted to an integer of 888. But a value of "0888 1234" would not, as it contains a space. In your case, I'd stick with the regular json_encode(). wireEncodeJSON() was made for saving module and field configuration settings, where we wanted to remove settings that had blank values in the name of space savings. If you also want that capability, but wireEncodeJSON() doesn't do it exactly how you want, you may want to copy the function out of /wire/core/Functions.php and into your own (named differently) in /site/templates/includes/functions.php (or something like that). You probably want to make adjustments to the line that converts it to an integer. 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...