Post::rules PHP Method

rules() public method

public rules ( ) : array
return array validation rules for model attributes.
    public function rules()
    {
        return [['blog_id, slug,  title, content, status, publish_time', 'required', 'except' => 'search'], ['blog_id, create_user_id, update_user_id, status, comment_status, access_type, create_time, update_time, category_id', 'numerical', 'integerOnly' => true], ['blog_id, create_user_id, update_user_id, create_time, update_time, status, comment_status, access_type', 'length', 'max' => 11], ['lang', 'length', 'max' => 2], ['publish_time', 'length', 'max' => 20], ['slug', 'length', 'max' => 150], ['image', 'length', 'max' => 300], ['create_user_ip', 'length', 'max' => 20], ['description, title, link, keywords', 'length', 'max' => 250], ['quote', 'filter', 'filter' => 'trim'], ['link', 'yupe\\components\\validators\\YUrlValidator'], ['comment_status', 'in', 'range' => array_keys($this->getCommentStatusList())], ['access_type', 'in', 'range' => array_keys($this->getAccessTypeList())], ['status', 'in', 'range' => array_keys($this->getStatusList())], ['slug', 'yupe\\components\\validators\\YSLugValidator', 'message' => Yii::t('BlogModule.blog', 'Forbidden symbols in {attribute}')], ['title, slug, link, keywords, description, publish_time', 'filter', 'filter' => [$obj = new CHtmlPurifier(), 'purify']], ['slug', 'unique'], ['tags', 'safe'], ['tags', 'default', 'value' => []], ['id, blog_id, create_user_id, update_user_id, create_time, update_time, slug, publish_time, title, quote, content, link, status, comment_status, access_type, keywords, description, lang', 'safe', 'on' => 'search']];
    }

Usage Example

Example #1
0
 /**
  * Update the specified post in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update($id)
 {
     $post = Post::findOrFail($id);
     $file = Input::file('image');
     $category = Postcategory::find(Input::get('post_category_id'));
     $data = array('title' => ucwords(Input::get('title')), 'slug' => $this->slugify(Input::get('slug')), 'content' => Input::get('content'), 'excerpt' => Input::get('excerpt'), 'post_category_id' => Input::get('post_category_id'), 'status' => Input::get('status'), 'comment_status' => Input::get('comment_status'), 'social_status' => Input::get('social_status'), 'created_at' => Input::get('created_at'));
     $validator = Validator::make($data, Post::rules($id));
     if ($validator->fails()) {
         return Redirect::back()->withErrors($validator)->withInput();
     }
     if (Input::hasFile('image')) {
         // checking file is valid.
         if (Input::file('image')->isValid()) {
             $destinationPath = '/uploads/' . $category->slug;
             // upload path
             $extension = Input::file('image')->getClientOriginalExtension();
             // getting image extension
             $fileName = rand(1, 1000) . '_' . $data['slug'] . '.' . $extension;
             // renameing image
             Input::file('image')->move(public_path() . $destinationPath, $fileName);
             // uploading file to given path
             $data['image'] = $destinationPath . "/" . $fileName;
         } else {
             // sending back with error message.
             return Redirect::back()->with('errors', 'Uploaded file is not valid')->withInput();
         }
     }
     if (Input::hasFile('documents')) {
         $documents = Input::file('documents');
         foreach ($documents as $newdocument) {
             if ($newdocument !== NULL) {
                 // dd($newdocument);
                 // checking file is valid.
                 $destinationPath = '/uploads/documents';
                 // upload path
                 $fileName = rand(1, 1000) . '_' . $newdocument->getClientOriginalName();
                 // renameing image
                 $newdocument->move(public_path() . $destinationPath, $fileName);
                 // uploading file to given path
                 // Save the photo
                 $document = new Document();
                 $document->name = $fileName;
                 $document->path = $destinationPath . "/" . $fileName;
                 $document->save();
                 $post->documents()->save($document);
             }
         }
     }
     if (Input::has('related_members')) {
         $post->members()->sync(Input::get('related_members'));
     }
     $post->update($data);
     return Redirect::route('admin.posts.edit', $post->id)->with("message", "Data berhasil disimpan");
 }