PDA

View Full Version : How can I check a custom option before saving?


EquinoxWorld
08-15-2011, 11:42 AM
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

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
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

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
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
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
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;


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:

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).

EquinoxWorld
08-16-2011, 02:53 AM
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:

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).

Thank you so much for your reply! I did just what you suggested and it all works PERFECT. If the user set the first option to "yes" then the following three are required to be entered if the user does not enter them (before saving) the missing field error appears automatically (because of the validation code), if the user still tries to save (after a missing fields warning popup) the error message from the plugin appears and nothing saves and the user is redirected back to the option group! Truly idiot proof. :D This is what I used for future references (replace params with your own if using this code):

Plugin:

Hook: admin_options_processing


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


if (empty($settings['cotw_sotw_first_place_prize'])) {
define('CP_REDIRECT', 'options.php?do=options&dogroup=cotw_sotw_options' . $vbulletin->GPC['grouptitle']);
print_stop_message('cotw_missing_param', $vbulletin->GPC['title']);
if (empty($settings['cotw_sotw_second_place_prize'])) {
define('CP_REDIRECT', 'options.php?do=options&dogroup=cotw_sotw_options' . $vbulletin->GPC['grouptitle']);
print_stop_message('cotw_missing_param', $vbulletin->GPC['title']);
if (empty($settings['cotw_sotw_third_place_prize'])) {
define('CP_REDIRECT', 'options.php?do=options&dogroup=cotw_sotw_options' . $vbulletin->GPC['grouptitle']);
print_stop_message('cotw_missing_param', $vbulletin->GPC['title']);
}
}
}

}


Validation code for each option:


global $vbulletin;

if ($vbulletin->options['cotw_sotw_pay_credits'] == 1) {

if (empty($data)) {



$error = 'invalid_value';
return false;

}
else {

return true;
}
}
else {
return true;
}


Thank you so much for your help kh99 and nhawk. Couldn't have asked for more. :up:

kh99
08-16-2011, 03:12 AM
Thank you so much for your reply! I did just what you suggested and it all works PERFECT.

lol...I'm glad you got it working because I'm not even sure what I suggested :) I think you really have nhawk to thank.

EquinoxWorld
08-16-2011, 03:15 AM
lol...I'm glad you got it working because I'm not even sure what I suggested :) I think you really have nhawk to thank.

We all put in to get this one resolved I think. nhawk has been also helping me out for some time now, he knows my gratitude is immense. Hope it helps someone else at some point, really glad to be a part of this community, collective thinking is the way to go my friends.

nhawk
08-16-2011, 11:12 AM
Glad it worked out. I thought it would :D

FYI.. that same hook can be used to process custom checkboxes, or any other data that is customized and can't automatically be processed in a settings group.

EquinoxWorld
08-16-2011, 11:23 AM
Glad it worked out. I thought it would :D

FYI.. that same hook can be used to process custom checkboxes, or any other data that is customized and can't automatically be processed in a settings group.

Thanks nhawk. I'll keep that in mind. I will actually implement that next when I try add some options for moderators outside of the admin panel. Should be interesting ;)