Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

butterscotch's avatar

Log in using student id not working

Hi I'm still new in using laravel and I have an application that uses student id and password to login. However, when I try log it in, it gives me this error "Method [validateStudentid] does not exist."

What should I do so that I can be able to login?

Here is my HomeController:

public function showLogin()
{
    // show the form
    return View::make('login');
}

public function doLogin()
{
    $rules = array(
        'studentid'    => 'required|studentid',
        'password' => 'required|alphaNum|min:2'
    );

$validator = Validator::make(Input::all(), $rules);

// if the validator fails, redirect back to the form
if ($validator->fails()) {
    return Redirect::to('login')
        ->withErrors($validator) 
        ->withInput(Input::except('password')); 
} 

else {

// create our user data for the authentication
$userdata = array(
    'studentid'     => Input::get('studentid'),
    'password'  => Input::get('password')
);

    // attempt to do the login
    if (Auth::attempt($userdata)) {

            echo 'SUCCESS!';
        } 
    else {        

        // validation not successful, send back to form 
        return Redirect::to('login');

        }

    }
}


public function doLogout()
{
    Auth::logout(); // log the user out of our application
    return Redirect::to('login'); // redirect the user to the login screen
}
0 likes
7 replies
MThomas's avatar

The problem is that it can't find the method for validating the student ID. Did you register the custom validation rule 'studentid' correctly?

And to point you to the line which invokes the error: 'studentid' => 'required|studentid',, here you define a validation rule named studentid on the studentid form field.

nolros's avatar

Option 1: place it in your controller and call it like you would any class.

    // $attribute = studentId
    // $value = idOfStudent (just so you understand the difference)
    // parameters - no in your case
    // I'm assuming your model is Student and you've have imported it

    public function doLogin()
    {

        Validator::extend('isStudent', function($attribute, $value, $parameters)
        {
            return Student::find(idOfStudent);

        });

        // rest of your code 
    // $results = Validator::make(Input::all(), $rules, $messsages);
    }
Option 2 , which is the right way would be to create a StudentValidationClass and ServiceProvider
1. Note this requires more effort to setup, but cleaner and more extensible
2. Note - I have not tested or run any of this code.
3. Create directory App/Validators/
4. Create a class StudentValidationClass (or whatever you want to call it)
5. Create a method for it, example, hasStudent()
<?php namespace App\Validators;

class StudentValidator extends \Illuminate\Validation\Validator {

    // 1. you must extend the class Validator class
    // 2. the advantage of this is you can also create methods for your other student validation values as well as other student validation attributes, like isPaid

    public function hasStudent($attribute, $value, $parameters)
    {
        // in this case value is idOfStudent
        return Student::find($value);
    }

    // you can make this more sophisticated than just returning an array, but for this example
    public function getStudentRules()
    {
        return [
            'studentid' => 'required|idOfStudent',
            'password'  => 'required|alphaNum|min:2'
        ];
    }

    public function getStudentMessages()
    {
        return [
            'studentid.required'         => 'Please submit a student id.',
            'istudentid.idOfStudent'    => 'No such student exists.'
        ];

    }
}

Now you create a ServiceProvider to hook it up.

<?php namespace App\Validators;

use Illuminate\Support\ServiceProvider;

class StudentServiceProvider extends ServiceProvider {

    public function register(){}

    public function boot()
    {
        $this->app->validator->resolver(function( $translator, $data, $rules, $messages )
        {
            return new StudentValidator($translator, $data, $rules, $messages );
        });
    }

}

Now we just need to tell Laravel to load this Service Provider and we would be all set. Open up your app/config/app.php and in the providers array add the following at the end:

'App\Validators\StudentServiceProvider',

run composer sump-autoload

1 like

Please or to participate in this conversation.