Laravel Validation with Ardent Tutorial

If you are already looking into Laravel Validating, you should already know a bit about Laravel. So, you should be aware that Laravel is a MVC or Model View Controller PHP framework. Validation should occur in the Model part of the Model, View, Controller trio because the Model is where the business or database logic is kept. In this Laravel validation tutorial, we are going to discover how to do basic validation using the Ardent package.

Installing Ardent

The first thing we can do is install Ardent. To do this, we ned to open up the json file named composer.json which should be in your application root folder. We need to add the following code to the end of this file:
  “require”: {
    “laravelbook/ardent”: “dev-master”
  }
You wll want to add this near the bottom, but before the last closing bracket(}) so the last several lines of composer.json should look like this after adding the above code:

    “minimum-stability”: “dev”,

  “require”: {
    “laravelbook/ardent”: “dev-master”
  }

}

Save the composer.json file then open up a terminal window to your server and run the following command prompt command:

composer update

That will cause your application to update and upload the dependencies for Ardent since you required it in the composer.json file. The command may take a while to finish from the command line, so wait until you get your command prompt back so you know it is finished. You will see several removing, installing and downloading lines in the terminal window while it is working.

After the composer update command is finished, you may notice that several folders and files were updated in your application. Also it should have added a folder named laravelbook if you didn’t already have it and inside that folder you will find the ardent directory. I mention this just so you can confirm it worked if you want to.

Validating with Ardent

Validating with Ardent is done in the Model as I mentioned above. Let’s look at a very basic User.php model file as an example of how to use Ardent to validate input from the user.

Here’s the default users.php file minus comments:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = ‘users’;
    protected $hidden = array(‘password’);
    public function getAuthIdentifier()  {
        return $this->getKey();
    }

    public function getAuthPassword() {
        return $this->password;
    }

    public function getReminderEmail() {
        return $this->email;
    }

}

You may have noticed, no validation occurs in the default file, but with Ardent, we can add some. Here is how to transform your users.php file to validate with Ardent:

  1. Add the following line of code towards the top of the file where you see the other two “use” declarations, so you can add the following just under where it says “use Illumincate….”:
    use LaravelBook\Ardent\Ardent;
  2. Change the extends line just under the line we added in step 1 from:  class User extends Eloquent implements UserInterface, RemindableInterface {             to:
    class User extends Ardent implements UserInterface, RemindableInterface {
  3. Just before the } bracket that closes the User class at the bottom of the file add in the following  Ardent validation rules to make your model self-validating, meaning that you won’t have to do anything, the model will simply reject invalid input and accept valid imput. So if a user trys to save something that doesn’t pass validation, the save method will return false and nothing will be saved, otherwise if it validates, it will save the data and create the user in this case. Here is a validation rule set example to add to the end of your User class before the closing bracket:
     // Ardent validation rules:
    public static $rules = array(
      ’email’ => ‘required|email’,
      ‘password’ => ‘required|alpha_num|min:6|confirmed’,
      ‘password_confirmation’ => ‘required|alpha_num|min:6’,
    );
  4. Not all data needs to be saved, so we need to tell Laravel to purge the redundant data. In this case redundant data would be the password confirmation. Add the following line of code just below your validation rules to purge that data:
    public $autoPurgeRedundantAttributes = true;
  5. Save the User.php file then open your applications routes.php file in the main folder. Add the following route to test the validation changes you made to your User model:                                                     //test Ardent validation process:
    Route::get(‘/user_test’, function() {
      $user = new User;
      $user->email = ‘linian11@yahoo.com’;
      $user->password = ‘mypassword’;
      $user->password_confirmation = ‘mypassword’;
      var_dump($user->save());
    });

That’s all the steps, now go to the route you created in the final step above and if the user validates and is saved, you will see this in your browswer window only:

bool(true)

Then you know it worked. if you don’t see that or see a false instead, go back and check your code.

Summary

I hope this Laravel Validation tutorial has helped you understand how to install and user Ardent so that you can make self-validating models from now on. You may want to expand on this validation technique to make it more user friendly, but this should at least get you pointed in the correct direction and get you started.

Leave a Reply

Your email address will not be published. Required fields are marked *