Skip to content

Instantly share code, notes, and snippets.

@spekkionu
Created October 9, 2014 20:07
Show Gist options
  • Save spekkionu/e9103993138e666f9f63 to your computer and use it in GitHub Desktop.
Save spekkionu/e9103993138e666f9f63 to your computer and use it in GitHub Desktop.
Standalone validation using laravel validation component
{
"name": "spakkionu/validate",
"description": "Validation Test",
"require": {
"illuminate/validation": "~4.2.9"
},
"license": "MIT",
"authors": [
{
"name": "Jonathan Bernardi",
"email": "[email protected]"
}
],
"minimum-stability": "stable"
}
<?php
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');
$request = Request::createFromGlobals();
$errors = null;
if ($request->isMethod('POST')) {
$values = $request->request->all();
$translator = new Translator('en_US', new MessageSelector());
$validatorFactory = new ValidatorFactory($translator);
$rules = array(
'username' => ['required', 'min:3', 'max:20'],
'password' => ['required', 'min:5', 'max:60']
);
$messages = array(
'username.required' => 'Username is required.',
'username.min' => 'Username must be at least :min characters.',
'username.max' => 'Username must be no more than :max characters.',
'password.required' => 'Password is required.',
'password.min' => 'Password must be at least :min characters.',
'password.max' => 'Password must be no more than :max characters.',
);
$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">
<input class="btn btn-primary" type="submit" value="Log In">
</div>
</form>
</div>
</body>
</html>
@shov
Copy link

shov commented May 18, 2018

{
    "name": "illuminate/validation",
    "version": "v5.6.22"
}
use Illuminate\Translation\ArrayLoader;
use Illuminate\Translation\Translator;

use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Factory as ValidatorFactory;

/**
 * Validation service
 */
class Validator
{
    /**
     * Validate array of data with given rules
     * @param array $source
     * @param array $rules
     * @throws ValidationException
     */
    public function validateArray(array $source, array $rules)
    {
        $translator = new Translator(new ArrayLoader(), 'en_US');
        $validatorFactory = new ValidatorFactory($translator);

        $validator = $validatorFactory->make($source, $rules);
        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment