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'
]);
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);
Each form request generated by Laravel has two methods: authorize and rules .
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$', ];
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',
]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With