Saturday, April 1, 2017

Laravel 5 Sanitizing Required Field Behavior

I am trying to sanitize the user input in my application following this article

Below is my request

class TestRequest extends Request
{

   public function authorize()
   {
      return true;
   }

   public function rules()
   {
       $this->sanitize();

      return [
          'title'=>'required|max:100'

      ];
   }

   public function sanitize()
   {

      $input = $this->all();
      if(!empty($input))
      {
        $input['title'] = trim(strip_tags($input['title']));
        $this->replace($input); 

      }

    }
}

Tough the title is required field, if I try to put <h1></h1> as input in the title field, as per the logic in sanitize() function the tags are stripped away, but an empty string is saved in the database. The required field validation in the rules in not taking any effect.

How to handle this?



via ZedBee

Advertisement