MrJuliuss / syntara

Admin package for Laravel 4.
MIT License
302 stars 77 forks source link

Username validation #141

Closed brack11 closed 9 years ago

brack11 commented 10 years ago

Hi, I'm trying to let visitors to register themselves, but when user inserts already taken username the form doesnt show any message, difficult to understand what is actually wrong.

Another issue - when user add number to the username it shows error message alfa (my site is in Swedish so, I will just referre to it as alfa message) something like "only letters allowed". If superuser creates user, then numbers are also allowed.

MrJuliuss commented 10 years ago

Hi,

Registration : this is not a syntara feature, without code i can't resolve your problem, ca you paste your code ? (php & js)

Alpha message : let me verify ;)

Julien

brack11 commented 10 years ago

js is user.js from syntara assets and here is php: UserController.php:

public function store() {
       try
        {   
            $validator = new UserValidator(Input::all(), 'create');
            if(!$validator->passes())
            {
                return Response::json(array('userCreated' => false, 'errorMessages' => $validator->getErrors()));
            }
            // create user
            if(Input::get('pass') === Input::get('pass1')) {

                $user = Sentry::getUserProvider()->create(array(
                    'email'    => Input::get('email'),
                    'password' => Input::get('pass'),
                    'username' => Input::get('username'),
                    'last_name' => (string)Input::get('last_name'),
                    'first_name' => (string)Input::get('first_name'),
                    'activated' => true,
                    'activated_at' => time(),
                ));
                $group = Sentry::getGroupProvider()->findByName('seller');
                $user->addGroup($group);
            } else {
                return json_encode(array('userCreated' => false, 'message' => trans('syntara::users.messages.user-email-exists'), 'messageType' => 'danger'));
            }
        }
        catch (\Cartalyst\Sentry\Users\LoginRequiredException $e){} // already catch by validators
        catch (\Cartalyst\Sentry\Users\PasswordRequiredException $e){} // already catch by validators
        catch (\Cartalyst\Sentry\Groups\GroupNotFoundException $e){}
        catch (\Cartalyst\Sentry\Users\UserExistsException $e)
        {
            return json_encode(array('userCreated' => false, 'message' => trans('syntara::users.messages.user-email-exists'), 'messageType' => 'danger'));
        }
        catch(\Exception $e)
        {
            return Response::json(array('userCreated' => false, 'message' => trans('syntara::users.messages.user-name-exists'), 'messageType' => 'danger'));
        }

        return json_encode(array('userCreated' => true, 'redirectUrl' => URL::route('listUsers')));
}

And here is blade:

<div class="form-group">
                                    <label class="control-label">{{ trans('syntara::all.password') }}</label>
                                    <p><input class="col-lg-12 form-control" type="password" placeholder="{{ trans('syntara::all.password') }}" id="pass" name="pass"></p>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">{{ trans('custom.same') }}</label>
                                    <p><input class="col-lg-12 form-control" type="password" placeholder="{{ trans('syntara::all.password') }}" id="pass" name="pass1"></p>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">{{ trans('syntara::users.last-name') }}</label>
                                    <p><input class="col-lg-12 form-control" type="text" placeholder="{{ trans('syntara::users.last-name') }}" id="last_name" name="last_name"></p>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">{{ trans('syntara::users.first-name') }}</label>
                                    <p><input class="col-lg-12 form-control" type="text" placeholder="{{ trans('syntara::users.first-name') }}" id="first_name" name="first_name"></p>
                                </div>
                                <div class="form-group">
                                    <button id="add-user" type="submit" class="btn btn-primary" style="margin-top: 15px;">{{ trans('syntara::all.create') }}</button>
                                </div>

However I have solved the problem by editing validadator.php

        'create' => array(
                'email' => array('required', 'email', 'unique:users,email'),
                'pass' => array('required', 'min:6', 'max:255', 'same:pass1'),
                'username' => array('required', 'min:3', 'max:255', 'alpha_num', 'unique:users,username'),
                'last_name' => array('min:3', 'max:255', 'alpha_dash'),
                'first_name' => array('min:3', 'max:255', 'alpha_dash'),
            ),
        'update' => array(
            'email' => array('required', 'email', 'unique:users,email'),
            'pass' => array('min:6', 'max:255', 'same:pass1'),
            'username' => array('required', 'min:3', 'max:255', 'alpha_num', 'unique:users,username'),
            'last_name' => array('min:3', 'max:255', 'alpha_dash'),
            'first_name' => array('min:3', 'max:255', 'alpha_dash'),
            ),
MrJuliuss commented 10 years ago

Hi,

brack11 commented 10 years ago

Service didnt return ANY message about existing username before I changed validator.php in app/config/packages/mrjulius/... But investigation by devtools showed that json data returned with the message about existing user. Now I made validator messages to show up. However, json messages are still not showing anywhere. Are there suppose to be special block for them? I looked through your views code and didnt find anything special in there. I see in user.js following lines:

showStatusMessage(result.message, result.messageType)

What do they suppose to do or how to actually show them in view?

MrJuliuss commented 10 years ago

Hi, you need to use showRegisterFormAjaxErrors(message) to show form errors.

Julien

brack11 commented 10 years ago

Can you explain that more? t.ex. $('#messages').showRegisterFormAjaxErrors(message) would that do the trick?

MrJuliuss commented 10 years ago

Hi, this function show errors directly in forms, insteaf of showStatusMessage which show a "global" status message in ui.

brack11 commented 10 years ago

Ahh, building another site and getting into the same pit. Verified all form elements with the working one, looks the same, in previous project things are working in current one no. I have linked to user.js from syntara folder, and it has all showRegisterFormAjaxErrors but no messages shown in the form. How does showRegisterFormAjaxErrors actually work, what is selector(s) in code that it uses?

brack11 commented 10 years ago

Ok, I figured it. The issue was that I had my form surrounded by bootstrap's panel class tags now I have changed panel to module and everything started to work. Maybe a good idea to make selector used by showRegisterFormAjaxErrors less dependent on module surrounding?

MrJuliuss commented 10 years ago

Hi, showRegisterFormAjaxErrors used the form-group selector : https://github.com/MrJuliuss/syntara/blob/master/public/assets/js/dashboard/base.js#L138