Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate without request in Laravel

I need to validate an array but without a request. In laravel docs validation is described like this:

$validator = Validator::make($request->all(), [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);

But I can't use $request because the data comes from an external api and the validation is not inside a controller. How can I validate this array? For example:

$validatedData = validate([
    'id' => 1,
    'body' => 'text'
], [
    'id' => 'required',
    'body' => 'required'
]);
like image 532
user3743266 Avatar asked Apr 14 '18 01:04

user3743266


People also ask

How can I validate my name in laravel?

php", to replace the :attribute name (input name) for a proper to read name (example: first_name > First name) . It seems very simple to use, but the validator doesn't show the "nice names". And the validation in the controller: $validation = Validator::make($input, $rules, $messages);

How many types of validation are there in laravel?

Each form request generated by Laravel has two methods: authorize and rules .

How do you validate exact words in laravel?

I know of at least two ways. // option one: 'in' takes a comma-separated list of acceptable values $rules = [ 'field' => 'in:hello', ]; // option two: write a matching regular expression $rules = [ 'field' => 'regex:^hello$', ];


1 Answers

Should be. Because $request->all() hold all input data as an array .

$input = [
    'title' => 'testTitle',
    'body' => 'text'
];


$input is your customs array.

$validator = Validator::make($input, [
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
]);
like image 104
Ariel Pepito Avatar answered Oct 16 '22 11:10

Ariel Pepito