Help with Adding Error Message on Form
-
I’m working with the profile information form, and I’m trying to add a link to the error message. I tried pretty hard and havn’t been able to do it, and I havn’t found anything online relating to this.
I’m trying to make the add_error function output an error message with a link like this.
UM()->form()->add_error( $key, __( “Toll-Free # Not Added, Click Here to add to your profile.”, ‘ultimate-member’ ) );
Any Ideas?
-
Yes UM is calling esc_attr for all error texts.
Look at the
function field_errorin.../ultimate-member/includes/core/class-fields.phplines 479/502Maybe you should redirect the user to this page instead of displaying the link?
-
This reply was modified 3 years, 5 months ago by
missveronica.
Is there any way you can make a change to that function to give it the option to bypass that esc_attr for custom options like this. I’m sure im not the first and I definitely won’t be the last. Forwarding is not an option because it would feel forceful and unexpected to the user.
I would do it on my end, but then it would be rewritten the next update.
Mayble replace esc_attr with wp_kses_post to allow for some basic html usage when the option is selected.
-
This reply was modified 3 years, 5 months ago by
baddon250.
You can try to add this filter to your action code snippet:
.../ultimate-member/includes/core/class-fields.phpin line 3906:$output = apply_filters( "um_{$key}_form_edit_field", $output, $this->set_mode );Where
$keyis your field key and$outputis the current HTML.
Replace the link in your action code snippet with a placeholder like{add_on_not_enabled}which you replace with your link in this filter code snippet.Do you think you could alter those 2 functions to be replaced with these 2 functions?
/** * Print field error * * @param string $text * @param bool $force_show * * @return string */ function field_error( $text, $force_show = false, $allow_html = false ) { if ( empty( $text ) ) { return ''; } if($allow_html){ $data = wp_kses_post($text); } else{ $data = esc_attr($text); } if ( $force_show ) { $output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; return $output; } if ( isset( $this->set_id ) && UM()->form()->processing == $this->set_id ) { $output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; } else { $output = ''; } if ( ! UM()->form()->processing ) { $output = '<div class="um-field-error"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; } return $output; } /** * Print field notice * * @param string $text * @param bool $force_show * * @return string */ function field_notice( $text, $force_show = false, $allow_html = false ) { if ( empty( $text ) ) { return ''; } if($allow_html){ $data = wp_kses_post($text); } else{ $data = esc_attr($text); } if ( $force_show ) { $output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; return $output; } if ( isset( $this->set_id ) && UM()->form()->processing == $this->set_id ) { $output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; } else { $output = ''; } if ( ! UM()->form()->processing ) { $output = '<div class="um-field-notice"><span class="um-field-arrow"><i class="um-faicon-caret-up"></i></span>' . $data . '</div>'; } return $output; }You’re probably going to end up doing something like this anyways, so maybe its a good time, because theres a lot of companies that run a SAAS busines model like we do, and one of our products resides on displaying certain fields on profiles.
No solution for UM is like my post above with a filter after the field_error function being called.
What do I do with $this->set_mode.
Keep in mind I will have multiple different links with the associated fields below.
One for Website.
One for Phone Number
One for Toll-Free Phone Number
One Sales Icons
One for Gallery Sorting
If I could run something inside the given functions I have, that would be best.
Here’s the function I’m working with. With $output being the error with link.
function um_custom_validate_website( $key, $array, $args ) { //$val = $args[$key]; $keys = array('page_url'); //website link 37081 if(!isset($args[$key]) || empty($args[$key])){ } else{ if(is_active_subscription(37081)){//37082 = toll-free-number } else{ $link = get_site_url(). '/product/toll-free-phone-number'; //my_submit_form_error('Add-On Not Enabled, <a href="'. $link .'">CLICK HERE</a> to to get the Add-On.', $key); $output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.'; UM()->form()->add_error( $key, __( "Add-On Not Enabled, Go to your Profile Add-Ons on your Profile Menu to add.", 'ultimate-member' ) ); } } } add_action( 'um_custom_field_validation_validate_website', 'um_custom_validate_website', 30, 3 );$this->set_modecontains the form type like register, login, password, profile, accountYes replace
$this->set_modewith$modein your filter function parameter list.The php error went away, but it’s not showing the updated error message for the user
-
This reply was modified 3 years, 5 months ago by
baddon250.
That code is not making the url show on the error message. Here is my code.
function um_custom_validate_website( $key, $array, $args ) { //$val = $args[$key]; $keys = array('page_url'); //website link 37081 if(!isset($args[$key]) || empty($args[$key])){ } else{ if(is_active_subscription(37081)){//37082 = toll-free-number } else{ $link = get_site_url(). '/product/toll-free-phone-number'; //my_submit_form_error('Add-On Not Enabled, <a href="'. $link .'">CLICK HERE</a> to to get the Add-On.', $key); $output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.'; UM()->form()->add_error( $key, __( "Add-On Not Enabled, Go to your Profile Add-Ons on your Profile Menu to add.", 'ultimate-member' ) ); } } } $link = get_site_url(). '/product/toll-free-phone-number'; $output = 'Add-On Not Enabled, Please go <b><a href="' . $link . '">HERE</a></b> to enable.'; $key = "user_url"; add_action( 'um_custom_field_validation_validate_website', 'um_custom_validate_website', 30, 3 ); apply_filters( "um_{$key}_form_edit_field", $output, $mode );You have not followed the code example in the documentation for the filter function:
/** * UM hook * * @type filter * @title um_{$key}_form_edit_field * @description Change field HTML on edit mode by field $key * @input_vars * [{"var":"$output","type":"string","desc":"Field HTML"}, * {"var":"$mode","type":"string","desc":"Fields Mode"}] * @change_log * ["Since: 2.0"] * @usage add_filter( 'um_{$key}_form_edit_field', 'function_name', 10, 2 ); * @example * <?php * add_filter( 'um_{$key}_form_edit_field', 'my_form_edit_field', 10, 2 ); * function my_form_edit_field( $output, $mode ) { * // your code here * return $output; * } * ?> */ -
This reply was modified 3 years, 5 months ago by
The topic ‘Help with Adding Error Message on Form’ is closed to new replies.