आप के बाद से यह एक बहुत आम नियम यह है कि आप दूसरे पर उपयोग करने के लिए चाहते हो सकता है है इस के लिए एक कस्टम मान्यता नियम बना सकते हैं आपके ऐप का हिस्सा (या शायद आपकी अगली परियोजना पर)।
अपने
एप्लिकेशन/प्रदाताओं पर
/AppServiceProvider.php
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//Add this custom validation rule.
Validator::extend('alpha_spaces', function ($attribute, $value) {
// This will only accept alpha and spaces.
// If you want to accept hyphens use: /^[\pL\s-]+$/u.
return preg_match('/^[\pL\s]+$/u', $value);
});
}
संसाधनों में अपने कस्टम सत्यापन संदेश को परिभाषित करें/लैंग/एन /validation.php
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
// Custom Validation message.
'alpha_spaces' => 'The :attribute may only contain letters and spaces.',
'accepted' => 'The :attribute must be accepted.',
....
और उपयोग यह सामान्य रूप से
public function rules()
{
return [
'name' => 'required|alpha_spaces',
'email' => 'email|unique:users,email',
'password' => 'required',
'phone' => 'required|numeric',
'address' => 'required|min:5',
];
}
स्रोत
2016-07-25 09:30:08
यह 'AppServiceProvider.php' –
कहां ढूंढें और अपने 'ऐप/प्रदाता/AppServiceProvider.php' के शीर्ष पर' उपयोग करें \ Support \ Facades \ Validator; 'जोड़ने के लिए मत भूलना; –
आप भी कर सकते हैं सेवा प्रदाताओं में $ this-> ऐप का उपयोग करें और त्रुटि संदेश के रूप में एक तीसरा पैरामीटर जोड़ें। अनुवाद की आवश्यकता नहीं होने पर उपयोगी। $ this-> ऐप ['वैधकर्ता'] -> विस्तार ('my_rule', फ़ंक्शन ($ विशेषताएँ, $ मान, $ पैरामीटर) {वापसी (बूल) preg_match (...);}, ': मेरा कस्टम संदेश विशेषता दें। '); –