Skip to content

Remove leading zeros in Decimal type. #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

gregoryloichot
Copy link

Fix a bug. It was possible to create Decimal like 00123. This Decimal could then be edited to 100123, but could not be edited back to 00123. This pull request remove the leading zeros.

Fix a bug. I was possible to create Decimal like 00123. This Decimal could be edited to 100123, but could not be reedited to 00123.
@@ -99,7 +99,7 @@ public function renderValue($value, \Doku_Renderer $R, $mode)
public function validate($rawvalue)
{
$rawvalue = parent::validate($rawvalue);
$rawvalue = str_replace(',', '.', $rawvalue); // we accept both
$rawvalue = trim((float)str_replace(',', '.', $rawvalue), 0); // we accept both
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my value is 1000 this will reduce it to 1.

echo trim((float) '1000', 0);
1

I believe casting the value to float should already do what you want.

echo (float) '00004440000';
4440000

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are right. I will change that.

@@ -113,7 +113,9 @@ public function validate($rawvalue)
throw new ValidationException('Decimal max', (float) $this->config['max']);
}

// remove leading zeros
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment seems to be in the wrong place

@@ -99,7 +99,7 @@ public function renderValue($value, \Doku_Renderer $R, $mode)
public function validate($rawvalue)
{
$rawvalue = parent::validate($rawvalue);
$rawvalue = str_replace(',', '.', $rawvalue); // we accept both
$rawvalue = trim((float)str_replace(',', '.', $rawvalue), 0); // we accept both

if ((string)$rawvalue != (string)(float) $rawvalue) {
throw new ValidationException('Decimal needed');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering. Shouldn't this already prevent the issue you're trying to fix?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments.

I updated the code. Let me know if you have any comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants