Created
October 9, 2014 20:07
-
-
Save spekkionu/e9103993138e666f9f63 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
{ | |
"name": "spakkionu/validate", | |
"description": "Validation Test", | |
"require": { | |
"illuminate/validation": "~4.2.9" | |
}, | |
"license": "MIT", | |
"authors": [ | |
{ | |
"name": "Jonathan Bernardi", | |
"email": "[email protected]" | |
} | |
], | |
"minimum-stability": "stable" | |
} |
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 | |
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
commented
May 18, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment