| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/payments/validating_textfield.h" | 5 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 namespace payments { | 9 namespace payments { |
| 10 | 10 |
| 11 ValidatingTextfield::ValidatingTextfield( | 11 ValidatingTextfield::ValidatingTextfield( |
| 12 std::unique_ptr<ValidationDelegate> delegate) | 12 std::unique_ptr<ValidationDelegate> delegate) |
| 13 : Textfield(), delegate_(std::move(delegate)) {} | 13 : Textfield(), delegate_(std::move(delegate)) {} |
| 14 | 14 |
| 15 ValidatingTextfield::~ValidatingTextfield() {} | 15 ValidatingTextfield::~ValidatingTextfield() {} |
| 16 | 16 |
| 17 void ValidatingTextfield::OnBlur() { | 17 void ValidatingTextfield::OnBlur() { |
| 18 Textfield::OnBlur(); | 18 Textfield::OnBlur(); |
| 19 was_blurred_ = true; |
| 19 | 20 |
| 20 // The first validation should be on a blur. The subsequent validations will | 21 // Do not validate if the view is being removed. |
| 21 // occur when the content changes. Do not validate if the view is being | 22 if (!being_removed_) |
| 22 // removed. | |
| 23 if (!was_blurred_ && !being_removed_) { | |
| 24 was_blurred_ = true; | |
| 25 Validate(); | 23 Validate(); |
| 26 } | |
| 27 | 24 |
| 28 if (!text().empty() && delegate_->ShouldFormat()) | 25 if (!text().empty() && delegate_->ShouldFormat()) |
| 29 SetText(delegate_->Format(text())); | 26 SetText(delegate_->Format(text())); |
| 30 } | 27 } |
| 31 | 28 |
| 32 void ValidatingTextfield::ViewHierarchyChanged( | 29 void ValidatingTextfield::ViewHierarchyChanged( |
| 33 const ViewHierarchyChangedDetails& details) { | 30 const ViewHierarchyChangedDetails& details) { |
| 34 if (details.child == this && !details.is_add) | 31 if (details.child == this && !details.is_add) |
| 35 being_removed_ = true; | 32 being_removed_ = true; |
| 36 } | 33 } |
| 37 | 34 |
| 38 void ValidatingTextfield::OnContentsChanged() { | 35 void ValidatingTextfield::OnContentsChanged() { |
| 36 // This is called on every keystroke. |
| 39 if (!text().empty() && GetCursorPosition() == text().length() && | 37 if (!text().empty() && GetCursorPosition() == text().length() && |
| 40 delegate_->ShouldFormat()) { | 38 delegate_->ShouldFormat()) { |
| 41 SetText(delegate_->Format(text())); | 39 SetText(delegate_->Format(text())); |
| 42 } | 40 } |
| 43 | 41 |
| 44 // Validation on every keystroke only happens if the field has been validated | |
| 45 // before as part of a blur. | |
| 46 if (!was_blurred_) | |
| 47 return; | |
| 48 | |
| 49 Validate(); | 42 Validate(); |
| 50 } | 43 } |
| 51 | 44 |
| 52 bool ValidatingTextfield::IsValid() { | 45 bool ValidatingTextfield::IsValid() { |
| 53 bool valid = delegate_->IsValidTextfield(this); | 46 return delegate_->IsValidTextfield(this); |
| 54 SetInvalid(!valid); | |
| 55 return valid; | |
| 56 } | 47 } |
| 57 | 48 |
| 58 void ValidatingTextfield::Validate() { | 49 void ValidatingTextfield::Validate() { |
| 59 // TextfieldValueChanged may have side-effects, such as displaying errors. | 50 // TextfieldValueChanged may have side-effects, such as displaying errors. |
| 60 SetInvalid(!delegate_->TextfieldValueChanged(this)); | 51 SetInvalid(!delegate_->TextfieldValueChanged(this, was_blurred_)); |
| 61 } | 52 } |
| 62 | 53 |
| 63 } // namespace payments | 54 } // namespace payments |
| OLD | NEW |