• Resolved theabsurdman

    (@theabsurdman)


    I’ve added a custom field “wherefrom” using the instructions provided here:
    https://pastebin.com/4fvKErcp

    add_filter('site-reviews/config/forms/review-form', function ($config) {
        $config['wherefrom'] = [
            'label' => __('Where are you from?', 'your_theme_domain'),
            'placeholder' => __('Enter home location', 'your_theme_domain'),
            'required' => true,
            'type' => 'text',
        ];
        return $config;
    });

    It’s all working except that the “required” attribute is NOT being respected for the new field.

    I’ve tried setting up my own validation using the following hook:

    add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
        // return true on success
        // or return false on failure
        // or return a custom error message string on failure
    
        glsr_log($var = $requestData);
    
        return $isValid;
    }, 10, 2);

    The console returns the following for object $requestData:

    [2021-01-12 12:56:26] DEBUG [/themes/x-child/functions.php:321] GeminiLabs\SiteReviews\Request Object
    (
        [storage:ArrayObject:private] => Array
            (
                [assign_to] => 
                [category] => 
                [content] => sdf
                [email] => [email protected]
                [form_id] => glsr_6022cc42
                [ip_address] => 81.110.195.45
                [name] => asdasd
                [rating] => 5
                [terms] => 1
                [title] => sdf
                [6389be83] => 
                [_action] => submit-review
                [_counter] => 0
                [_nonce] => 3fa969d548
                [_post_id] => 6039
                [_referer] => 
                [assigned_posts] => 6039
                [assigned_terms] => 
                [assigned_users] => 
                [excluded] => 
                [arrivaldate] => 2021-01-12
                [wherefrom] => worthing
                [_ajax_request] => 1
                [_recaptcha-token] => 
            )
    )

    But I can’t seem to access the values in the object, whatever I try, eg.

    glsr_log($var = $requestData['storage']['wherefrom']) //<-- returns nothing
    glsr_log($var = $requestData->storage->wherefrom) //<-- returns nothing

    How do I get those values? Or at least get the new custom field to be “required”?

    • This topic was modified 4 years, 1 month ago by Yui.
    • This topic was modified 4 years, 1 month ago by theabsurdman.
    • This topic was modified 4 years, 1 month ago by theabsurdman.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    First of all, adding custom fields is not actively supported (as shown at the top of the pastebin link). However, I will briefly address your questions here.

    1. I pasted the “site-reviews/config/forms/review-form” hook you provided and the validation works for me:

    2. Access the values like this:

    glsr_log($requestData->wherefrom)

    or this:

    glsr_log($requestData['wherefrom'])

    NOT this:

    glsr_log($requestData['storage']['wherefrom'])

    Thread Starter theabsurdman

    (@theabsurdman)

    Great! I appreciate the rapid response. I can read the $requestData values now, thanks.

    Still not getting my custom field as “required” though, I get this:

    <input class="glsr-input glsr-input-text" id="site-reviews-wherefrom-glsr_6022cc42" name="site-reviews[wherefrom]" type="text" placeholder="Enter home location" required="" value="">

    Strange! I might have to set it with jQuery if all else fails!

    One more question – how does the validation hook return “a custom error message string on failure”?

    /**
     * Perform custom validation here if needed
     * Paste this in your active theme's functions.php file.
     * @param bool $isValid
     * @param array $requestData
     * @return bool|string
     */
    add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
        // return true on success
        // or return false on failure
        // or return a custom error message string on failure
    	
    	glsr_log($requestData['wherefrom']);
    	
        return $isValid;
    }, 10, 2);

    Thanks again.

    • This reply was modified 4 years, 1 month ago by theabsurdman.
    • This reply was modified 4 years, 1 month ago by theabsurdman.
    Plugin Author Gemini Labs

    (@geminilabs)

    Can you share the URL of the page that has the form?

    Plugin Author Gemini Labs

    (@geminilabs)

    One more question – how does the validation hook return “a custom error message string on failure”?

    if (empty($requestData->wherefrom)) {
        return 'Please enter your location';
    }
    Thread Starter theabsurdman

    (@theabsurdman)

    Can’t really post the URL as the site is in development and we don’t want it spidered yet.

    Could I email it to you privately?

    Plugin Author Gemini Labs

    (@geminilabs)

    Sure, use the Contact Support section on the help page.

    Plugin Author Gemini Labs

    (@geminilabs)

    Site Reviews uses two methods of form validation:

    1. Client-side validation using javascript.
    2. Server-side validation. This is done before creating the review.

    When the javascript file is loaded, a novalidate attribute is added to the form. This disables the browser validation and allows Site Reviews to use its own validation instead.

    However, the javascript validation of the form is not initialising completely because the form is in an accordion and hidden on page load.

    The “required” attributes on the form fields do nothing because of the “novalidate” attribute on the form, but since the Site Reviews javascript validation is not initialised, the form is submitted and falls back to server-side validation.

    You can use the “site-reviews/validate/custom” hook to perform server-side validation of custom fields.

    To fix the javascript validation, you will need to run new GLSR.Form(); somewhere in your javascript when or after the review form is displayed (“GLSR” is a global javascript variable).

    Thread Starter theabsurdman

    (@theabsurdman)

    Great support! I think that fixes all my problems, thanks again.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘how to validate custom fields?’ is closed to new replies.