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 | 19 |
20 // The first validation should be on a blur. The subsequent validations will | 20 // The first validation should be on a blur. The subsequent validations will |
21 // occur when the content changes. Do not validate if the view is being | 21 // occur when the content changes. Do not validate if the view is being |
22 // removed. | 22 // removed. |
23 if (!was_blurred_ && !being_removed_) { | 23 if (!was_blurred_ && !being_removed_) { |
24 was_blurred_ = true; | 24 was_blurred_ = true; |
25 Validate(); | 25 Validate(/*display_error=*/true); |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 void ValidatingTextfield::ViewHierarchyChanged( | 29 void ValidatingTextfield::ViewHierarchyChanged( |
30 const ViewHierarchyChangedDetails& details) { | 30 const ViewHierarchyChangedDetails& details) { |
31 if (details.child == this && !details.is_add) | 31 if (details.child == this && !details.is_add) |
32 being_removed_ = true; | 32 being_removed_ = true; |
33 } | 33 } |
34 | 34 |
35 void ValidatingTextfield::OnContentsChanged() { | 35 void ValidatingTextfield::OnContentsChanged() { |
36 // Validation on every keystroke only happens if the field has been validated | 36 // Validation on every keystroke only happens if the field has been validated |
37 // before as part of a blur. | 37 // before as part of a blur. |
38 if (!was_blurred_) | 38 if (!was_blurred_) |
39 return; | 39 return; |
40 | 40 |
41 Validate(); | 41 Validate(/*display_error=*/true); |
42 } | 42 } |
43 | 43 |
44 void ValidatingTextfield::Validate() { | 44 void ValidatingTextfield::Validate(bool display_error) { |
45 // ValidateTextfield may have side-effects, such as displaying errors. | 45 // ValidateTextfield may have side-effects, such as displaying errors. |
46 SetInvalid(!delegate_->ValidateTextfield(this)); | 46 SetInvalid(!delegate_->ValidateTextfield(this, display_error)); |
47 } | 47 } |
48 | 48 |
49 } // namespace payments | 49 } // namespace payments |
OLD | NEW |