Support the ongoing development of Laravel.io →
Input Forms Validation
Last updated 2 years ago.
0

Try

/Validator::make($rules);

or even better, use the controller validation as described here

http://laravel.com/docs/5.0/validation

i.e.

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required|unique|max:255',
        'body' => 'required',
    ]);
    // do something, e.g. store
}

All provided that you are working with L5, but I assume that from the error.

Last updated 9 years ago.
0

In L5 you should validate the data before it hits the controller, makes life a lot easier.

Have a google for "Requests". For an easy example

/app/Http/Requests/PriorityRequest.php

namespace ServiceDesk\Http\Requests;

use ServiceDesk\Http\Requests\Request;

class PriorityRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
	if ( ! \Auth::check() )
	        {
    	    return false;
	        }
            return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
     public function rules()
     {
	  return [
              'title' => "required|max:255",
                  'description' => "required"
   	  ];
     }

         public function messages()
         {
              return [
                  'title.required' => 'Please provide a title.',
                  'description.required' => 'Please provide a description.'
             ];
          }
}

The in the store method of my RESTful controller I have

use Illuminate\Http\Request;
use ServiceDesk\Http\Requests\PriorityRequest;
...

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store( PriorityRequest $request )
{
    ....
}

If validation fails the request is automatically redirected back to the calling page without ever hitting the controller.

Hope it helps.

Last updated 9 years ago.
0

You can use the package Laravel 5 Javascript Validation. This package enables transparent client-side Javascript Valditaion in your views based on JQuery Validation Plugin

This is a basic example of how to reuse your validation rules in the controller .

PostController.php

namespace App\Http\Controllers;

class PostController extends Controller {

    /**
     * Define your validation rules in a property in 
     * the controller to reuse the rules.
     */
    protected $validationRules=[
                'title' => 'required|unique|max:255',
                'body' => 'required',
    ];
    
    /**
     * Show the edit form for blog post
     * We create a JsValidator instance based on shared validation rules
     * @param  string  $post_id
     * @return Response
     */
    public function edit($post_id)
    {
        $validator = JsValidator::make($this->validationRules);
        $post = Post::find($post_id);
    
        return view('edit_post')->with([
            'validator' => $validator,
            'post' => $post
        ])    
   
    }
    
    
    /**
     * Store the incoming blog post.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        $v = Validator::make($request->all(), $this->validationRules]);
    
        if ($v->fails())
        {
            return redirect()->back()->withErrors($v->errors());
        }
    
        // do store stuff
    }
}

In the view you simply should print the validator object passed to the view. Remember that this package depends of JQuery and you have to include before that jsvalidation.js

edit_post.balde.php

 <div class="container">
     <div class="row">
         <div class="col-md-10 col-md-offset-1">
             <form class="form-horizontal" role="form" method="POST" action="" id="ddd">
                 <div class="form-group">
                     <label class="col-md-4 control-label">Title</label>
                     <div class="col-md-6">
                         <input type="text" class="form-control" name="title">
                     </div>
                 </div>
                 <div class="form-group">
                     <label class="col-md-4 control-label">Array</label>
                     <div class="col-md-6">
                         <textarea name="body"></textarea>
                     </div>
                 </div>
             </form>
         </div>
     </div>
 </div>
 <!-- Scripts -->
 <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
 <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
 
 <!-- Laravel Javascript Validation -->
 <script type="text/javascript" src="{{ asset('vendor/jsvalidation/js/jsvalidation.js')}}"></script>
 {!! $validator !!}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

hayahay hayahay Joined 3 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.