PMA\libraries\Sanitize::formatJsVal PHP Method

formatJsVal() public static method

Formats a value for javascript code.
public static formatJsVal ( string $value ) : string
$value string String to be formatted.
return string formatted value.
    public static function formatJsVal($value)
    {
        if (is_bool($value)) {
            if ($value) {
                return 'true';
            }
            return 'false';
        }
        if (is_int($value)) {
            return (int) $value;
        }
        return '"' . Sanitize::escapeJsString($value) . '"';
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Formats javascript assignment for form validation api
  * with proper escaping of a value.
  *
  * @param string  $key   Name of value to set
  * @param string  $value Value to set
  * @param boolean $addOn Check if $.validator.format is required or not
  * @param boolean $comma Check if comma is required
  *
  * @return string Javascript code.
  */
 public static function getJsValueForFormValidation($key, $value, $addOn, $comma)
 {
     $result = $key . ': ';
     if ($addOn) {
         $result .= '$.validator.format(';
     }
     $result .= Sanitize::formatJsVal($value);
     if ($addOn) {
         $result .= ')';
     }
     if ($comma) {
         $result .= ', ';
     }
     return $result;
 }