xxxxxxxxxx
Accepted
Accepted If
Active URL
After (Date)
After Or Equal (Date)
Alpha
Alpha Dash
Alpha Numeric
Array
Bail
Before (Date)
Before Or Equal (Date)
Between
Boolean
Confirmed
Current Password
Date
Date Equals
Date Format
Declined
Declined If
Different
Digits
Digits Between
Dimensions (Image Files)
Distinct
Email
Ends With
Enum
Exclude
Exclude If
Exclude Unless
Exclude With
Exclude Without
Exists (Database)
File
Filled
Greater Than
Greater Than Or Equal
Image (File)
In
In Array
Integer
IP Address
JSON
Less Than
Less Than Or Equal
MAC Address
Max
MIME Types
MIME Type By File Extension
Min
Multiple Of
Not In
Not Regex
Nullable
Numeric
Password
Present
Prohibited
Prohibited If
Prohibited Unless
Prohibits
Regular Expression
Required
Required If
Required Unless
Required With
Required With All
Required Without
Required Without All
Required Array Keys
Same
Size
Sometimes
Starts With
String
Timezone
Unique (Database)
URL
UUID
xxxxxxxxxx
use Illuminate\Support\Facades\Validator;
// ....
// On your Store function
public function store(Request $request, $id)
// Validation
$validator = Validator::make($request, [
'name' => 'required',
'username' => 'required|unique:users,username,NULL,id,deleted_at,NULL',
'email' => 'nullable|email|unique:users,email,NULL,id,deleted_at,NULL',
'password' => 'required',
]);
// Return the message
if($validator->fails()){
return response()->json([
'error' => true,
'message' => $validator->errors()
]);
}
// ....
}
// On your Update function
public function update(Request $request, $id)
{
// Validation
$validator = Validator::make($input, [
'name' => 'required',
'username' => 'required|unique:users,username,' . $id. ',id,deleted_at,NULL',
'email' => 'nullable|email|unique:users,email,' . $id. ',id,deleted_at,NULL',
'roles' => 'required'
]);
// Return the message
if($validator->fails()){
return response()->json([
'error' => true,
'msg' => $validator->errors()
]);
}
// ....
}
xxxxxxxxxx
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
/**
* Store a new blog post.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Retrieve the validated input...
$validated = $validator->validated();
// Retrieve a portion of the validated input...
$validated = $validator->safe()->only(['name', 'email']);
$validated = $validator->safe()->except(['name', 'email']);
// Store the blog post...
}
}
xxxxxxxxxx
/**
* Store a new blog post.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// The blog post is valid...
}
xxxxxxxxxx
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
xxxxxxxxxx
public function store()
{
$this->validate(request(), [
'song' => [function ($attribute, $value, $fail) {
if ($value <= 10) {
$fail(':attribute needs more cowbell!');
}
}]
]);
}
xxxxxxxxxx
use Illuminate\Support\Facades\Validator;
$customMessage = [
'title.max' => "title is too large",
];
$rules = [
'id' => 'integer|exists:master_advert_bundles',
'title' => ['required', 'unique:posts', 'max:255'],
'body' => ['required']
];
$validate = validation($request->all(), $rules);
$validate = Validator::make($request->all(), $rules, $customMessage);
if ($validate->fails()) {
return $validate->messages();
}
xxxxxxxxxx
/*
accepted: The field under validation must be yes, on, 1, or true.
active_url: The field under validation must be a valid URL according to PHP's checkdnsrr function.
after: The field under validation must be a value after a given date.
alpha: The field under validation must be entirely alphabetic characters.
alpha_dash: The field under validation may have alpha-numeric characters, as well as dashes and underscores.
alpha_num: The field under validation must be entirely alpha-numeric characters.
array: The field under validation must be a PHP array.
before: The field under validation must be a value preceding the given date.
between: The field under validation must have a size between the given min and max.
confirmed: The field under validation must have a matching field of foo_confirmation.
date: The field under validation must be a valid date.
date_format: The field under validation must match the given format.
different: The field under validation must have a different value than field.
digits: The field under validation must be numeric and must have an exact length of value.
digits_between: The field under validation must have a length between the given min and max.
email: The field under validation must be formatted as an e-mail address.
exists: The field under validation must exist on a given database table.
filled: The field under validation must not be empty when it is present.
image: The file under validation must be an image (jpeg, png, bmp, gif, svg, or webp).
in: The field under validation must be included in the given list of values.
/*
xxxxxxxxxx
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Retrieve the validated input...
$validated = $validator->validated();
// Retrieve a portion of the validated input...
$validated = $validator->safe()->only(['name', 'email']);
$validated = $validator->safe()->except(['name', 'email']);
// Store the blog post...
}
xxxxxxxxxx
$request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'publish_at' => 'nullable|date',
]);