-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Conversation
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.
types/Decimal.php
Outdated
@@ -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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
types/Decimal.php
Outdated
@@ -113,7 +113,9 @@ public function validate($rawvalue) | |||
throw new ValidationException('Decimal max', (float) $this->config['max']); | |||
} | |||
|
|||
// remove leading zeros |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.