3

I have created a custom error function by creating a class;

<?php

class CoreValidator extends Illuminate\Validation\Validator
{
    public function validatePostcode($attribute, $value, $parameters = null)
    {
        $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
        if(preg_match($regex ,$value)) {
            return true;
        }
        return false;
    }
}

I reference it in my model;

public static $rules = array(
        'first_name' => 'required|Max:45',
        'surname' => 'required|Max:45',
        'address_line_1' => 'required|Max:255',
        'address_line_2' => 'Max:255',
        'address_line_3' => 'Max:255',
        'town' => 'required|Max:45',
        'county' => 'Max:45',
        'postcode' => 'required|Postcode',
        'phone_number' => 'required|Max:22'
    );

It has been registered in my global.php;

Validator::resolver(function($translator, $data, $rules, $messages) {
        return new CoreValidator($translator, $data, $rules, $messages);
    });

It all works well, but the error message it returns is

validation.postcode

How/where do I set a custom error message for this?
I have tried setting app/lang/en/validation.php with (neither work);

'custom' => array(
        "validation.postcode" => "my error message 1",
        "postcode" => "my error message 2"
    )

P.S. I know that there is a regex validation method already, but this problem is more generic for me.

2

6 Answers 6

7

I think I have cracked it.

I added the message to the main array in app/lang/en/validation.php, not into the custom sub-array.

return array(
    ...
    "url" => "The :attribute format is invalid.",
    "postcode" => "my error message 2",
    ...
)

If this isn't the correct way, then someone is free to correct me.

Sign up to request clarification or add additional context in comments.

6 Comments

I did it the same way and think also this is the correct way.
I'd very much like to know if that's correct also, it seems sketchy.
@uɐɥʇɐᴎ I just had this same issue - see my posted answer on how to correctly utilize the custom validation array.
This still doesn't work for me with a 3rd party custom rule :-( github.com/felixkiss/uniquewith-validator
File is in a slightly different place in v5: github.com/laravel/laravel/blob/master/resources/lang/en/…
|
3

You can use setCustomMessages() method to assign custom messages like the bellow code

<?php

class CoreValidator extends Illuminate\Validation\Validator
{

    private $custom_messages = array(
        "customvalidation" => "my error message.",
    );

    public function __construct($translator, $data, $rules, $messages = array(), $customAttributes = array())
    {
        parent::__construct($translator, $data, $rules, $messages, $customAttributes);
        $this->setCustomMessages($this->custom_messages);
    }

    public function validateCustomvalidation($attribute, $value, $parameters = null)
    {
        // validation code here
    }

}

Comments

2

maybe this code more better :

// for example I am using sub-array custom at default validation file, but you can do it in other file as you wishes.
..other..
'custom' => array(
        'email' => array(
            'required' => 'We need to know your e-mail address!',
        ),
        "required"         => "Hey!!! don't forget at :attribute field is required.",
    ),
..other..

// you can determine your custom languages at your wishes file
$messages = \Lang::get('validation.custom');

Validator::make($input, $rules, $messages);

1 Comment

if you are doing package development, this is the only good option. \Lang::get('package::validation.custom');
1

From documentation:

In some cases, you may wish to specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file.

'custom' => array(
    'email' => array(
        'required' => 'We need to know your e-mail address!',
    ),
),

That means, in your case,

'custom' => array(
    'postcode' => array(
        'PostCode' => 'error message for PostCode rule',
        'required' => 'error message for required rule',
    ),
),

Comments

1

If you want to utilize the custom validation messages array in app/lang/xx/validation.php, the correct way is as follows:

'custom' => array(
    'formFieldName' => array(
        'postcode' => 'error message for PostCode rule',
        'iamalwayslowercase' => 'error message for this rule'
    ),
),

Note that you use the name of the form field and then in the array you use the lowercased name of the rule.

3 Comments

If you wanted to keep your $messages with the $rules, you could also just have the following in your model (so you don't mess with the language files): public static $messages = array ( 'formFieldName.postcode' => 'error message for PostCode rule' );
This still doesn't work for me with a 3rd party custom rule :-( github.com/felixkiss/uniquewith-validator
@groovenectar I am unable to review your code, but I can definitely say the above works and have been using it for some time.
0

The code below also works perfectly, take note of the underscore on the index of the $customValidatorMessages array. Hope it helps someone :-)

class CoreValidator extends Illuminate\Validation\Validator
{
    /**
     * The array of custom validator error messages.
     *
     * @var array
     */
    protected $customValidatorMessages = array(); 

    public function validatePostcode($attribute, $value, $parameters = null)
    {
        $regex = "/^((GIR 0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9][0-9]?)|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY])))) [0-9][ABD-HJLNP-UW-Z]{2}))$/i";
        if(preg_match($regex ,$value)) {
            return true;
        }

        $this->customValidatorMessages['post_code'] = 'Postcode error message.';

        $this->setCustomMessages($this->customValidatorMessages);

        return false;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.