Show hidden field validation message along main error message#2084
Show hidden field validation message along main error message#2084AbdiTolesa wants to merge 2 commits into
Conversation
WalkthroughThe changes introduce a new public method named Changes
Possibly related PRs
Suggested labels
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (2)
classes/models/fields/FrmFieldHidden.php (2)
59-64: Update PHPDoc version and parameter documentation.The PHPDoc block needs improvement:
- Replace placeholder version "x.x" with actual version
- Document the expected structure of the $args parameter
- Document the meaning of the empty array return value
/** - * @since x.x + * @since 5.0.0 * - * @param array $args + * @param array $args { + * Validation arguments. + * @type mixed $value The field value to validate + * @type array $errors Current validation errors + * } - * @return array + * @return array Empty array as validation errors are handled via filter */
68-70: Add localization support for error message concatenation.The error message concatenation should support translation for proper internationalization.
- return $msg . ' ' . $error; + /* translators: 1: Main error message, 2: Additional error details */ + return sprintf( __( '%1$s %2$s', 'formidable' ), $msg, $error );
| public function validate( $args ) { | ||
| $error = FrmFieldsHelper::get_error_msg( $this->field, 'unique_msg' ); | ||
| if ( ! empty( $error ) ) { | ||
| add_filter( 'frm_invalid_error_message', function( $msg ) use ( $error ) { | ||
| return $msg . ' ' . $error; | ||
| } ); | ||
| } | ||
| return array(); | ||
| } |
There was a problem hiding this comment.
💡 Codebase verification
The review comment's concerns are valid but need adjustment
The parent class FrmFieldType has a basic validate() method that returns an empty array, and other field types like FrmFieldText, FrmFieldEmail, etc., properly handle validation by:
- Returning specific error arrays with keys in format
field{id} - Not using global filters for field-specific validation
- Some even call
parent::validate($args)first
The suggested fix is good but needs refinement:
- Keep the field-specific error format (
field{id}) for consistency - Remove the filter approach entirely as other field types don't use it
- Return validation errors directly
public function validate( $args ) {
$errors = array();
$error = FrmFieldsHelper::get_error_msg( $this->field, 'unique_msg' );
if ( ! empty( $error ) ) {
- add_filter( 'frm_invalid_error_message', function( $msg ) use ( $error ) {
- return $msg . ' ' . $error;
- } );
+ $errors['field' . $this->field->id] = $error;
}
- return array();
+ return $errors;
}🔗 Analysis chain
Fix potential memory leak and improve error handling.
The current implementation has several issues:
- The anonymous function is added to the filter but never removed
- The filter name 'frm_invalid_error_message' suggests global scope which could affect other validations
- The $args parameter is unused
- The return value doesn't reflect validation status
Consider this improved implementation:
public function validate( $args ) {
+ $errors = array();
$error = FrmFieldsHelper::get_error_msg( $this->field, 'unique_msg' );
if ( ! empty( $error ) ) {
- add_filter( 'frm_invalid_error_message', function( $msg ) use ( $error ) {
- return $msg . ' ' . $error;
- } );
+ $filter_name = 'frm_invalid_error_message_' . $this->field->id;
+ add_filter( $filter_name, function( $msg ) use ( $error ) {
+ remove_filter( current_filter(), __FUNCTION__ );
+ return $msg . ' ' . $error;
+ } );
+ $errors['field' . $this->field->id] = $error;
}
- return array();
+ return $errors;
}This solution:
- Uses a field-specific filter name to avoid global conflicts
- Removes the filter after first use
- Returns validation errors properly
Let's verify if the parent class has validation logic we should consider:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for validate method in parent class
ast-grep --pattern 'class FrmFieldType {
$$$
validate($$$) {
$$$
}
$$$
}'
Length of output: 87
Script:
#!/bin/bash
# Let's try a broader search for the parent class and validation method
rg -l "class FrmFieldType" --type php
# Then search for validate method in any class
rg "function validate\(" --type php -A 5
Length of output: 6814
|
This should really be in Pro. |
No description provided.