wolfshaven Posted October 2, 2009 Share Posted October 2, 2009 I have received excellent help here with my SMF Forms Mod recently (thank you again by the way) , so I'm posing another question to you guys...One of my user presented me with a interesting question about functions that the original author put in the mod that only allows the user to enter a integer or float using input type="text".Currently the checks are done using... $value = isset($_REQUEST[$field['title']]) ? intval($_REQUEST[$field['title']]) : ''; and $value = isset($_REQUEST[$field['title']]) ? floatval($_REQUEST[$field['title']]) : ''; However as one of my users discovered if the number 0 is entered by itself nothing gets passed to the forum post for that field instead of passing the answer of "0".Doing some research it looks like the problem is caused by the "intval" and "floatval" functions, apparently they don't like 0 by itself. Also if a user enters 01234 it post to the forum as 1234 dropping the 0So my question is, is there a better way to handle this? Link to comment Share on other sites More sharing options...
chibineku Posted October 2, 2009 Share Posted October 2, 2009 Well, that leading 0 tends to be useless, but you could try my screwball stupid idea and use a regex (there will be better answers coming) to ignore leading 0s, intval the remainder, and add the leading 0s back on:$value = isset($_REQUEST[$field['title']]) ? preg_replace("/([0]+)(.*)/",$1.intval($2)",$_REQUEST[$field["title"]]):"";I bet there are thousands of reasons that won't work! Mainly my regex experience comes from apache mod_rewriting and I'm not sure if PHP captures and labels the same way. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 2, 2009 Share Posted October 2, 2009 If you want to keep the leading zeros it's probably better not to use intval at all, just use is_numeric to check if it's a numeric string. If you want to save the leading zeros then that's string data, not an integer. An integer doesn't have leading zeros. However as one of my users discovered if the number 0 is entered by itself nothing gets passed to the forum post for that field instead of passing the answer of "0".That sounds like it's doing a value comparison somewhere and assuming that the string "0" is a "false" value, which it is. A type comparison would not evaluate the string "0" is false. Link to comment Share on other sites More sharing options...
wolfshaven Posted October 3, 2009 Author Share Posted October 3, 2009 I'm not so worried about the leading zeros as I am about it rejecting 0 as an answer altogether. It was more of an observation, something I'm sure will be asked of me in the future. At least now I have an answer for them. :)is_numeric might work to replace the float function for the form as the only thing I can see that useful for is allowing users to enter integers or decimals but the other line is for strictly limiting the users answers to integers. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 3, 2009 Share Posted October 3, 2009 I'm not so worried about the leading zeros as I am about it rejecting 0 as an answer altogether.Show the code where it validates that, that can be fixed if you want. Link to comment Share on other sites More sharing options...
wolfshaven Posted October 3, 2009 Author Share Posted October 3, 2009 Well as shown before this is what code checks the two fields... case 'int': $value = isset($_REQUEST[$field['title']]) ? intval($_REQUEST[$field['title']]) : ''; // If value is empty then set it to the default. if(($value == '') && !$required) $value = $default; // Restrict the length of value if necessary. if(($size != '')) $value = substr($value, 0, $size); break; case 'float': $value = isset($_REQUEST[$field['title']]) ? floatval($_REQUEST[$field['title']]) : ''; // If value is empty then set it to the default. if(($value == '') && !$required) $value = $default; // Restrict the length of the float value if necessary. if(($size != '')) $value = rtrim(substr($value, 0, $size), '.'); break; This is the code that displays them in the form its self... // Int, Float or text box? else echo ' <input type="text" name="', $field_name, '" id="', $field_name, '" value="', $field_data['value'], '" />'; This is the Submit button for the form // Output the save button, the end of the tables and the form. echo ' <tr> <td class="windowbg2" colspan="3" align="center" valign="middle"><input type="submit" value="', $txt['save'], '"', (!empty($context['save_disabled']) ? ' disabled="disabled"' : ''), ' /></td> </tr> </table> </td> </tr> </table> <input type="hidden" name="sc" value="', $context['session_id'], '" /> </form>';}// Function to call the correct function for showing the submit form page.function template_submit_form(){ global $context; // Well, we can try to get a user defined form template, but don't hold your hopes too high!;D if (isset($context['template_function']) && function_exists('form_template_' . $context['template_function'])) call_user_func('form_template_' . $context['template_function']); // Call the default template for the submit form page if we have to... else form_template_submit_form();} This is where it converts the form to enter it in a template for the forum post... // Add this fields value to the list of variables for the output post. $vars[] = '/\{'.$field['title'].'\}/'; $replace[] = str_replace('$','\$',$value); // Also add this data back into the data array, just in case we can't actually submit the form. $data[$i]['value'] = $value; Note the line "$replace[] = str_replace('$','\$',$value);" This was the last fix (where its replacing the $ with itself) ) I picked up from Ingolme of these forums, to keep something like "Bob has $23.45" from converting to "Bob has .45". The current problem with "0's" happens both before and after this fix. As I haven't submitted it to my users yet, I'm sure it's not the problem.And lastly this is the code that actually places the collected data in the forum post... // Replace all vars with their correct value, for both the message and the subject. $output = preg_replace($vars, $replace, $output); $subject = preg_replace($vars, $replace, $subject); That is only part of the code as everything is quite long but that where I'm pretty sure the problem is. You can get the entire mod at SMF http://custom.simplemachines.org/mods/index.php?mod=1279 if you want to see if there's something else. v1.5 is the current release and these bug fixes as well as a couple others Ive already figured out will make v1.6 Link to comment Share on other sites More sharing options...
justsomeguy Posted October 4, 2009 Share Posted October 4, 2009 You should be able to change this:if(($value == '') && !$required)to this:if(($value === '') && !$required)Using == will cause it to be true if the value is 0 also, or any value that evaluates to false. Using === checks specifically for the empty string and nothing else. Link to comment Share on other sites More sharing options...
wolfshaven Posted October 4, 2009 Author Share Posted October 4, 2009 Thank you very much justsomeguy, that worked perfectly. :)Its always embarrassing when its something so simple and I miss it. Ill be sure to credit you and Ingolme for your help in the next version. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.