Chromium Code Reviews| Index: chrome/browser/ui/views/payments/validating_textfield.cc |
| diff --git a/chrome/browser/ui/views/payments/validating_textfield.cc b/chrome/browser/ui/views/payments/validating_textfield.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f164d7be4be28f15565f45396e2e763f192fd3cd |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/payments/validating_textfield.cc |
| @@ -0,0 +1,40 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/views/payments/validating_textfield.h" |
| + |
| +#include "base/strings/utf_string_conversions.h" |
|
anthonyvd
2017/02/08 15:28:28
unused?
Mathieu
2017/02/08 21:21:50
Done.
|
| + |
| +namespace payments { |
| + |
| +ValidatingTextfield::ValidatingTextfield( |
| + ValidatingTextfield::Delegate* delegate) |
| + : Textfield(), delegate_(delegate) {} |
| + |
| +void ValidatingTextfield::OnBlur() { |
| + Textfield::OnBlur(); |
| + |
| + // The first validation of non-empty data should be on a blur. The subsequent |
| + // validations will occur when the contents changes. |
| + if (!was_validated_ && !text().empty()) { |
| + was_validated_ = true; |
| + Validate(); |
| + } |
| +} |
| + |
| +void ValidatingTextfield::OnContentsChanged() { |
| + // Validation on every keystroke only happens if the field has been validated |
| + // before as part of a blur. |
| + if (!was_validated_) |
| + return; |
| + |
| + Validate(); |
| +} |
| + |
| +void ValidatingTextfield::Validate() { |
| + // ValidateTextfield may have side-effects, such as displaying errors. |
| + SetInvalid(!delegate_->ValidateTextfield(this)); |
| +} |
| + |
| +} // namespace payments |