Błąd podczas walidacji formularza


(h0lend3r) #1

Dla pola username minimalna ilość znaków to 2, a maksymalna 16. Po wpisaniu wartości pomiędzy 2 a 16 znaków, wyskakują błąd, informujący o tym, że podana wartość jest za mała. http://scr.hu/6bka/604bi To samo dzieje sie z polem password.

Validation::validate(['username' => Validator::notEmpty()->min(2)->max(24),
                      'email' => Validator::notEmpty()->email(),
                      'password' => Validator::notEmpty()->min(6)->max(32),
                      'repeatedPassword' => Validator::notEmpty()->equals($_POST['password'])
                      ], $_POST);

Tak wygląda wywoływanie metody walidującej, a to sama metoda:

public static function validate($fieldsAndRules, $request)
    {
        try {

            $validator = new Validator();

            foreach ($fieldsAndRules as $field => $rules) {
                $validator->key($field, $rules);
            }

            $validator->assert($request);

        } catch (AbstractNestedException $e) {
            self::$messages = $e->getMessages();    
        }
    }

Używam biblioteki respect/Validation. To cały widok html:

<?php addPartial('header'); ?>

    <div class="container">
        <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="panel-title text-center">Sign Up</div>
                </div>

                <div style="padding-top:30px" class="panel-body">

                    <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                    <form action="/shop/auth/postRegister" method="post" class="form-horizontal" role="form">

                        <div style="margin-bottom: 25px" class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                            <input type="text" class="form-control" name="username" value="<?= \Shop\libs\Session::flash('input-username') ?>" placeholder="Username">
                        </div>

                        <?php if(\Shop\libs\Session::has('error-username')): ?>
                            <div class="label label-danger label-form">
                                <?= \Shop\libs\Session::flash('error-username') ?>
                            </div>
                        <?php endif; ?>

                        <div style="margin-bottom: 25px" class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
                            <input type="text" class="form-control" name="email" value="<?= \Shop\libs\Session::flash('input-email') ?>" placeholder="Email Address">
                        </div>

                        <?php if(\Shop\libs\Session::has('error-email')): ?>
                            <div class="label label-danger label-form">
                                <?= \Shop\libs\Session::flash('error-email') ?>
                            </div>
                        <?php endif; ?>

                        <div style="margin-bottom: 25px" class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                            <input type="password" class="form-control" name="password" placeholder="Password">
                        </div>

                        <?php if(\Shop\libs\Session::has('error-password')): ?>
                            <div class="label label-danger label-form">
                                <?= \Shop\libs\Session::flash('error-password') ?>
                            </div>
                        <?php endif; ?>

                        <div style="margin-bottom: 25px" class="input-group">
                            <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                            <input type="password" class="form-control" name="repeatedPassword" placeholder="Repeat Password">
                        </div>

                        <?php if(\Shop\libs\Session::has('error-repeatedPassword')): ?>
                            <div class="label label-danger label-form">
                                <?= \Shop\libs\Session::flash('error-repeatedPassword') ?>
                            </div>
                        <?php endif; ?>

                        <div style="margin-top:10px" class="form-group">
                            <!-- Button -->

                            <div class="col-sm-12">
                                <input type="submit" value="Register" class="btn btn-primary btn-block"/>
                            </div>
                        </div>



                    </form>
                </div>
            </div>
        </div>

<?php addPartial('footer'); ?>

To klasa którą używam do przekierowywania gdy np. walidacja nie przebiegnie niepomyślnie:

class Redirect
{

    public static function to($href)
    {
        $href = 'http://' . $_SERVER['REMOTE_ADDR'] . "/shop/{$href}";
        header("Location: {$href}");

        return new self;
    }

    public static function intended()
    {
        header("location: " . $_SERVER['HTTP_REFERER']);

        return new self;
    }

    public function with($key, $value)
    {
        Session::set($key, $value);
        return $this;
    }

    public function withInput()
    {
        foreach ($_POST as $key => $value)
        {
            Session::set('input-' . $key, $value);
        }

        return $this;
    }

    public function withErrors($errors)
    {
        for($i = 0; $i < count($errors); $i++)
        {
            Session::set('error-' . array_keys($_POST)[$i], $errors[$i]);
        }
    }

if(Validation::fails())
{
     Redirect::intended()
             ->withInput()
             ->withErrors(Validation::getMessages());

}

Myśle, że ten błąd występuje gdzieś w sesji, lecz nie jestem w stanie tego zlokalizować. Wkleje jeszcze cały kod klasy Session:

class Session
{

    public static function has($key)
    {
        return isset($_SESSION[$key]) ? true : false;
    }

    public static function set($key, $value)
    {
        return $_SESSION[$key] = $value;
    }

    public static function get($key)
    {
        return isset($_SESSION[$key]) ? $_SESSION[$key] : '';
    }

    public static function delete($key)
    {
        if(self::has($key))
        {
            unset($_SESSION[$key]);
        }
    }

    public static function flash($key, $val = '')
    {
        if(self::has($key))
        {
            $session = self::get($key);
            self::delete($key);
            return $session;
        }
        else
        {
            self::set($key, $val);
        }
    }

}