1

i am new to laravel.Here i have a form where i have to fill up a name field and send it to controllers store() method for validation. Otherwise it will show custom error.But whenever i submit the form with or without input i am getting the following error.

Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, string given, called in C:\xampp\htdocs\mylaravel\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 221 and defined

for experiment purpose i am catching the user input using the following format

$data = $request->input('name');

create.blade.php:

<h1>login form</h1>
      @if($errors->has())
        <div><span> Opps !! </span></br>
          <ul>
            @foreach ($errors->all() as $error)
                <li> {{ $error }}</li>    
            @endforeach
          </ul>
        </div>
      @endif
   {!!Form::open(array('url'=>'user','method'=>'POST', 'files'=>true)) !!}

   {!!Form::label('name','Your name')!!}
   {!!Form::text('name')!!}
   </br>

   {!!Form::submit('submit')!!}
    {!!Form::close()!!}

store() method in userController.php file:

public function store(Request $request)
{
    //
    $data = $request->input('name');

    $rules = array(
       'name' => 'unique:users,name|required|alpha_num'

    );

    // Create a new validator instance.
    $validator = Validator::make($data, $rules);
    if($validator->fails()){

        $errors=$validator->messages();
        return Redirect::route('user.create')->withErrors($validator);

    }else{

            return Redirect::route('user.index');
        }

    }

}

3 Answers 3

3

According to your error,Validator expects it parameters to be array, but you are passing a string there as $data= $request->input('name') . So, you should pass array in Validator::make() . Below code should work for you.

$validator = Validator::make($request->all(), [
    'name' => 'unique:users,name|required|alpha_num'
]);

Here is the doc if you want to explore more .

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

Comments

2

You need to pass array inside Validator::make.

Right now your are passing string in the form of $data variable.

For example :

$validator = Validator::make(
    array('name' => 'Dayle'),
    array('name' => 'required|min:5')
);

DOCS : https://laravel.com/docs/4.2/validation

Comments

2

you have pass your params as array in validation,so your code will be

public function store(Request $request)
{
//
    $data = $request->all();

$rules = array(
   'name' => 'unique:users,name|required|alpha_num'

);

// Create a new validator instance.
$validator = Validator::make($data, $rules);
if($validator->fails()){

    $errors=$validator->messages();
    return Redirect::route('user.create')->withErrors($validator);

}else{

        return Redirect::route('user.index');
    }

}

}

Comments

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.