# စိစစ္ျခင္း
Laravel အေနျဖင့္ data မ်ားကုိ စိစစ္ရာတြင္ ရုိးရွင္း အဆင္ေျပေသာ နည္းလမ္းမ်ားကုိ အသုံးျပဳထားသည္။ error message မ်ားကုိ Validation class မွ တဆင့္ ထုတ္ယူႏုိင္သည္။
$validator = Validator::make(
array('name' => 'Dayle'),
array('name' => 'required|min:5')
);
Validation ျပဳလုပ္ရာတြင္ make method ဟုသည့္ method ကုိ အသုံးျပဳျပီး array တြင္းပါရွိမည့္ ပထမ argument မွာ data ျဖစ္ျပီး ဒုတိယ argument မွာ ထုိ data မ်ားကုိ စိစစ္မည့္ rule မ်ားကို ထည့္သြင္းရမည္။
တခုထက္ပုိေသာ rule မ်ားကို သတ္မွတ္လုိပါက "pipe" character ကုိျဖစ္ေစ array အတြင္း ျခား၍ျဖစ္ေစ ထည့္သြင္းႏုိင္သည္။
$validator = Validator::make(
array('name' => 'Dayle'),
array('name' => array('required', 'min:5'))
);
$validator = Validator::make(
array(
'name' => 'Dayle',
'password' => 'lamepassword',
'email' => '[email protected]'
),
array(
'name' => 'required',
'password' => 'required|min:8',
'email' => 'required|email|unique:users'
)
);
Validator instance ကုိ ျပဳလုပ္ျပီးပါက fails သုိ ့မဟုတ္ passes method ကုိ အသုံးျပဳ၍ အခ်က္အလက္မ်ား စိစစ္ႏုိင္သည္။
if ($validator->fails())
{
// The given data did not pass validation
}
စိစစ္ျခင္း မေအာင္ျမင္ပါက validator မွ error message ကုိ ရယူႏုိင္ေပသည္။
$messages = $validator->messages();
္fail ျဖစ္သည့္ rule မ်ားကုိသာ ရယူလုိျပီး message မ်ား မပါဝင္ေစလုိပါက failed method ကုိ အသုံးျပဳႏုိင္သည္။
$failed = $validator->failed();
Validator class အေနျဖင့္ size ႏွင့္ mimes အပါအဝင္ မ်ားေျမာင္လွေသာ validation method မ်ားကုိ အေထာက္အပံ့ေပးထားျပီး file မ်ား validate ျပဳလုပ္လုိပါက ထုိထဲ့သုိ ့ ထည့္သြင္းရန္သာ လုိေပမည္။
After calling the on a
Validator instance မွ messages method ကုိ ေခၚျပီးပါက Error message မ်ားျဖင့္ အလုပ္လုပ္ရာတြင္ လြယ္ကူေစမည့္ method မ်ားစြာပါဝင္မည့္ MessageBag ပါဝင္မည္ ျဖစ္သည္။
echo $messages->first('email');
foreach ($messages->get('email') as $message)
{
//
}
foreach ($messages->all() as $message)
{
//
}
if ($messages->has('email'))
{
//
}
echo $messages->first('email', '<p>:message</p>');
မွတ္ခ်က္: ပုံမွန္အားျဖင့္ messages မ်ားကုိ Bootstrap ျဖင့္ အဆင္ေျပမည့္ ပုံစံမ်ားအေနျဖင့္ သတ္မွတ္ထားပါသည္။
foreach ($messages->all('<li>:message</li>') as $message)
{
//
}
Validation ကုိ ေဆာင္ရြက္ျပီးသည္ႏွင့္ Error message မ်ားကုိ လြယ္လင့္တကူ ျပန္လည္ျပသႏုိင္ရန္ လုိအပ္ေပသည္။ ထုိလုိအပ္ခ်က္မ်ားကုိ Laravel မွ အဆင္ေျပလြယ္ကူစြာ ျဖည့္စြမ္းထားသည္။ ေအာက္ပါ route မ်ားကုိ ဥပမာ အေနျဖင့္ၾကည့္ပါ။
Route::get('register', function()
{
return View::make('user.register');
});
Route::post('register', function()
{
$rules = array(...);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails())
{
return Redirect::to('register')->withErrors($validator);
}
});
Note that when စိစစ္ျခင္း မေအာင္ျမင္ပါက Validator instance ကုိ withErrors method ျဖင့္ Error မ်ားကို passing ေပးလုိက္ျပီး Redirect ျပဳလုပ္လုိက္သည္ ကုိ ေတြ ့ရေပမည္။ အဆုိပါ method ကုိ အသုံးျပဳျခင္းျဖင့္ error message မ်ားကို လြယ္လင့္တကူ ျဖတ္ကနဲ ျပသရာတြင္ သုံးႏုိင္ရင္ next request ၏ Session ထဲတြင္ ထည့္သြင္းထားပါသည္။
သုိ ့ပင္ေသာ္ညား GET route နဲ ့ Error Message မ်ားကုိ အေသခ်ည္ေႏွာင္ထားရန္ မလုိသည္ကို သတိျပဳရမည္။ အဘယ္ေၾကာင့္ဆုိေသာ္ Laravel သည္ Session data မ်ားမွ Error မ်ားကုိ စစ္ေဆးျပီး view ဆီသုိ ့ အဆင္ေျပသည္ႏွင့္ တျပိဳင္နက္ ျပသႏုိင္ရန္ ျပင္ဆင္ထားသည္ကုိ သတိျပဳရမည္။ **ထုိေၾကာင့္ အေရးၾကီးသည့္ အခ်က္မွာ$errors ဟုသည္ variable မွာ သင့္ view ၏ request တုိင္းတြင္ ျပင္ဆင္ထားေသာေၾကာင့္ အျမဲတမ္း အဆင္သင့္ ျဖစ္ေနသည္ကုိ မွတ္ထားရန္လုိသည္။ $errors variable မွာ MessageBag ၏ instance ျဖစ္သည္။
ထုိေၾကာင့္ redirect ျပဳလုပ္ျပီးေနာက္ $errors variable ႏွင့္ သင့္ view မွာ အလုိအေလ်ာက္ ခ်ည္ေႏွာင္ျပီးသား ျဖစ္ေပသည္။
<?php echo $errors->first('email'); ?>
သင့္အေနျဖင့္ Page တစ္ခုတည္းတြင္ မ်ားျပားလွေသာ form မ်ားသည္ရွိသည္ ဆုိပါစုိ ့။ ထုိအခါ သင့္အေနျဖင့္ Error မ်ား၏ MessageBag မ်ားကုိ ကြဲျပားျခားနား ေစရန္ အမည္နာမ ေပးလုိေပမည္။ ထုိအခါတြင္ သင့္အေနျဖင့္ withErrors ဟုသည့္ method ၏ ဒုတိယ argument အေနျဖင့္ မိမိေပးလုိသည့္ အမည္ကုိ ထည့္သြင္းႏုိင္သည္။
return Redirect::to('register')->withErrors($validator, 'login');
ထုိေနာက္ သင့္အေနျဖင့္ $errors variable မွ MessageBag instance ကုိ ေအာက္ပါအတုိင္း ဆြဲထုတ္ႏုိင္သည္။
<?php echo $errors->login->first('email'); ?>
ေအာက္တြင္ ေဖာ္ျပထားသည္မွာ အသုံးျပဳႏုိင္ေသာ စိစစ္ေရး rule မ်ားႏွင့္ ၄င္းတုိ ့၏ function မ်ားျဖစ္ၾကသည္။
အဆုိပါ field တြင္ စိစစ္သည္မွာ yes, on, သုိ ့မဟုတ္ 1 တုိ ့ျဖစ္သည္။ "Terms of Service" ကဲ့သုိ ့ တခုသာ ေရြးမေရြး စိစစ္ရာေနရာမ်ားတြင္ ၄င္းကုိ အသုံးျပဳႏုိင္သည္။
အဆုိပါ field တြင္ စိစစ္သည္မွာ checkdnsrr ဟုသည္ PHP function ကုိ အသုံးျပဳ၍ အင္ထုထားသည့္ URL ဟုတ္မဟုတ္ကို စစ္ေဆးသြားမည္ ျဖစ္သည္။
အဆုိပါ field တြင္ စိစစ္သည္မွာ သတ္မွတ္ထားေသာ date အတြင္းတြင္သာ ထည့္သြင္းေစရႏ္ ျဖစ္သည္။ date မ်ားကုိ PHP ၏ strtotime function ကုိ အသုံးျပဳ၍ ေျပာင္းလဲကာ စိစစ္သြားမည္ ျဖစ္သည္။
အဆုိပါ field တြင္ ပါဝင္ေသာ အခ်က္အလက္မ်ားသည္ အကၡရာ မ်ားသာ ျဖစ္ရမည္ ျဖစ္သည္။ ဥပမာ ကိန္းဂဏန္းမ်ားကို လက္ခံသြားမည္ မဟုတ္။
အဆုိပါ field တြင္ ပါဝင္ေသာ အခ်က္အလက္မ်ားသည္ အကၡရာ ႏွင့္ ကိန္းဂဏန္းမ်ားသာ မက dash ႏွင့္ underscore ကုိပါ လက္ခံသြားမည္ ျဖစ္သည္။
အဆုိပါ field တြင္ ပါဝင္ေသာ အခ်က္အလက္မ်ားသည္ အကၡရာ ႏွင့္ ကိန္းဂဏန္းမ်ားသာ လက္ခံသြားမည္။
အဆုိပါ field တြင္ ပါဝင္ေသာ အခ်က္အလက္မ်ားသည္ array အမ်ိဳးအစားကိုသာ လက္ခံသြားမည္။
အဆုိပါ field တြင္ပါဝင္ေသာ အခ်က္အလက္မ်ားကို date ျဖင့္ စိစစ္သတ္မွတ္ျခင္း ျဖစ္သည္။ dates မ်ားကုိ PHP မွ strtotime function ကုိ အသုံးျပဳ၍ passing ေပးသြားမည္ ျဖစ္သည္။
အဆုိပါ field တြင္ထည့္သြင္းေသာ အခ်က္အလက္မ်ား ၏ အမ်ားဆုံးႏွင့္ အနည္းဆုံး တန္ဖုိးမ်ားကုိ သတ္မွတ္ျခင္း ျဖစ္ျပီး String ၊ numeric ႏွင့္ file မ်ားကို size rule ကုိ အသုံးျပဳသကဲ့သုိ ့ ဆင္တင္တင္ပင္ ျဖစ္သည္။
အဆုိပါ field ၏ အခ်က္အလက္သည္ ရည္ညြန္း field ၏ အခ်က္အလက္ ဥပမာ foo_confirmation ႏွင့္ တူညီရမည္ ျဖစ္သည္။ ဥပမာ ျပဳရေသာ္ password field သည္ password_confirmation field ႏွင့္ ထပ္တူညီရမည္ ျဖစ္သည္။
တိက် မွန္ကန္ေသာ date ျဖစ္ေစရန္ စိစစ္ေပးျပီး strtotime ဟူေသာ PHP function ကုိ အသုံးျပဳထားသည္။
အဆုိပါ field မွ format ႏွင့္ တူညီရမည္ ျဖစ္ျပီး date_parse_from_format ဟူသည္ PHP function ကုိ အသုံးျပဳထားသည္။
အဆုိပါ field သည္ အျခား ရည္ညြန္း field ႏွင့္ လုံးဝ ကြဲျပားျခားရမည္ ျဖစ္သည္။ The given field must be different than the field under validation.
အဆုိပါ file တြင္ numeric value ျဖစ္ျပီး တိက်ေသခ်ာေသာ ဂဏန္း အလုံးအေရအတြက္ ကုိသာ ထည့္သြင္းရမည္ျဖစ္သည္။
အဆုိ field တြင္ min and max အၾကား ထည့္သြင္းရေသာ ဂဏန္းအလုံးအေရအတြက္ကုိသာ ထည့္သြင္းခြင့္ရမည္ျဖစ္သည္။
အဆုိပါ field တြင္ email address format အတုိင္း ထည့္သြင္းထားျခင္း ရွိမရွိ စစ္ေဆးသြားမည္ ျဖစ္သည္။
The field under validation must exist on a given database table.
'state' => 'exists:states'
'state' => 'exists:states,abbreviation'
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'exists:staff,email,account_id,1'
Passing NULL as a "where" clause value will add a check for a NULL database value:
'email' => 'exists:staff,email,deleted_at,NULL'
The file under validation must be an image (jpeg, png, bmp, or gif)
The field under validation must be included in the given list of values.
The field under validation must have an integer value.
The field under validation must be formatted as an IP address.
The field under validation must be less than or equal to a maximum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The file under validation must have a MIME type corresponding to one of the listed extensions.
'photo' => 'mimes:jpeg,bmp,png'
The field under validation must have a minimum value. Strings, numerics, and files are evaluated in the same fashion as the size rule.
The field under validation must not be included in the given list of values.
The field under validation must have a numeric value.
The field under validation must match the given regular expression.
Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character.
The field under validation must be present in the input data.
The field under validation must be present if the field field is equal to value.
The field under validation must be present only if any of the other specified fields are present.
The field under validation must be present only if all of the other specified fields are present.
The field under validation must be present only when any of the other specified fields are not present.
The field under validation must be present only when the all of the other specified fields are not present.
The given field must match the field under validation.
The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.
The field under validation must be unique on a given database table. If the column option is not specified, the field name will be used.
'email' => 'unique:users'
'email' => 'unique:users,email_address'
'email' => 'unique:users,email_address,10'
You may also specify more conditions that will be added as "where" clauses to the query:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.
The field under validation must be formatted as an URL.
Note: This function uses PHP's
filter_varmethod.
In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list:
$v = Validator::make($data, array(
'email' => 'sometimes|required|email',
));
In the example above, the email field will only be validated if it is present in the $data array.
Sometimes you may wish to require a given field only if another field has a greater value than 100. Or you may need two fields to have a given value only when another field is present. Adding these validation rules doesn't have to be a pain. First, create a Validator instance with your static rules that never change:
$v = Validator::make($data, array(
'email' => 'required|email',
'games' => 'required|numeric',
));
Let's assume our web application is for game collectors. If a game collector registers with our application and they own more than 100 games, we want them to explain why they own so many games. For example, perhaps they run a game re-sell shop, or maybe they just enjoy collecting. To conditionally add this requirement, we can use the sometimes method on the Validator instance.
$v->sometimes('reason', 'required|max:500', function($input)
{
return $input->games >= 100;
});
The first argument passed to the sometimes method is the name of the field we are conditionally validating. The second argument is the rules we want to add. If the Closure passed as the third argument returns true, the rules will be added. This method makes it a breeze to build complex conditional validations. You may even add conditional validations for several fields at once:
$v->sometimes(array('reason', 'cost'), 'required', function($input)
{
return $input->games >= 100;
});
Note: The
$inputparameter passed to yourClosurewill be an instance ofIlluminate\Support\Fluentand may be used as an object to access your input and files.
If needed, you may use custom error messages for validation instead of the defaults. There are several ways to specify custom messages.
$messages = array(
'required' => 'The :attribute field is required.',
);
$validator = Validator::make($input, $rules, $messages);
Note: The
:attributeplace-holder will be replaced by the actual name of the field under validation. You may also utilize other place-holders in validation messages.
$messages = array(
'same' => 'The :attribute and :other must match.',
'size' => 'The :attribute must be exactly :size.',
'between' => 'The :attribute must be between :min - :max.',
'in' => 'The :attribute must be one of the following types: :values',
);
Sometimes you may wish to specify a custom error messages only for a specific field:
$messages = array(
'email.required' => 'We need to know your e-mail address!',
);
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!',
),
),
Laravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using the Validator::extend method:
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
The custom validator Closure receives three arguments: the name of the $attribute being validated, the $value of the attribute, and an array of $parameters passed to the rule.
You may also pass a class and method to the extend method instead of a Closure:
Validator::extend('foo', 'FooValidator@validate');
Note that you will also need to define an error message for your custom rules. You can do so either using an inline custom message array or by adding an entry in the validation language file.
Instead of using Closure callbacks to extend the Validator, you may also extend the Validator class itself. To do so, write a Validator class that extends Illuminate\Validation\Validator. You may add validation methods to the class by prefixing them with validate:
<?php
class CustomValidator extends Illuminate\Validation\Validator {
public function validateFoo($attribute, $value, $parameters)
{
return $value == 'foo';
}
}
Next, you need to register your custom Validator extension:
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new CustomValidator($translator, $data, $rules, $messages);
});
When creating a custom validation rule, you may sometimes need to define custom place-holder replacements for error messages. You may do so by creating a custom Validator as described above, and adding a replaceXXX function to the validator.
protected function replaceFoo($message, $attribute, $rule, $parameters)
{
return str_replace(':foo', $parameters[0], $message);
}
If you would like to add a custom message "replacer" without extending the Validator class, you may use the Validator::replacer method:
Validator::replacer('rule', function($message, $attribute, $rule, $parameters)
{
//
});