Right now I just have one error message variable that pulls the item name from the form input field that is creating the validation error. So if I only had one letter typed in the 'firstname' input field the error message should show up like this...
firstname must be a minimum of 2 characters.
Which is fine, but I want to create new variables, so that I can put something like
$firstname_validate_error
next to the firstname input field and if that input field is throwing the error for the error to come up next to that field.
Is there a way to do this with the code I have without making this a huge task? I'm not sure how to structure a variable to do this with my validate class.
I am getting my validation errors from this:
} else {
foreach($validation->errors() as $error) {
$error;
}
Is there a way I can structure this so I can put some kind of error variable next to each input field in the form so if that input field is throwing the validation error, the error message comes up?
if(Input::exists()) {
if(Token::check(Input::get('token'))) {
if(Input::exists()) {
$validate = new Validate();
$validation = $validate->check($_POST, array(
'firstname' => array(
'required' => true,
'min' => 2,
'max' => 50
),
'lastname' => array(
'required' => true,
'min' => 2,
'max' => 50
),
'email' => array (
'required' => true,
'min' => 2,
'max' => 50
),
'phone_number' => array (
'required' => true,
'min' => 7,
'max' => 15
),
'username' => array(
'required' => true,
'min' => 4,
'max' => 20,
'unique' => 'users'
),
'password' => array(
'required' => true,
'min' => 6
),
'password_again' => array(
'required' => true,
'matches' => 'password'
)
));
if($validation->passed()) {
$user = new User();
$salt = Hash::salt(32);
try {
$user->create(array(
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email' => Input::get('email'),
'phone_number' => Input::get('phone_number'),
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'), $salt),
'salt' => $salt,
'joined' => date('Y-m-d H:i:s'),
'group' => 1
));
$success = "You have successfully created an account. We will notify you once the account has been approved. Then you will be able to login.";
echo $success;
} catch(Exception $e) {
die($e->getMessage());
}
} else {
foreach($validation->errors() as $error) {
$error;
}
}
}
}
}
My validate class that pertains to this.
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
$item = escape($item);
if($rule === 'required' && 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($rule_value, array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} already exists.");
}
break;
My form is like this..
<form action="" method="POST">
<div class="field">
<label for="firstname">First Name</label>
<input type="text" class="inputbar" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" required>
</div>
<div class="field">
<label for="lastname">Last Name</label>
<input type="text" class="inputbar" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" required>
</div>