vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   How can I check a custom option before saving? (https://vborg.vbsupport.ru/showthread.php?t=268571)

EquinoxWorld 08-15-2011 11:42 AM

How can I check a custom option before saving?
 
Hello everyone, I was wondering if someone could please help me in a concern I am having with one of my custom options I created. Allow me to explain;

I have the following options:

Option A: yes no

Option A1: blah1

Option A2: blah2

Option A3: blah3

I want to be able to force the user to fill out options A1, A2, and A3 if they click yes for option A. I have an option that if turned on or selected yes, 3 other options have to be filled out for the option to execute correctly so I don't want the user to be able to save if the other 3 options have not been filled out yet. Is there any way I can get this done? I would imagine I can use a code in the Validation php code filed , some sort of if condition maybe? I am just not entirely sure how to go about it. If anyone can shed any light on this matter or can provide any more info it would be very much appreciated as always. Thanks for your time everyone.

kh99 08-15-2011 01:39 PM

If you create a plugin using hook profile_updateoptions, you can check the values and if they aren't correct, call eval(standard_error()), like

PHP Code:

if (some check for values fails)
    eval(
standard_error("Some error message")); 


Look in profile.php, search for fetch_hook('profile_updateoptions') (around line 2411) to see how the values are stored.

EquinoxWorld 08-15-2011 02:24 PM

Quote:

Originally Posted by kh99 (Post 2233769)
If you create a plugin using hook profile_updateoptions, you can check the values and if they aren't correct, call eval(standard_error()), like

PHP Code:

if (some check for values fails)
    eval(
standard_error("Some error message")); 


Look in profile.php, search for fetch_hook('profile_updateoptions') (around line 2411) to see how the values are stored.

Thank you so much. Sounds easy enough. :) One question though what is the purpose of the area for PHP Validation Code where you set the options? Thanks anyway for your help. I'll try it out and let you know how it went.

kh99 08-15-2011 02:39 PM

hmm...validation code? Maybe I misunderstood and we're talking about different kinds of options.

EquinoxWorld 08-15-2011 02:43 PM

Quote:

Originally Posted by kh99 (Post 2233796)
hmm...validation code? Maybe I misunderstood and we're talking about different kinds of options.

I was talking about the options (custom options) you can create when in debug mode from the admin panel under "Settings".

kh99 08-15-2011 02:56 PM

Oh...my mistake, I thought you meant custom profile fields. So what I posted above is useless. I don't have time right now but I'll look in to it later if you don't have an answer yet.

EquinoxWorld 08-15-2011 03:11 PM

Quote:

Originally Posted by kh99 (Post 2233807)
Oh...my mistake, I thought you meant custom profile fields. So what I posted above is useless. I don't have time right now but I'll look in to it later if you don't have an answer yet.

xD Thanks kh99, it's ok. Please let me know if you find an answer when you have a chance, you know where to find me :) .

nhawk 08-15-2011 08:17 PM

Personally I think you're best off using a plugin to check values and throw the error.

Hook the plugin to admin_options_processing and compare $settings['your_setting_name'] and throw the error if it's not correct or empty.

I don't know if that's the absolute correct hook, but I think it will work. Otherwise, you have to intercept $data before it's written to the settings database.

EquinoxWorld 08-15-2011 11:11 PM

Quote:

Originally Posted by nhawk (Post 2233892)
Personally I think you're best off using a plugin to check values and throw the error.

Hook the plugin to admin_options_processing and compare $settings['your_setting_name'] and throw the error if it's not correct or empty.

I don't know if that's the absolute correct hook, but I think it will work. Otherwise, you have to intercept $data before it's written to the settings database.

Hey nhawk, thanks for your reply. I tried something like what you suggested;

PHP Code:

if ($settings['cotw_sotw_pay_credits']  == 1)  {


    if (empty(
$settings['cotw_sotw_first_place_prize'])) {
            
$error 'invalid_value';
                    return 
false
        if (empty(
$settings['your_setting_name'])) {
                 
$error 'invalid_value';
                         return 
false
            if (empty(
$settings['your_setting_name'])) {
                                
$error 'invalid_value';
                                        return 
false;  
             }
         }
   }
    


When the bottom 3 options are empty (or any of the three for that matter), this will still save the options but give them value of "0" if they were empty when the option was saved. That's better than saving it without any value at all I suppose. Would much rather see the error on the field it's missing, I'm doing something wrong in this code because the error does not print out anywhere on the page, just saves and reloads. The good part is that I know now how to control these options and it's requirements to save and such, that hook you mentioned works just fine for my purposes btw, now it's just a matter of printing the error in the missing field(s). Anyone have any ideas? :erm:

kh99 08-15-2011 11:54 PM

What you have is probably the best way to keep invalid values from being saved. The PHP Validation code will allow you to check a value and print a message under it if it's invalid, but it will still be saved (maybe you knew that already and that's why you created the thread).

What you might be able to do in your plugin is print an error message then redirect back to the option page like:

PHP Code:

define('CP_REDIRECT''options.php?do=options&dogroup=' $vbulletin->GPC['grouptitle']);
print_stop_message('error_phrase'$vbulletin->GPC['title']); 


(You'd want to use a real error phrase name, but I'm not sure of the other parameters you'd want). I don't think there's any easy way to get the error message to display under the value unless you let it save the invalid values (that is a reasonable option - you could check the values the mod code and use defaults if they're invalid, put in PHP validation code for your fields, and then whenever someone goes to the adminCP they'll see the error message).


All times are GMT. The time now is 11:22 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01052 seconds
  • Memory Usage 1,757KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete