Jump to content

Hello, Been googling a bit but couldent find what I was looking for.

 

I want to create custom validations for stuff I have, etc ranks or country. how do I do this?

 

Best would be a link to how you do it if there is a official way.

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/793568-laravel-54-custom-validation/
Share on other sites

Link to post
Share on other sites

Its okey when you ask a lot of questions, it would simply be awesome if you check google and the official Laravel Documentation first. 

I have nothing against helping, its just that some of your question seem so logical to me ;) and I think you can really easily find the answers yourself ;) 

Anyway for your question: Its in the official Laravel Documentation (first thing always to check) > Validation > Extending 

https://laravel.com/docs/5.4/validation#custom-validation-rules

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

Still have to figure out that myself too, havent worked with them really. I know i saw a post on facebook that laravel will add some custom validations in a new version. Or maybe it has been released already. But im not sure :D

Quote or mention me if not feel ignored 

Link to post
Share on other sites

3 hours ago, leodaniel said:

Its okey when you ask a lot of questions, it would simply be awesome if you check google and the official Laravel Documentation first. 

I have nothing against helping, its just that some of your question seem so logical to me ;) and I think you can really easily find the answers yourself ;) 

Anyway for your question: Its in the official Laravel Documentation (first thing always to check) > Validation > Extending 

https://laravel.com/docs/5.4/validation#custom-validation-rules

Yea I have tryed but I dident find this when looking thru it. but I did not compleatly understand what does what and were I put my rules and error messages in, etc I want to match it to a array with country codes to be sure it's a valid country.

 

if I understand the doc correct "foo" is the validation name, like required is one. I guess $value is the thing you are validating. $parameters are etc min: >6<- that part.

 

but the rest I don't understand.

Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
            return $value == 'foo';
        });

 

When that gets sorted out is it just straight forward etc $value is the thing to be validated.

 

etc

 

$array = array("NO", "PL");
if (in_array($value, $array)) {
    return true;
}

?

 

 

 

Back-end developer, electronics "hacker"

Link to post
Share on other sites

1 hour ago, Joveice said:

Yea I have tryed but I dident find this when looking thru it. but I did not compleatly understand what does what and were I put my rules and error messages in, etc I want to match it to a array with country codes to be sure it's a valid country.

 

if I understand the doc correct "foo" is the validation name, like required is one. I guess $value is the thing you are validating. $parameters are etc min: >6<- that part.

but the rest I don't understand.


Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
            return $value == 'foo';
        });

 

When that gets sorted out is it just straight forward etc $value is the thing to be validated etc?

 

 

 

So basically you just add them into your serviceprovider. So in your service provider. You can also use anonymus functions but I would always put them into a separate class. 

<?php
use Illuminate\Support\Facades\Validator;

...
  
public function boot(){
  /* I would put them always into a seperate class in my example MyValidationRules */
  Validator::extend('country_codes','App\\MyValidationRules@CountryCodes');
  
}

Then your class MyValidationRules. 

<?php
use Illuminate\Validation\Validator;

class MyValidationRules
{
    protected $allowed_countries = ['CH','DE'];

    /**
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param Validator $validator
     * @return bool
     */
    public function CountryCodes($attribute, $value, $parameters,Validator $validator){
        /* validate */
        return in_array($value,$this->allowed_countries);
    }

}

Then Add the message for country_codes in resources/lang/en/validation.php

<?php

    return [
        ...
        'country_codes'=>'Country code is invalid',
        ...
    ];

And now you are ready to use it!

<?php
    $input = [
        'country'=>'CH'
    ];
    $rules = [
        'country'=>'required|country_codes'
    ];

    $v = \Illuminate\Support\Facades\Validator::make($input,$rules);
    
    if($v->fails()){
        abort(403);
    }

 

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to post
Share on other sites

1 minute ago, leodaniel said:

So basically you just add them into your serviceprovider. So in your service provider. You can also use anonymus functions but I would always put them into a separate class. 


<?php
use Illuminate\Support\Facades\Validator;

...
  
public function boot(){
  /* I would put them always into a seperate class in my example MyValidationRules */
  Validator::extend('country_codes','App\\MyValidationRules@CountryCodes');
  
}

Then your class MyValidationRules. 


<?php
use Illuminate\Validation\Validator;

class MyValidationRules
{
    protected $allowed_countries = ['CH','DE'];

    /**
     * @param $attribute
     * @param $value
     * @param $parameters
     * @param Validator $validator
     * @return bool
     */
    public function CountryCodes($attribute, $value, $parameters,Validator $validator){
        /* validate */
        return in_array($value,$this->allowed_countries);
    }

}

Then Add the message for country_codes in resources/lang/en/validation.php


<?php

    return [
        ...
        'country_codes'=>'Country code is invalid',
        ...
    ];

And now you are ready to use it!


<?php
    $input = [
        'country'=>'CH'
    ];
    $rules = [
        'country'=>'required|country_codes'
    ];

    $v = \Illuminate\Support\Facades\Validator::make($input,$rules);
    
    if($v->fails()){
        abort(403);
    }

 

Ooooh. Now I see, thanks!

Back-end developer, electronics "hacker"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×