Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nether Input

Nether Input

Demonstration by example of how the Input library can make working with data input/output nice and clean.

Bob Majdak Jr

May 30, 2015
Tweet

More Decks by Bob Majdak Jr

Other Decks in Programming

Transcript

  1. what it does  handle input from GET, POST, or

    any other named dataset from a chainable OOP interface.  define different functions, validators, or sanitisers to different fields.  define a default function to handle any field without a specific handler.  reuse the same filter sets on different datasets.  case insensitive (by default) – your OOP code standards do not have to match the HTML naming conventions.  lightweight in both dependency and api.
  2. form setup <form method="post"> <input type="text" name="username" /> <input type="text"

    name="email" /> <input type="password" name="password" /> <input type="submit" value="Sign Up" /> </form> a generic html form. nothing special here.
  3. filter setup $input = (new Nether\Input\Filter($_POST)) ->Username(function($v){ if(strlen($v) < 3)

    return false; if(preg_match('/[^a-z0-9]/i',$v)) return false; return $v; }) ->Email(function($v){ return filter_var($v,FILTER_VALIDATE_EMAIL); }) ->Password(function($v){ if(strlen($v) < 8) return false; return $v; }); assign functions to specific form values by calling them as methods on the filter object, supplying them with a callback to execute. $input->FieldName($callback); this can be defined ahead of time as part of your application setup if you feel like you are going to use it enough, or you can just define it before you plan to use it.
  4. filter setup (cont.) $input = (new Nether\Input\Filter($_POST)) ->Username(function($v){ ... })

    ->Email(function($v){ ... }) ->Password(function($v){ ... }); regardless of where you set it up, all your validation logic is now grouped together and out of the way. code folding for the win.
  5. application logic if(!$input->Username) echo User::ErrorInvalidUsername; elseif(!$input->Email) echo User::ErrorInvalidEmail; elseif(!$input->Password) echo

    User::ErrorInvalidPassword; else { $user = User::Create([ 'Username' => $input->Username, 'Email' => $input->Email, 'Password' => $input->Password ]); $router->Redirect('/dashboard/'); } access the data with your filters automatically applied just by reading out a property that matches the field name. echo $input->FieldName; now your signup.php actual controller logic can be fairly clean.
  6. filter reuse $result = $db->Query($sql,$args); while($row = $result->Next()) { $filter->SetDataset($row);

    // ... do stuff with the data. } after you define a bunch of filters, you can change out the dataset to reuse the filters multiple times.
  7. useful for output too <?php $post = (new Nether\Input\Filter($_POST)) ->SetDefaultFunction(function($v){

    return htmlentities($v); }); ?> <form method="post"> <input type="text" name="username" name="<?php echo $post->Username ?>" /> <input type="text" name="email" name="<?php echo $post->Email ?>" /> <input type="password" name="password“ name="<?php echo $post->Password ?>" /> <input type="submit" value="Sign Up" /> </form> here is that same html form from earlier, now with support for prefilling on failure. a great first line of defense against cross-site scripting problems like script tag injection.
  8. how to install “require”: { “netherphp/input”: ”~1.0.0” } install via

    composer. input depends on two other nether libraries: object and option. they are small though and automatically installed by composer - you do not need the full nether stack to use input.
  9. about  site: http://nether.io  repository: http://github.com/netherphp/input  developer: bob

    majdak jr <[email protected]> @bobmajdakjr all components of the nether framework are released under the bsd 3-clause licence.