Skip to content
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

added null check, so that zeros don’t turn up unexpectantly #7103

Merged
merged 1 commit into from
Jul 5, 2017

Conversation

pitchandtone
Copy link

No description provided.

Copy link
Contributor

@tractorcow tractorcow left a comment

Choose a reason for hiding this comment

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

It seems ok, but it would be good to have a test. @pitchandtone could you add a check for empty strings and nulls being converted to null?

@pitchandtone
Copy link
Author

Tests added.

public function testNullSet() {
$field = new NumericField('Number');
$field->setValue('');
$this->assertEquals($field->Value(), null);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry to be pedantic, but the expected value is on the left, expression on the right. :)

@@ -154,6 +154,9 @@ public function setValue($value, $data = null)
*/
protected function cast($value)
{
if (strlen($value) === 0) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is false being cast for a numeric field? I would expect null to be used here.

Copy link
Author

Choose a reason for hiding this comment

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

It's set to false, so that in the Value function it doesn't end up being pushed into a formatter.

I can update the Value function to check is null and false if you'd prefer.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's try null and see if we get better value for money :) It feels more semantically correct than false for empty numeric values.

$field->setValue(null);
$this->assertEquals($field->Value(), null);
$field->setValue(0);
$this->assertEquals($field->Value(), 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also please test dataValue()

@pitchandtone
Copy link
Author

Reworked now with null check.

public function testNullSet() {
$field = new NumericField('Number');
$field->setValue('');
$this->assertEquals(null, $field->Value());
Copy link
Contributor

Choose a reason for hiding this comment

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

assertNull would be a more appropriate assertion here - but I won't block merge over it

Copy link
Contributor

Choose a reason for hiding this comment

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

OK - I fixed this as part of the rebase

@dhensby dhensby changed the base branch from master to 4.0 July 4, 2017 10:04
@dhensby
Copy link
Contributor

dhensby commented Jul 4, 2017

I've rebased this onto 4.0 so that it makes the 4.0 release

@dhensby
Copy link
Contributor

dhensby commented Jul 4, 2017

It appears the tests aren't passing :/

public function testNullSet() {
$field = new NumericField('Number');
$field->setValue('');
$this->assertNull($field->Value());
Copy link
Contributor

Choose a reason for hiding this comment

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

It's coming up as '', which is probably should be the expected value for this test. Only the datavalue() should be null, although it emits an empty string to the user through the front-end Value() method

I suggest:

  • changing setValue() to setSubmittedValue() (to simulate a user submitting an empty string)
  • expect these invalid strings to be returned as-is ($this->originalValue).

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll leave this for @pitchandtone to tidy up. Remember to pull the commit locally that I've pushed up...

@pitchandtone
Copy link
Author

@dhensby I've done the rebase on 4.0 here, and fixed that text, let me know if there's anything missing.

@flamerohr flamerohr closed this Jul 5, 2017
@kinglozzer kinglozzer changed the base branch from 4.0 to 4 July 5, 2017 08:33
@kinglozzer kinglozzer reopened this Jul 5, 2017
@dhensby
Copy link
Contributor

dhensby commented Jul 5, 2017

There's a code style issue, but I'll merge by hand and fix it.

dhensby added a commit to dhensby/silverstripe-framework that referenced this pull request Jul 5, 2017
dhensby added a commit to dhensby/silverstripe-framework that referenced this pull request Jul 5, 2017
@dhensby dhensby merged commit f14e6ba into silverstripe:4 Jul 5, 2017
@tractorcow
Copy link
Contributor

Good job @pitchandtone and thanks for picking up the extra cleanup @dhensby

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

Successfully merging this pull request may close these issues.

None yet

5 participants