2016-04-26 89 views
0

驗證錯誤消息我很肯定我缺少的只是一些小的,但該死的地獄無法弄清楚..請幫我傢伙:)Laravel 5.2定製與參數

我已經擴展AppServiceProvider .PHP

public function boot() 
    { 
     // 
     Validator::extend('ageLimit', 'App\Http\[email protected]'); 
    } 

我已經創造了新的CustomValidator.php

<?php 

namespace App\Http; 
use DateTime; 

class CustomValidator { 

    public function validateAgeLimit($attribute, $value, $parameters, $validator) 
    { 
     $today = new DateTime(date('m/d/Y')); 
     $bday = new DateTime($value); 

     $diff = $bday->diff($today); 
     $first_param = $parameters[0]; 

     if($diff->y >= $first_param){    
      return true; 
     }else{ 
      return false; 
     } 

    } 

} 

我添加編新線validation.php

/* 
|-------------------------------------------------------------------------- 
| Custom Validation Language Lines 
|-------------------------------------------------------------------------- 
| 
| Here you may specify custom validation messages for attributes using the 
| convention "attribute.rule" to name the lines. This makes it quick to 
| specify a specific custom language line for a given attribute rule. 
| 
*/ 
'age_limit' => ':attribute -> Age must be at least :ageLimit years old.', 

'custom' => [ 
    'attribute-name' => [ 
     'rule-name' => 'custom-message', 
    ], 
], 

這纔是我的規則:

'birth_date' => 'required|date|ageLimit:15', 

所有這一切工作正常...排除的參數:和ageLimit在validation.php文件..

我怎樣才能達到那裏參數15我在規則中傳遞?

因爲我得到這個消息:

Birth day -> Age must be at least :ageLimit years old. 

和肯定,我想獲得這樣的:

Birth day -> Age must be at least 15 years old. 

回答

1

下面你Validator::extend(...)您可以添加:

Validator::replacer('ageLimit', function($message, $attribute, $rule, $parameters) { 
    $ageLimit = $parameters[0]; 

    return str_replace(':ageLimit', $ageLimit, $message); 
}); 

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

希望這有助於!

+0

工程就像一個魅力!!!!! +1000謝謝高手:) –