| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/payments/validating_textfield.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 namespace payments { |
| 10 |
| 11 ValidatingTextfield::ValidatingTextfield( |
| 12 std::unique_ptr<ValidatingTextfield::Delegate> delegate) |
| 13 : Textfield(), delegate_(std::move(delegate)) {} |
| 14 |
| 15 ValidatingTextfield::~ValidatingTextfield() {} |
| 16 |
| 17 void ValidatingTextfield::OnBlur() { |
| 18 Textfield::OnBlur(); |
| 19 |
| 20 // The first validation of non-empty data should be on a blur. The subsequent |
| 21 // validations will occur when the contents changes. |
| 22 if (!was_validated_ && !text().empty()) { |
| 23 was_validated_ = true; |
| 24 Validate(); |
| 25 } |
| 26 } |
| 27 |
| 28 void ValidatingTextfield::OnContentsChanged() { |
| 29 // Validation on every keystroke only happens if the field has been validated |
| 30 // before as part of a blur. |
| 31 if (!was_validated_) |
| 32 return; |
| 33 |
| 34 Validate(); |
| 35 } |
| 36 |
| 37 void ValidatingTextfield::Validate() { |
| 38 // ValidateTextfield may have side-effects, such as displaying errors. |
| 39 SetInvalid(!delegate_->ValidateTextfield(this)); |
| 40 } |
| 41 |
| 42 } // namespace payments |
| OLD | NEW |