9

I have gone through with the JInput Documentation.

We can access the JInput object like this:

JFactory::getApplication()->input;

If we are retrieving more values, it would be useful to store the JInput object in a variable so we don’t need to call JFactory::getApplication() every time we are doing that:

$jinput = JFactory::getApplication()->input;

To retrieve a value from JInput we need to use its get method:

$data = $jinput->get('varname', 'default_value', 'filter');

As you can see, it has 3 parameters:

  1. the variable name
  2. the default value (defaults to null)
  3. the filter name (defaults to cmd)

And here comes my question regarding the 3rd parameter.

How can I make my own filter for Jinput? For example if I have to validate phone numbers or IP address then how can I make my own filter for that?

  • 1
    This for me is 'question of the year' so far...+1 – Lodder Apr 5 '16 at 22:28
  • me too, +1 - already asked about that customizable filter – Tom Kuschel Apr 6 '16 at 16:06
2

this is quite simple

In first of it create an custom Jinput class such as class JinputTelephone // what ever you want.

perhaps, have a look in existing inputfilter classes under /libraries/joomla/input

// Modify the getter and filterMethod as you want

/**
 * Gets a value from the input data.
 *
 * @param   string  $name     Name of the value to get.
 * @param   mixed   $default  Default value to return if variable does not exist.
 * @param   string  $filter   Filter to apply to the value.
 *
 * @return  mixed  The filtered input value.
 *
 * @since   11.1
 */
public function get($name, $default = null, $filter = 'cmd')
{
    if('phonyfilter'=== $filter){ 
    // here we ca got with static filter classes like for formvalidation  or 
    }        
    elseif (isset($this->data[$name]))
    {
        return $this->filter->clean($this->data[$name], $filter);
    }

    return $default;
}

// usage

$phoneInput = new JinputTelephone();
$phoneInput->get('anPhoneNumber','0040','phonyfilter');

// with this method you can also use the regular input handling // But you can't use it with JApplication an input

There are many other ways to do that Another solution $data = MyFilter::SanitizeNumber($jinput->get('varname', 'default_value', 'raw'));

does it helps you?

1

My try,

i see that you have some experience with joomla so i will just put my few cents into it.

  1. you need to create system plugin
  2. you need to create a method after init or similar.
  3. now you need to initiate JLoader if i'm not wrong and add path to libraries a.k.a. /plugins/system/myplugin/libraries/newfilter.php
  4. enable plugin and profit ; )

PS. you can create by this way an overrides too joomla core libraries and platform. Such a Curl SSL problem with certs ; )

regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.