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