"C# Basics (Retired)" was retired on June 30, 2019.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Laravel Form Validation extending Validator

I'm having trouble creating and using my own validation rule using the laravel framework.

There are my steps app/customValidation I'm extending Validator by creating my own class. I called that class "customValidation"

class customValidation extends Illuminate\Validation\Validator{

        public function itsTimeToValidateInput($attribute, $value){
            Validator::extend('alpha_spaces', function($attribute, $value){

                return preg_match('/^[\pL\s]+$/u', $value);
            });
        }
 }

app/start/global I then included my custom class in global.php require

```app_path(). '/customValidation.php';

I then use my custom validation in a function I wrote in one of my controllers and it's not working. Below is a portion of my function suntax. The "apha_spaces" is my extension of the validator. It's not working. My question is, how do I get it to work?

        ```$rules = array(
            'expression' => 'required|max:90|min:3|alpha_spaces|unique:sayings'
        );

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

2 Answers

Not always the best but try: http://culttt.com/2014/01/20/extending-laravel-4-validator/

*EDIT this looks a lot better.

also laracasts has a free video on validation

Instead of validating for excessive/any white space, why don't you crop them out anyway with trim? Unless you have a really valid reason to throw a validation error at the user! I would checkout all similar string functions - there's bound to be one there to suit your needs.

I've read the documentation and can see what you're trying to get at. From what I can tell, you've combined two different ways to add a new function for this validation.

I would first make it more Laravel-ee and do:

<?php


use Illuminate\Validation\Validator;

class customValidation extends Validator{

also:

Although down to the developer, Laravel is very consistent with naming, so I would recommend using standard class names with studly caps:

class CustomValidation

instead of

class customValidation

You're probably looking to do something more like:

<?php

Validator::extend('alpha_spaces', function($attribute, $value, $parameters)
{
    //awesome code here
});

and that's it!

No need to create a new class.

But if you do want to create a new class, you could maybe do something like:

<?php


use App\Namespace\Here\CustomValidator;

class RegistrationForm extends CustomValidator {


 $rules = array( /*Loads of sweet rules */ );




}

and in your custom validator class

<?php

use Illuminate\Validation\Validator;

class CustomValidator extends Validator {

public function validateAlphaSpaces($attribute, $value, $parameters)
{
    return $value == 'foo';
}

}

because your class that contains the rules is extending your custom validator, which is extending validator, you should have everything you need!

I haven't tested any of this so simplify first by adding a simple callback function, then advance onto a whole new class!

Tom

If you return false in your alpha_spaces methid do you receive any errors? Your reg ex may be wrong.

Are any of the other validations working?

Have you validation messages set for the alpha_spaces method?

Hi

Thanks for the help.

"If you return false in your alpha_spaces methid do you receive any errors? Your reg ex may be wrong."

I don't believe my regrex is wrong. I want my regrex to check against excessive white space. For example, if someone types in "John()()()()()()Joe"-> the () represent whitepsace my validation will kick back to the user and say something like "The :attribute may only contain letters "

"Are any of the other validations working?" Yes, the required validation is working.

"Have you validation messages set for the alpha_spaces method?" Yes, please have a look at my second laravel bin (below)

This is my composer.json file http://laravel.io/bin/32j4w

These are all my files within my app http://laravel.io/bin/KkdOD

I'm currently getting an error message that is stating Fatal error: Class 'Validator' not found in C:\wamp\www\laravel\register\app\validate\validators.php on line 6 http://tinyurl.com/ky27wbt