Reputation: 81
I am new to php OOP
Currently I am using the following code to validate a form on the server side.
The files that are used in this case are:
register.php:
<form action="" method="post">
<input type="text" placeholder="first name" value="<?php echo Input::get('fname'); ?>" name="fname" autocomplete="off" />
<input type="text" placeholder="last name" value="<?php echo Input::get('lname'); ?>" name="lname" autocomplete="off" />
<input type="email" placeholder="email" value="<?php echo Input::get('email'); ?>" name="email" autocomplete="off" />
<input type="password" placeholder="choose password" value="<?php echo Input::get('password'); ?>" name="password" autocomplete="off" />
<input type="password" placeholder="confirm password" value="<?php echo Input::get('password_again'); ?>" name="password_again" autocomplete="off" />
<span class="gen-sel">
<input type="radio" name="gen" value="male" id="male" />
<label for="male" class="fa fa-male"> male</label>
<input type="radio" name="gen" value="female" id="female" />
<label for="female" class="fa fa-female"> female</label>
</span>
<button class="btn right" type="submit" name="submit_register">Register</button>
<span>Already signed up? <a href="login.php">click here</a> to login</span>
</form>
Input.php which is a class to check whether an input is submitted or not:
<?php
class Input
{
public static function exists($type = 'post'){
switch($type){
case 'post':
return (!empty($_POST)) ? true : false;
break;
case 'get':
return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}
}
public static function get($item){
if(isset($_POST[$item])){
return $_POST[$item];
} else if(isset($_GET[$item])){
return $_GET[$item];
}
return '';
}
}
and the last file is Validate.php which is a class which has methods to validate the code (it is working properly for text input fields, i.e. email, text, password.):
public function check($source, $items = array()){
foreach ($items as $item => $rules){
foreach ($rules as $rule => $value){
$value = $source[$item];
if($rule === "required" && empty($value)){
$this->addError( $item,"Marked field is required");
} else{
}
}
if(empty($this->_errors)){
$this->passed = true;
}
}
return $this;
}
When I submit the form I get the following error for my text box:
Notice: Undefined index: gen in /Users/Sammy/Sites/workspace/zen-cust/classes/Validation.php on line 16
which points towards this line of code in Validation.php:
$value = $source[$item];
I know, instead of selected code I have added huge chunks of code in here, but I am really not able to understand what is the fault over here!
Thanks for the help in advance
Upvotes: 0
Views: 455
Reputation: 57316
You are checking whether the value is present using empty
function - which is correct - but you are doing it after trying to use it. You should check first instead:
if($rule === "required" && empty($source[$item])) {
Upvotes: 1
Reputation: 11
you could try to var_dump
the $source
, there you can see what all is in the $source
array.
because the error is caused do to the fact that it can't find the index in the array $source
Upvotes: 0