We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 727
    • 502 Posts
    Would it be possible to make the precision of floats configurable in the next version? It would probably be best specified as part of the eform tag. Perhaps the types "float_n" could be implemented. For example, float_2, float_4, etc. Instead of checking the whole type string in the code, just check to see if it starts with "float" and then split() to get the precision. "float" could imply "float_2" to be backwards compatible.

    I am working on an application where I need 10 decimal places, and I don’t like to modify eform directly.

    Thanks, Andy
      • 30223
      • 1,010 Posts
      You can easily achieve what you want by using

      <input type="text" name="floatValue" eform="Float Value:float:1:Must be a float:#FUNCTION checkFloat" />


      Add a function in the same manner as an eForm event function (in a separate snippet):
      <?php
      function checkFloat($value){
         if( !is_numeric($value) ) return false;
         
         //code to check if it's within the parameters you want
         //you'll have to write this yourself :)
         //return false if not valid, true is valid
      }
      
      ?>
      
        • 727
        • 502 Posts
        Hi Toby, what I want to replace is not the checking but where the value is converted to a string containing the floating point value, i.e. the number_format function call. That is the point where the number of decimal places is specified. smiley

        Andy
        • Line 271 of eform.inc.php
          			# format report fields
          			foreach($fields as $name => $value) {
          				$fld = $formats[$name];
          				if ($fld) {
          					$datatype = $fld[2];
          					switch ($datatype)  {
          
          						case "integer":
          							$value = number_format($value);
          							break;
          						case "float":
          							$value = number_format($value, 2, '.', ',');
          							break;
          
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 727
            • 502 Posts
            Yes, that’s the spot. So can we make the "2" argument to number_format configurable? Thanks, smiley

            Andy
            • Sure. Just make that a variable, say $precision, and in the snippet code add (just before the eform params comment)
              // float precision
              precision => isset($precision)? $precision:2,
              
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 727
                • 502 Posts
                Great! So can that perhaps make it into the next release of eform? grin Although I think the suggestion I made in the first post is a bit more flexible, as it allows the user to specify a different precision for each input tag that uses a float type.

                Andy