where did the class validate come from ??

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

where did the class validate come from ??

Post by gautamz07 »

I was just going through this article here on how to upload images in laravel: http://devartisans.com/articles/image-upload-laravel-5 .

I see the below function in the article:

Code: Select all

public function store(Request $request)
	{
        $image = new Image();
        $this->validate($request, [
            'title' => 'required',
            'image' => 'required'
        ]);
        $image->title = $request->title;
        $image->description = $request->description;
		if($request->hasFile('image')) {
            $file = Input::file('image');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
            
            $name = $timestamp. '-' .$file->getClientOriginalName();
            
            $image->filePath = $name;

            $file->move(public_path().'/images/', $name);
        }
        $image->save();
        return $this->create()->with('success', 'Image Uploaded Successfully');
	}
I don't quite understand the below method:

Code: Select all

 $this->validate($request, [
            'title' => 'required',
            'image' => 'required'
  ]);
where did the validate method come into the class ??

I believe that line of code is checking if the fields title and image are populated , but where did the class validate come from ??

Thank you.
Gautam.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: where did the class validate come from ??

Post by requinix »

It's a method on the parent Controller class (or perhaps a superclass of it). I'm not sure where Controller is defined.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: where did the class validate come from ??

Post by Celauran »

https://laravel.com/api/5.3/Illuminate/ ... uests.html

It's a trait used by the base controller.
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Re: where did the class validate come from ??

Post by gautamz07 »

Thanks guys ! :)
Post Reply