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/shipping_address_editor_view_controll
er.h" | 5 #include "chrome/browser/ui/views/payments/shipping_address_editor_view_controll
er.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 LOG_IF(ERROR, !success && !ignore_errors) | 398 LOG_IF(ERROR, !success && !ignore_errors) |
399 << "Can't set profile country to: " << country; | 399 << "Can't set profile country to: " << country; |
400 if (!success && !ignore_errors) | 400 if (!success && !ignore_errors) |
401 return false; | 401 return false; |
402 } else { | 402 } else { |
403 DCHECK_EQ(temporary_profile_.get(), profile); | 403 DCHECK_EQ(temporary_profile_.get(), profile); |
404 } | 404 } |
405 | 405 |
406 bool success = true; | 406 bool success = true; |
407 for (const auto& field : text_fields()) { | 407 for (const auto& field : text_fields()) { |
408 // Force a blur in case the value was left untouched. | |
409 field.first->OnBlur(); | |
410 // ValidatingTextfield* is the key, EditorField is the value. | 408 // ValidatingTextfield* is the key, EditorField is the value. |
411 if (field.first->invalid()) { | 409 if (field.first->IsValid()) { |
412 success = false; | |
413 } else { | |
414 success = profile->SetInfo(autofill::AutofillType(field.second.type), | 410 success = profile->SetInfo(autofill::AutofillType(field.second.type), |
415 field.first->text(), locale); | 411 field.first->text(), locale); |
| 412 } else { |
| 413 success = false; |
416 } | 414 } |
417 LOG_IF(ERROR, !success && !ignore_errors) | 415 LOG_IF(ERROR, !success && !ignore_errors) |
418 << "Can't setinfo(" << field.second.type << ", " << field.first->text(); | 416 << "Can't setinfo(" << field.second.type << ", " << field.first->text(); |
419 if (!success && !ignore_errors) | 417 if (!success && !ignore_errors) |
420 return false; | 418 return false; |
421 } | 419 } |
422 for (const auto& field : comboboxes()) { | 420 for (const auto& field : comboboxes()) { |
423 // ValidatingCombobox* is the key, EditorField is the value. | 421 // ValidatingCombobox* is the key, EditorField is the value. |
424 ValidatingCombobox* combobox = field.first; | 422 ValidatingCombobox* combobox = field.first; |
425 // The country has already been dealt with. | 423 // The country has already been dealt with. |
426 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) | 424 if (combobox->id() == autofill::ADDRESS_HOME_COUNTRY) |
427 continue; | 425 continue; |
428 if (combobox->invalid()) { | 426 if (combobox->IsValid()) { |
429 success = false; | |
430 } else { | |
431 success = profile->SetInfo( | 427 success = profile->SetInfo( |
432 autofill::AutofillType(field.second.type), | 428 autofill::AutofillType(field.second.type), |
433 combobox->GetTextForRow(combobox->selected_index()), locale); | 429 combobox->GetTextForRow(combobox->selected_index()), locale); |
| 430 } else { |
| 431 success = false; |
434 } | 432 } |
435 LOG_IF(ERROR, !success && !ignore_errors) | 433 LOG_IF(ERROR, !success && !ignore_errors) |
436 << "Can't setinfo(" << field.second.type << ", " | 434 << "Can't setinfo(" << field.second.type << ", " |
437 << combobox->GetTextForRow(combobox->selected_index()); | 435 << combobox->GetTextForRow(combobox->selected_index()); |
438 if (!success && !ignore_errors) | 436 if (!success && !ignore_errors) |
439 return false; | 437 return false; |
440 } | 438 } |
441 return success; | 439 return success; |
442 } | 440 } |
443 | 441 |
(...skipping 21 matching lines...) Expand all Loading... |
465 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 463 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
466 ShippingAddressValidationDelegate( | 464 ShippingAddressValidationDelegate( |
467 ShippingAddressEditorViewController* controller, | 465 ShippingAddressEditorViewController* controller, |
468 const EditorField& field) | 466 const EditorField& field) |
469 : field_(field), controller_(controller) {} | 467 : field_(field), controller_(controller) {} |
470 | 468 |
471 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 469 ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
472 ~ShippingAddressValidationDelegate() {} | 470 ~ShippingAddressValidationDelegate() {} |
473 | 471 |
474 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 472 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
475 ValidateTextfield(views::Textfield* textfield) { | 473 IsValidTextfield(views::Textfield* textfield) { |
476 return ValidateValue(textfield->text()); | 474 return ValidateValue(textfield->text(), nullptr); |
477 } | 475 } |
478 | 476 |
479 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 477 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
480 ValidateCombobox(views::Combobox* combobox) { | 478 IsValidCombobox(views::Combobox* combobox) { |
481 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); | 479 return ValidateValue(combobox->GetTextForRow(combobox->selected_index()), |
| 480 nullptr); |
| 481 } |
| 482 |
| 483 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 484 TextfieldValueChanged(views::Textfield* textfield) { |
| 485 base::string16 error_message; |
| 486 bool is_valid = ValidateValue(textfield->text(), &error_message); |
| 487 controller_->DisplayErrorMessageForField(field_, error_message); |
| 488 return is_valid; |
| 489 } |
| 490 |
| 491 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
| 492 ComboboxValueChanged(views::Combobox* combobox) { |
| 493 base::string16 error_message; |
| 494 bool is_valid = ValidateValue( |
| 495 combobox->GetTextForRow(combobox->selected_index()), &error_message); |
| 496 controller_->DisplayErrorMessageForField(field_, error_message); |
| 497 return is_valid; |
482 } | 498 } |
483 | 499 |
484 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 500 void ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
485 ComboboxModelChanged(views::Combobox* combobox) { | 501 ComboboxModelChanged(views::Combobox* combobox) { |
486 controller_->OnComboboxModelChanged(combobox); | 502 controller_->OnComboboxModelChanged(combobox); |
487 } | 503 } |
488 | 504 |
489 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: | 505 bool ShippingAddressEditorViewController::ShippingAddressValidationDelegate:: |
490 ValidateValue(const base::string16& value) { | 506 ValidateValue(const base::string16& value, base::string16* error_message) { |
491 if (!value.empty()) { | 507 if (!value.empty()) { |
492 if (field_.type == autofill::PHONE_HOME_WHOLE_NUMBER && | 508 if (field_.type == autofill::PHONE_HOME_WHOLE_NUMBER && |
493 controller_->chosen_country_index_ < controller_->countries_.size() && | 509 controller_->chosen_country_index_ < controller_->countries_.size() && |
494 !autofill::IsValidPhoneNumber( | 510 !autofill::IsValidPhoneNumber( |
495 value, controller_->countries_[controller_->chosen_country_index_] | 511 value, controller_->countries_[controller_->chosen_country_index_] |
496 .first)) { | 512 .first)) { |
497 controller_->DisplayErrorMessageForField( | 513 if (error_message) { |
498 field_, l10n_util::GetStringUTF16( | 514 *error_message = l10n_util::GetStringUTF16( |
499 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE)); | 515 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE); |
| 516 } |
500 return false; | 517 return false; |
501 } | 518 } |
502 // As long as other field types are non-empty, they are valid. | 519 // As long as other field types are non-empty, they are valid. |
503 controller_->DisplayErrorMessageForField(field_, base::ASCIIToUTF16("")); | |
504 return true; | 520 return true; |
505 } | 521 } |
506 bool is_required_valid = !field_.required; | 522 if (error_message && field_.required) { |
507 const base::string16 displayed_message = | 523 *error_message = l10n_util::GetStringUTF16( |
508 is_required_valid ? base::ASCIIToUTF16("") | 524 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
509 : l10n_util::GetStringUTF16( | 525 } |
510 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); | 526 return !field_.required; |
511 controller_->DisplayErrorMessageForField(field_, displayed_message); | |
512 return is_required_valid; | |
513 } | 527 } |
514 | 528 |
515 } // namespace payments | 529 } // namespace payments |
OLD | NEW |