Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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": ""
"minimum-stability": "stable"
use Illuminate\Validation\Factory as ValidatorFactory;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\HttpFoundation\Request;
$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>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Validation Test</title>
<link rel="stylesheet" href="//">
<div class="container">
<div class="page-header">
<h1>Validation Test</h1>
<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 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 class="form-group">
<input class="btn btn-primary" type="submit" value="Log In">

This comment has been minimized.

Copy link

@ahmadina ahmadina commented Feb 4, 2016

Cool, one question How can I add another language such as Persian(Farsi)?


This comment has been minimized.

Copy link

@ra3oul ra3oul commented Feb 4, 2016

@ahmadina ridi and there is no water !


This comment has been minimized.

Copy link

@shov 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