| 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 | 25 |
| 29 void ValidatingTextfield::ViewHierarchyChanged( | 26 void ValidatingTextfield::ViewHierarchyChanged( |
| 30 const ViewHierarchyChangedDetails& details) { | 27 const ViewHierarchyChangedDetails& details) { |
| 31 if (details.child == this && !details.is_add) | 28 if (details.child == this && !details.is_add) |
| 32 being_removed_ = true; | 29 being_removed_ = true; |
| 33 } | 30 } |
| 34 | 31 |
| 35 void ValidatingTextfield::OnContentsChanged() { | 32 void ValidatingTextfield::OnContentsChanged() { |
| 36 // Validation on every keystroke only happens if the field has been validated | 33 // This is called on every keystroke. |
| 37 // before as part of a blur. | |
| 38 if (!was_blurred_) | |
| 39 return; | |
| 40 | |
| 41 Validate(); | 34 Validate(); |
| 42 } | 35 } |
| 43 | 36 |
| 44 bool ValidatingTextfield::IsValid() { | 37 bool ValidatingTextfield::IsValid() { |
| 45 bool valid = delegate_->IsValidTextfield(this); | 38 return delegate_->IsValidTextfield(this); |
| 46 SetInvalid(!valid); | |
| 47 return valid; | |
| 48 } | 39 } |
| 49 | 40 |
| 50 void ValidatingTextfield::Validate() { | 41 void ValidatingTextfield::Validate() { |
| 51 // TextfieldValueChanged may have side-effects, such as displaying errors. | 42 // TextfieldValueChanged may have side-effects, such as displaying errors. |
| 52 SetInvalid(!delegate_->TextfieldValueChanged(this)); | 43 SetInvalid(!delegate_->TextfieldValueChanged(this, was_blurred_)); |
| 53 } | 44 } |
| 54 | 45 |
| 55 } // namespace payments | 46 } // namespace payments |
| OLD | NEW |