Display Error Message for Custom Validation in Laravel 4
laravel, laravel-4, php, validation
Solution
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.
Problem
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.