Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: chrome/browser/ui/views/payments/credit_card_editor_view_controller.cc

Issue 2881643002: Focus first invalid field of payment request editor (Closed)
Patch Set: Fixed a case typo Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/credit_card_editor_view_controller.h" 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 #include <vector> 10 #include <vector>
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 EditorViewController* controller, 383 EditorViewController* controller,
384 const std::vector<std::string>& supported_card_networks) 384 const std::vector<std::string>& supported_card_networks)
385 : field_(field), 385 : field_(field),
386 controller_(controller), 386 controller_(controller),
387 supported_card_networks_(supported_card_networks.begin(), 387 supported_card_networks_(supported_card_networks.begin(),
388 supported_card_networks.end()) {} 388 supported_card_networks.end()) {}
389 CreditCardEditorViewController::CreditCardValidationDelegate:: 389 CreditCardEditorViewController::CreditCardValidationDelegate::
390 ~CreditCardValidationDelegate() {} 390 ~CreditCardValidationDelegate() {}
391 391
392 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 392 bool CreditCardEditorViewController::CreditCardValidationDelegate::
393 ValidateTextfield(views::Textfield* textfield) { 393 IsValidTextfield(views::Textfield* textfield) {
394 return ValidateValue(textfield->text()); 394 return ValidateValue(textfield->text(), nullptr);
395 } 395 }
396 396
397 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 397 bool CreditCardEditorViewController::CreditCardValidationDelegate::
398 ValidateCombobox(views::Combobox* combobox) { 398 IsValidCombobox(views::Combobox* combobox) {
399 return ValidateCombobox(combobox, nullptr);
400 }
401
402 bool CreditCardEditorViewController::CreditCardValidationDelegate::
403 TextfieldValueChanged(views::Textfield* textfield) {
404 base::string16 error_message;
405 bool is_valid = ValidateValue(textfield->text(), &error_message);
406 controller_->DisplayErrorMessageForField(field_, error_message);
407 return is_valid;
408 }
409
410 bool CreditCardEditorViewController::CreditCardValidationDelegate::
411 ComboboxValueChanged(views::Combobox* combobox) {
412 base::string16 error_message;
413 bool is_valid = ValidateCombobox(combobox, nullptr);
414 controller_->DisplayErrorMessageForField(field_, error_message);
415 return is_valid;
416 }
417
418 bool CreditCardEditorViewController::CreditCardValidationDelegate::
419 ValidateValue(const base::string16& value, base::string16* error_message) {
420 if (!value.empty()) {
421 base::string16 local_error_message;
422 bool is_valid =
423 field_.type == autofill::CREDIT_CARD_NUMBER
424 ? autofill::IsValidCreditCardNumberForBasicCardNetworks(
425 value, supported_card_networks_, &local_error_message)
426 : autofill::IsValidForType(value, field_.type,
427 &local_error_message);
428 if (error_message)
429 *error_message = local_error_message;
430 return is_valid;
431 }
432
433 if (error_message && field_.required) {
434 *error_message = l10n_util::GetStringUTF16(
435 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
436 }
437 return !field_.required;
438 }
439
440 bool CreditCardEditorViewController::CreditCardValidationDelegate::
441 ValidateCombobox(views::Combobox* combobox, base::string16* error_message) {
399 // The billing address ID is the selected item identifier and not the combobox 442 // The billing address ID is the selected item identifier and not the combobox
400 // value itself. 443 // value itself.
401 if (field_.type == kBillingAddressType) { 444 if (field_.type == kBillingAddressType) {
402 // TODO(crbug.com/718905) Find a way to deal with existing incomplete 445 // TODO(crbug.com/718905) Find a way to deal with existing incomplete
403 // addresses when choosing them as billing addresses. 446 // addresses when choosing them as billing addresses.
404 autofill::AddressComboboxModel* model = 447 autofill::AddressComboboxModel* model =
405 static_cast<autofill::AddressComboboxModel*>(combobox->model()); 448 static_cast<autofill::AddressComboboxModel*>(combobox->model());
406 return !model->GetItemIdentifierAt(combobox->selected_index()).empty(); 449 if (model->GetItemIdentifierAt(combobox->selected_index()).empty()) {
450 if (error_message) {
451 *error_message =
452 l10n_util::GetStringUTF16(IDS_PAYMENTS_BILLING_ADDRESS_REQUIRED);
453 }
454 return false;
455 }
456 return true;
407 } 457 }
408 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); 458 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()),
409 } 459 error_message);
410
411 bool CreditCardEditorViewController::CreditCardValidationDelegate::
412 ValidateValue(const base::string16& value) {
413 if (!value.empty()) {
414 base::string16 error_message;
415 bool is_valid =
416 field_.type == autofill::CREDIT_CARD_NUMBER
417 ? autofill::IsValidCreditCardNumberForBasicCardNetworks(
418 value, supported_card_networks_, &error_message)
419 : autofill::IsValidForType(value, field_.type, &error_message);
420 controller_->DisplayErrorMessageForField(field_, error_message);
421 return is_valid;
422 }
423
424 bool is_required_valid = !field_.required;
425 const base::string16 displayed_message =
426 is_required_valid ? base::ASCIIToUTF16("")
427 : l10n_util::GetStringUTF16(
428 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
429 controller_->DisplayErrorMessageForField(field_, displayed_message);
430 return is_required_valid;
431 } 460 }
432 461
433 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { 462 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) {
434 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; 463 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET;
435 return true; 464 return true;
436 } 465 }
437 466
438 } // namespace payments 467 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698