I'm trying to transition my app from a collection of server scripts to something that uses SOLID concepts.
I'm stuck trying to figure out a better way to do ajax validation.
I currently have a file named Ajax.php
in each directory, that handles all of the ajax validation requests from its siblings.
E.G /CC/index.php
sends its ajax requests to /CC/Ajax.php
.
This is the markup/code that drives this validation.
HTML
<input class="form-control validate"
name="OfficePhone"
id="OfficePhone"
type="text"
placeholder="(123) 456-7890"
data-request="OfficePhone"
value=""
>
JavaScript
var ajaxDefaults = {url:'Ajax.php',type:'post',cache:false,timeout:8000};
function ValidateTextInput(element, timeStamp) {
var $this = element;
var mydata = {};
mydata['DATA_REQUEST'] = $this.attr('data-request');
mydata[$this.attr('id')] = $this.val();
var myOptions = {
data:mydata,
beforeSend: ajaxBeforeSend,
el: $this,
elStamp : timeStamp
};
var options = $.extend({}, ajaxDefaults, myOptions);
$.ajax(options).done(ajaxDone).fail(ajaxFail).always(ajaxAlways);
}
Ajax.php
if (
(empty($_SERVER['HTTP_X_REQUESTED_WITH']) || strcasecmp($_SERVER['HTTP_X_REQUESTED_WITH'], 'xmlhttprequest') !== 0) ||
(empty($_SERVER['REQUEST_METHOD']) || strcasecmp($_SERVER['REQUEST_METHOD'], 'post') !== 0)
)
{
exit;
}
if (!isset($_POST['DATA_REQUEST']))
{
exit;
}
$output = array();
switch ($_POST['DATA_REQUEST'])
{
case "OfficePhone" :
// validate here
break;
case "AnotherName" :
// validate here
break;
}
echo json_encode($output);
exit;
I just started using FastRoute so was thinking that I could consolidate all of my Ajax.php
handlers into one class, but this would require some new markup.
Route
['POST', '/ajax/{id:[0-9]+}', ['AjaxValidationHandler','handle']]
New Ajax Handler
class AjaxValidationHandler {
private $request = null;
private $response = null;
// the dependencies for this class are resolved by my DIC
// this class is meant to act like a service/handler/controller of sorts
public function __construct(\Request $request, \Response $response) {
$this->request = $request;
$this->response = $response;
}
public function handle($routePa){
$method = 'validate' . $routeParams['id'];
if( !method_exists ( $this , $method ) ) {
throw new Exception('Invalid ajax request');
}
$this->{$method};
}
// OfficePhone
public function validate1(){
}
// AnotherName
public function validate2(){
}
}
HTML
<input class="form-control validate"
name="OfficePhone"
id="OfficePhone"
type="text"
placeholder="(123) 456-7890"
data-request="1"
value=""
>
JavaScript
var ajaxDefaults = {type:'post',cache:false,timeout:8000};
function ValidateTextInput(element, timeStamp) {
var $this = element;
var mydata = {};
mydata[$this.attr('id')] = $this.val();
var myOptions = {
url: '/ajax/'.concat( $this.attr('data-request') ),
data:mydata,
beforeSend: ajaxBeforeSend,
el: $this,
elStamp : timeStamp
};
var options = $.extend({}, ajaxDefaults, myOptions);
$.ajax(options).done(ajaxDone).fail(ajaxFail).always(ajaxAlways);
}
Known Pro and Cons
Cons
- In
AjaxValidationHandler
, if I don't keep up the comments for each method then it is hard update/maintain methods named validateX.
Pros.
- I think the class will have better performance than the switch statements.
- I can remove duplicated validation functions.
- 1 file instead of many
Ajax.php
files.
Is there a better validation scheme I can use to consolidate all of my handler scripts?