The register.php file
<?php
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users'),
'password' => array(
'required' => true,
'min' => 6),
'password_again' => array(
'required' => true,
'matches' => 'password'),
'name' => array(
'required' => false,
'min' => 2,
'max' => 50)
));
if($validation->passed()) {
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'name' => Input::get('name'),
'joined' => date('Y-m-d H:i:s'),
'group' => 1
));
Session::flash('home', 'You have been registered and can now log in!');
Redirect::to('index.php');
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validate->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
The Validate.php file
<?php
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct() {
$this->_db = DB::getInstance();
}
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
if($rule === 'required' && $rule_value === true && empty($value)) {
$this->addError("{$item} is required.");
} else if (!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$item}.");
}
break;
case 'unique':
$check = $this->_db->get('users', array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} is already taken.");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
protected function addError($error) {
$this->_errors[] = $error;
}
public function passed() {
return $this->_passed;
}
public function errors() {
return $this->_errors;
}
}
This is the code that i use to check if the password length, username and name is correct. If the password entered by the user is not correct it will output the error. For example "password_again does not match". Is it possible to change this variable name is any way? I want it to output like = "Password Again is not correct" or "Username is already is use". It would look much better with uppercase letters and without the underlines.
I hope you understood my question
Looking forward to your answers.
It may be possible to embed the error message somewhere in your form definition so that when a particular type of error occurs you show a custom message.
'password_again' => array(
'error_message' => 'Your custom error message here!'
'required' => true
)
Or maybe have a lookup file for the text you want to show for a particular field.
$error_messages = array(
'password_again' => 'We need a better password'
);
See Validation section of Laravel to get ideas on how to custom error messages could be stored.
FINAL EDIT:
I'm sorry. I didn't see your Validator.php class.
Here goes what you want:
<?php
class Validate {
private $_passed = false,
$_errors = array(),
$_db = null;
public function __construct() {
$this->_db = DB::getInstance();
}
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$itemName = $item;
if ($rule == "display_name") {
continue;
} else {
if (array_key_exists("display_name", $rules)) $itemName = $rules["display_name"];
}
$value = trim($source[$item]);
if($rule === 'required' && $rule_value === true && empty($value)) {
$this->addError("{$itemName} is required.");
} else if (!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$itemName} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$itemName} must be a maximum of {$rule_value} characters.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$itemName}.");
}
break;
case 'unique':
$check = $this->_db->get('users', array($item, '=', $value));
if($check->count()) {
$this->addError("{$itemName} is already taken.");
}
break;
}
}
}
}
if(empty($this->_errors)) {
$this->_passed = true;
}
return $this;
}
protected function addError($error) {
$this->_errors[] = $error;
}
public function passed() {
return $this->_passed;
}
public function errors() {
return $this->_errors;
}
}
Just edited the check() method to replace $item (the key name) with the value of the key "display_name" if exists. If not exists, just use the $item itself. Also, when iterating through the array, if the current $rule is "display_name", then it continues the loop from the next iteration. That way you don't process the "display_name" key.
If I am not wrong, now you just need to pass another key alongside the rules with the name "display_name" and the display string you want to represent that item. For example:
$validation = $validate->check($_POST, array(
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'users',
'display_name' => 'Username'),
'password' => array(
'required' => true,
'min' => 6,
'display_name' => 'Password'),
'password_again' => array(
'required' => true,
'matches' => 'password',
'display_name' => 'Password Again'),
'name' => array(
'required' => false,
'min' => 2,
'max' => 50,
'display_name' => 'Name')
));
Sorry for all the previous edits.
In order to turn some_string
into Some String
you can do like this:
echo ucwords(str_replace('_', ' ', 'password_again')); // "Password Again"
In the validate.php
file, you could use this approach to modify the value of $item
once the value has been fetched:
...
$value = trim($source[$item]);
$item = ucwords(str_replace('_', ' ', $item));
...
Then messages will automatically be like "Password Again must be a minimum of 2 characters". You can also modify any error messages as you see fit:
...
case 'matches':
if($value != $source[$rule_value]) {
// Old: $this->addError("{$rule_value} must match {$item}.")
$this->addError("{$item} is not correct.");
}
break;
case 'unique':
$check = $this->_db->get('users', array($item, '=', $value));
if($check->count()) {
// Old: $this->addError("{$item} is already taken.");
$this->addError("{$item} is already in use.");
}
break;
...
And so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.