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

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: Last final nit :-) 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 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 void CreditCardEditorViewController::AddAndSelectNewBillingAddress( 382 void CreditCardEditorViewController::AddAndSelectNewBillingAddress(
383 const autofill::AutofillProfile& profile) { 383 const autofill::AutofillProfile& profile) {
384 views::Combobox* address_combobox = 384 views::Combobox* address_combobox =
385 static_cast<views::Combobox*>(dialog()->GetViewByID(kBillingAddressType)); 385 static_cast<views::Combobox*>(dialog()->GetViewByID(kBillingAddressType));
386 autofill::AddressComboboxModel* model = 386 autofill::AddressComboboxModel* model =
387 static_cast<autofill::AddressComboboxModel*>(address_combobox->model()); 387 static_cast<autofill::AddressComboboxModel*>(address_combobox->model());
388 int index = model->AddNewProfile(profile); 388 int index = model->AddNewProfile(profile);
389 // SetSelectedIndex doesn't trigger a perform action notification, which is 389 // SetSelectedIndex doesn't trigger a perform action notification, which is
390 // needed to update the valid state. 390 // needed to update the valid state.
391 address_combobox->SetSelectedRow(index); 391 address_combobox->SetSelectedRow(index);
392 // But it needs to be blured at least once.
393 address_combobox->OnBlur();
392 } 394 }
393 395
394 CreditCardEditorViewController::CreditCardValidationDelegate:: 396 CreditCardEditorViewController::CreditCardValidationDelegate::
395 CreditCardValidationDelegate( 397 CreditCardValidationDelegate(
396 const EditorField& field, 398 const EditorField& field,
397 EditorViewController* controller, 399 EditorViewController* controller,
398 const std::vector<std::string>& supported_card_networks) 400 const std::vector<std::string>& supported_card_networks)
399 : field_(field), 401 : field_(field),
400 controller_(controller), 402 controller_(controller),
401 supported_card_networks_(supported_card_networks.begin(), 403 supported_card_networks_(supported_card_networks.begin(),
402 supported_card_networks.end()) {} 404 supported_card_networks.end()) {}
403 CreditCardEditorViewController::CreditCardValidationDelegate:: 405 CreditCardEditorViewController::CreditCardValidationDelegate::
404 ~CreditCardValidationDelegate() {} 406 ~CreditCardValidationDelegate() {}
405 407
406 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 408 bool CreditCardEditorViewController::CreditCardValidationDelegate::
407 ValidateTextfield(views::Textfield* textfield) { 409 IsValidTextfield(views::Textfield* textfield) {
408 return ValidateValue(textfield->text()); 410 return ValidateValue(textfield->text(), nullptr);
409 } 411 }
410 412
411 bool CreditCardEditorViewController::CreditCardValidationDelegate:: 413 bool CreditCardEditorViewController::CreditCardValidationDelegate::
412 ValidateCombobox(views::Combobox* combobox) { 414 IsValidCombobox(views::Combobox* combobox) {
415 return ValidateCombobox(combobox, nullptr);
416 }
417
418 bool CreditCardEditorViewController::CreditCardValidationDelegate::
419 TextfieldValueChanged(views::Textfield* textfield) {
420 base::string16 error_message;
421 bool is_valid = ValidateValue(textfield->text(), &error_message);
422 controller_->DisplayErrorMessageForField(field_, error_message);
423 return is_valid;
424 }
425
426 bool CreditCardEditorViewController::CreditCardValidationDelegate::
427 ComboboxValueChanged(views::Combobox* combobox) {
428 base::string16 error_message;
429 bool is_valid = ValidateCombobox(combobox, nullptr);
430 controller_->DisplayErrorMessageForField(field_, error_message);
431 return is_valid;
432 }
433
434 bool CreditCardEditorViewController::CreditCardValidationDelegate::
435 ValidateValue(const base::string16& value, base::string16* error_message) {
436 if (!value.empty()) {
437 base::string16 local_error_message;
438 bool is_valid =
439 field_.type == autofill::CREDIT_CARD_NUMBER
440 ? autofill::IsValidCreditCardNumberForBasicCardNetworks(
441 value, supported_card_networks_, &local_error_message)
442 : autofill::IsValidForType(value, field_.type,
443 &local_error_message);
444 if (error_message)
445 *error_message = local_error_message;
446 return is_valid;
447 }
448
449 if (error_message && field_.required) {
450 *error_message = l10n_util::GetStringUTF16(
451 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
452 }
453 return !field_.required;
454 }
455
456 bool CreditCardEditorViewController::CreditCardValidationDelegate::
457 ValidateCombobox(views::Combobox* combobox, base::string16* error_message) {
413 // The billing address ID is the selected item identifier and not the combobox 458 // The billing address ID is the selected item identifier and not the combobox
414 // value itself. 459 // value itself.
415 if (field_.type == kBillingAddressType) { 460 if (field_.type == kBillingAddressType) {
416 // TODO(crbug.com/718905) Find a way to deal with existing incomplete 461 // TODO(crbug.com/718905) Find a way to deal with existing incomplete
417 // addresses when choosing them as billing addresses. 462 // addresses when choosing them as billing addresses.
418 autofill::AddressComboboxModel* model = 463 autofill::AddressComboboxModel* model =
419 static_cast<autofill::AddressComboboxModel*>(combobox->model()); 464 static_cast<autofill::AddressComboboxModel*>(combobox->model());
420 return !model->GetItemIdentifierAt(combobox->selected_index()).empty(); 465 if (model->GetItemIdentifierAt(combobox->selected_index()).empty()) {
466 if (error_message) {
467 *error_message =
468 l10n_util::GetStringUTF16(IDS_PAYMENTS_BILLING_ADDRESS_REQUIRED);
469 }
470 return false;
471 }
472 return true;
421 } 473 }
422 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); 474 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()),
423 } 475 error_message);
424
425 bool CreditCardEditorViewController::CreditCardValidationDelegate::
426 ValidateValue(const base::string16& value) {
427 if (!value.empty()) {
428 base::string16 error_message;
429 bool is_valid =
430 field_.type == autofill::CREDIT_CARD_NUMBER
431 ? autofill::IsValidCreditCardNumberForBasicCardNetworks(
432 value, supported_card_networks_, &error_message)
433 : autofill::IsValidForType(value, field_.type, &error_message);
434 controller_->DisplayErrorMessageForField(field_, error_message);
435 return is_valid;
436 }
437
438 bool is_required_valid = !field_.required;
439 const base::string16 displayed_message =
440 is_required_valid ? base::ASCIIToUTF16("")
441 : l10n_util::GetStringUTF16(
442 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE);
443 controller_->DisplayErrorMessageForField(field_, displayed_message);
444 return is_required_valid;
445 } 476 }
446 477
447 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) { 478 bool CreditCardEditorViewController::GetSheetId(DialogViewID* sheet_id) {
448 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET; 479 *sheet_id = DialogViewID::CREDIT_CARD_EDITOR_SHEET;
449 return true; 480 return true;
450 } 481 }
451 482
452 } // namespace payments 483 } // namespace payments
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698