-
Notifications
You must be signed in to change notification settings - Fork 42
Give email/url/phone/number fields their own invalid-message copy #3495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ec94d76
5924625
b742211
308bf87
fecffd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -313,4 +313,67 @@ public function test_get_error_msg() { | |
| $error_message = FrmFieldsHelper::get_error_msg( $field, 'unique_msg' ); | ||
| $this->assertSame( 'My example field must be unique', $error_message ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
| * @covers FrmFieldsHelper::get_error_msg | ||
| * @covers FrmFieldsHelper::default_invalid_msg | ||
| */ | ||
| public function test_get_error_msg_invalid_is_field_type_specific() { | ||
| $form_id = $this->factory->form->create(); | ||
|
|
||
| // Email, url, phone, number, and quantity fields get their own corrective message | ||
| // when no custom one is set; a type with no specific copy (text) keeps the | ||
| // original generic message. | ||
| $tests = array( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking coverage gap: this loop asserts array(
'type' => 'url',
'name' => 'Website',
'expected' => 'Website is invalid. Enter a valid web address, like https://example.com',
),
array(
'type' => 'phone',
'name' => 'Cell',
'expected' => 'Cell is invalid. Enter a valid phone number',
),
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added both url and phone tuples as suggested, plus quantity for the fix above. All six cases (email, url, phone, number, quantity, text) now covered. |
||
| array( | ||
| 'type' => 'email', | ||
| 'name' => 'Email', | ||
| 'expected' => 'Enter a valid email address, like name@example.com', | ||
| ), | ||
| array( | ||
| 'type' => 'url', | ||
| 'name' => 'Website', | ||
| 'expected' => 'Enter a valid web address, like https://example.com', | ||
| ), | ||
| array( | ||
| 'type' => 'phone', | ||
| 'name' => 'Cell', | ||
| 'expected' => 'Enter a valid phone number', | ||
| ), | ||
| array( | ||
| 'type' => 'number', | ||
| 'name' => 'Age', | ||
| 'expected' => 'Enter a number', | ||
| ), | ||
| array( | ||
| 'type' => 'quantity', | ||
| 'name' => 'Amount', | ||
| 'expected' => 'Enter a number', | ||
| ), | ||
| array( | ||
| 'type' => 'text', | ||
| 'name' => 'Comment', | ||
| 'expected' => 'Comment is invalid', | ||
| ), | ||
| ); | ||
|
|
||
| foreach ( $tests as $test ) { | ||
| $field = $this->factory->field->create_and_get( | ||
| array( | ||
| 'name' => $test['name'], | ||
| 'form_id' => $form_id, | ||
| 'type' => $test['type'], | ||
| ) | ||
| ); | ||
|
|
||
| $error_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); | ||
| $this->assertSame( $test['expected'], $error_message ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| // A custom message saved on the field is never overridden by the type-specific default. | ||
| $field->field_options['invalid'] = 'Please fix [field_name]'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| $error_message = FrmFieldsHelper::get_error_msg( $field, 'invalid' ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( 'Please fix Comment', $error_message ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking, architecture-fit note: this hardcodes a type-string → message array in the helper, but the codebase already has a polymorphic mechanism for exactly this shape (
get_default_html( $type )a bit further down this same class callsFrmFieldFactory::get_field_type( $type )->default_html(), overridden per field-type subclass). Putting the type-specific message onFrmFieldEmail/FrmFieldUrl/FrmFieldPhone/FrmFieldNumberas an overridable method (generic fallback on the baseFrmFieldType) would reach any field type registered via the existingfrm_get_field_type_classfilter automatically, including ones this repo doesn't define. Not asking for a rewrite here, just flagging it as the more extensible shape for next time this needs a 5th type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on the extensibility shape, leaving as-is for this PR per your own note (not asking for a rewrite). Filed as a follow-up idea if a 5th type ever needs this.