0

I'm busy learning PHP and have been following a tutorial to create a basic site where you can register and have a user account/change your password/update info etc.

I'm on the part where you change your password (I'm at about 6:10 in this Video) and I've become stuck. As far as I'm concerned I have done everything he has done in the tutorial but when I submit my form I get the following errors below. Please go easy on me as I'm sure I have left out a "," or spelled something wrong but for the life of me I can't find it. (Note: When I submit the form I do type in my current password but I get the validation error saying I haven't.)

(Update: Marking this post as duplicate and pointing me to another post is not very helpful. As I tried to stress above I am just learning PHP and therefore can't tease a solution out of the post and somehow relate it to my problem.)

Notice: Undefined index: password_current in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_current in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_new in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_new in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_new_again in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_new_again in C:\wamp\www\Movrate\classes\Validate.php on line 15

Notice: Undefined index: password_new_again in C:\wamp\www\Movrate\classes\Validate.php on line 15

password_current is required

password_new is required

password_new_again is required

Here is my code for the update password page:

    <?php
require_once 'core/init.php';

$user = new User();

if(!$user->isLoggedIn()) {
    Redirect::to('index.php');
}

if(Input::exists()) {
    if(Token::check(Input::get('token'))) {

        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'password_current' => array(
                'required' => true,
                'min' => 6
            ),
            'password_new' => array(
                'required' => true,
                'min' => 6
            ),
            'password_new_again' => array(
                'required' => true,
                'min' => 6,
                'matches' => 'password_new'
            )
        ));

        if($validation->passed()) {
            // change of password
        } else {
            foreach($validation->errors() as $error) {
                echo $error, '<br>';
            }
        }

    }
}
?>

<form action="" method="post">
    <div class="field">
        <lable for="password_current">Current password</label>
        <input type="password" name="passsword_current" id="password_current">
    </div>

    <div class="field">
        <lable for="password_new">New password</label>
        <input type="password" name="passsword_new" id="password_new">
    </div>

    <div class="field">
        <lable for="password_new_again">New password again</label>
        <input type="password" name="passsword_new_again" id="password_new_again">
    </div>

    <input type="submit" value="Change">
    <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
</form>

Here is the code for my validate class:

<?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]);
                $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;
                    }
                }

            }
        }

        if(empty($this->_errors)){
            $this->_passed = true;
        }

        return $this;
    }

    private function addError($error) {
        $this->_errors[] = $error;
    }

    public function errors() {
        return $this->_errors;
        }

    public function passed() {
        return $this->_passed;
    }

}
krz
  • 334
  • 5
  • 18

3 Answers3

1

I believe you have posted the wrong "Update password" page code above. You've posted the code for a form with only a single field "name". But the output response, validation class, the video, and the fact that you said 'i do type in my current password" indicate that you have created the update password form somewhere, but you must have just posted the wrong page code above. You may want to double check that and post the latest/correct page code. You need to make sure the input fields all exist in the form.

Without being able to see that page/code, it's possible the notice you are getting may be because you have different error_reporting level to that of the video demonstrator. You can prevent this notice from appearing by correctly checking for the array value in your validation class by replacing line 15:

$value = trim($source[$item]);

with this:

$value = isset($source[$item]) ? trim($source[$item]) : '';

It's worth giving that a shot.

Manachi
  • 1,027
  • 16
  • 30
  • Hi Manachi, thanks for pointing that out, I have put the correct code in now, see above! I will also try your suggestion. – krz Aug 19 '15 at 07:06
  • Hi @skr - no worries. Did you try the suggestion as well? I suspect it should resolve the issue. – Manachi Aug 19 '15 at 12:06
  • Hi Manachi, yes that does take away the error notices. However the validation is still not working. Even though I put in my current password when I submit the form, the validation says "current_password" is required. If you see the comment I made to mdami below I did a var_dump on the $_POST and it sees that the password_current string has 'password' in it. Do you know why the validation would then be thinking the field is empty? – krz Aug 20 '15 at 17:31
0

From what I can tell you are missing inputs fields in your form add the missing inputs.

check the posted data after form submission and see what is missing.

var_dump($_POST);

change your input name where it say passsword_.. to password_ match the filedname to the validation rule name. and you should be ok.

mdamia
  • 4,447
  • 1
  • 24
  • 23
  • Hi mdamia, sorry I posted the wrong code above. I have put the correct code in now for the update password page. – krz Aug 19 '15 at 07:07
  • see my updated answer, the output should have all your form fields. – mdamia Aug 19 '15 at 07:20
  • I put the var_dump in and got this: array (size=4) 'passsword_current' => string 'password' (length=8) 'passsword_new' => string '' (length=0) 'passsword_new_again' => string '' (length=0) 'token' => string '3d2c0e59a882afe842eb0d21e975abd4' (length=32) password_current is required password_new is required password_new_again is required So even though I put the password in it says the field is required. Does that mean there is something wrong with the validation? – krz Aug 20 '15 at 17:25
  • check your input name passsword_current, pass .... you have 3s's in there where your validation rules is only 2s's :). – mdamia Aug 20 '15 at 17:34
  • WOW, that was it. Thanks mdamia. Sorry for wasting everyones time with such a silly problem ;) – krz Aug 20 '15 at 17:40
  • don't mention it, it happens to the best of us. – mdamia Aug 20 '15 at 18:00
0

If you are learning you can

GAMITG
  • 3,810
  • 7
  • 32
  • 51
ishenkoyv
  • 665
  • 4
  • 9