-
-
Save absent1706/2e368a9d7e802b972ebd4ba93bf79945 to your computer and use it in GitHub Desktop.
Standalone validation using laravel validation component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"illuminate/validation": "~4.2.9" | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// TODO: leave old input (through session) | |
use Illuminate\Validation\Factory as ValidatorFactory; | |
use Symfony\Component\Translation\Translator; | |
use Symfony\Component\Translation\MessageSelector; | |
use Symfony\Component\HttpFoundation\Request; | |
require('vendor/autoload.php'); | |
class DbValidator extends \Illuminate\Validation\Validator { | |
public function __construct($connection, $translator, $data, $rules, $messages, $customAttributes) { | |
$this->connection = $connection; | |
return parent::__construct($translator, $data, $rules, $messages, $customAttributes); | |
} | |
public function validateExists($attribute, $value, $parameters) | |
{ | |
$this->requireParameterCount(2, $parameters, 'exists'); | |
$table = $parameters[0]; | |
$column = $parameters[1]; | |
$result = $this->_query("select count(*) as value_count from `$table` where `$column` = ?", [$value]); | |
return ($result['value_count'] > 0); | |
} | |
public function validateUnique($attribute, $value, $parameters) | |
{ | |
$this->requireParameterCount(2, $parameters, 'exists'); | |
$table = $parameters[0]; | |
$column = $parameters[1]; | |
$result = $this->_query("select count(*) as value_count from `$table` where `$column` = ?", [$value]); | |
return ($result['value_count'] == 0); | |
} | |
public function _query($query, $bindings) | |
{ | |
$statement = $this->connection->prepare($query); | |
$statement->execute($bindings); | |
return $statement->fetch(); | |
} | |
} | |
$request = Request::createFromGlobals(); | |
$errors = null; | |
if ($request->isMethod('POST')) { | |
$values = $request->request->all(); | |
$translator = new Translator('en_US', new MessageSelector()); | |
$validatorFactory = new ValidatorFactory($translator); | |
$validatorFactory->resolver(function($translator, $data, $rules, $messages, $customAttributes) { | |
$connection = new PDO('mysql:host=localhost;dbname=test', 'root', ''); | |
return new DbValidator($connection, $translator, $data, $rules, $messages, $customAttributes); | |
}); | |
$rules = array( | |
'username' => ['required', 'min:3', 'max:20', 'unique:user,first_name'], | |
'country_id' => ['required', 'exists:product_subject,product_id'], | |
'password' => ['required', 'min:5'] | |
); | |
$defaultMessages = array( | |
'required' => '{}' | |
); | |
$messages = array( | |
'username.required' => 'Is required.', | |
'username.unique' => 'Must be unique', | |
'username.min' => 'Must be at least :min characters.', | |
'username.max' => 'Must be no more than :max characters.', | |
'password.required' => 'Is required.', | |
'password.min' => 'Must be at least :min characters.', | |
'country_id.exists' => 'Incorrect value', | |
); | |
$validator = $validatorFactory->make($values, $rules, $messages); | |
if ($validator->fails()) { | |
$errors = $validator->messages(); | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<title>Validation Test</title> | |
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="page-header"> | |
<h1>Validation Test</h1> | |
</div> | |
<form class="" action="index.php" method="post" novalidate> | |
<div class="form-group <?php if($errors && $errors->has('username')) echo 'has-error';?>"> | |
<label class="control-label" for="username">Username</label> | |
<input class="form-control" id="username" type="text" name="username" required> | |
<?php if($errors && $errors->has('username')): ?> | |
<p class="help-block"><?php echo htmlspecialchars($errors->first('username'));?></p> | |
<?php endif; ?> | |
</div> | |
<div class="form-group <?php if($errors && $errors->has('password')) echo 'has-error';?>"> | |
<label class="control-label" for="password">Password</label> | |
<input class="form-control" id="password" type="password" name="password" required> | |
<?php if($errors && $errors->has('password')): ?> | |
<p class="help-block"><?php echo htmlspecialchars($errors->first('password'));?></p> | |
<?php endif; ?> | |
</div> | |
<div class="form-group <?php if($errors && $errors->has('country_id')) echo 'has-error';?>"> | |
<label class="control-label" for="country_id">country id</label> | |
<input class="form-control" id="country_id" type="country_id" name="country_id" required> | |
<?php if($errors && $errors->has('country_id')): ?> | |
<p class="help-block"><?php echo htmlspecialchars($errors->first('country_id'));?></p> | |
<?php endif; ?> | |
</div> | |
<div class="form-group"> | |
<input class="btn btn-primary" type="submit" value="Log In"> | |
</div> | |
</form> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment