0

Iam just trying to validate some POST datas.

Route::post('/', function(){

$data = ['url' => request('url')];
$validation = Validator::make($data, ['url' => 'required|url'])->validate();

if($validation->fails())
{
    $dd('failed');
}

I don't understand why It doesn't work, can you help me please ?

6
  • 2
    It's dd('failed');. Remove the $. Commented Jan 8, 2020 at 14:41
  • 3
    ->validate() returns an array. You can't call {array}->fails(), as array doesn't have that method. I think you can just call $validator = Validator::make(...);, then if($validator->fails()){ ... } Commented Jan 8, 2020 at 14:41
  • thank you for answer. Can you tell how I can do ? I am learning Laravel and PHP its very complicated Commented Jan 8, 2020 at 14:44
  • 1
    Re-read my comment. I just told you what you can try... Remove the ->validate(). Commented Jan 8, 2020 at 14:44
  • ok tim lewis, thank you. how can I thank you I dont find the green flag Commented Jan 8, 2020 at 14:47

2 Answers 2

1

The error you're getting is due to the return type of ->validate(). This will return an array, so $validation will be an array instead of a Validator instance, and you can't call ->fails() on an array. To solve this, simply omit ->validate():

$validation = Validator::make($data, ['url' => 'required|url']);
if($validation->fails()){
  dd("Failed");
}

Sidenote; watch your syntax. $dd() is not a valid call.

Sign up to request clarification or add additional context in comments.

2 Comments

->validate() will return an array,if it was not failed;otherwise it throw exception
@Md.Amirozzaman Right; thanks for the clarification. Either way, ->validate()->fails() (either directly chained or via variable assignment) is not valid in either case :)
0

You may use the validate method provided by the Illuminate\Http\Request object directly as follow

$request->validate([
    'title' => 'required|unique:posts|max:255',
    'author.name' => 'required',
    'author.description' => 'required',
]);
//if fails code after this line will not be executed

This validation will auto redirected to back.This was default validation by laravel. If it not fails,it will continue with executing.

Manually

Now you can implement manually and redirect as your requirment.

use Validator;

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

    //checks your validation and redirect as you want

    if ($validator->fails()) {
          return redirect('where/ever/you/want')
                         ->withErrors($validator)
                         ->withInput();
     }

Again you can default redirection,by calling validate() method

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

If you call validate method it will redirect as default laravel.

dd() is a method,short version of die and dump

Useful link:

https://github.com/laravel/framework/blob/5.7/src/Illuminate/Validation/Validator.php#L312

1 Comment

@Julien Rns Neo check this answer,how validator works!before use validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.