0

我正在尝试在 Laravel 中验证表单。如果我没有输入所需的值,它会将我重定向回表单,但我$errors每次都保持不变

object(Illuminate\Support\ViewErrorBag)#198 (1) {
  ["bags":protected]=>
  array(0) {
  }
}

我的控制器

<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Category;
use App\Http\Requests\CategoryRequest;

class CategoriesController extends Controller {

public function store(CategoryRequest   $request) {
        Category::create($request->all());
        return redirect('cpl_biovideo/dashboard');

    }

}

我的请求

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class CategoryRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return TRUE;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:4|max:20'
        ];
    }
}

我的模特

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $fillable =   ['name'];
}

我的观点

<div class="card">
        <div class="card-header">
            <h2>Add Category</h2>
        </div>
        <?php
            echo "<pre>";
            var_dump($errors);
            echo "</pre>";
        ?>
        <form action="{{url('cpl_biovideo/save-category')}}" method="post">
            <input type="hidden" name="_token" value="{{csrf_token()}}" />
            <div class="card-body card-padding">

                <div class="form-group">
                    <div class="fg-line">
                        <input type="text" class="form-control input-lg" placeholder="Category Name" name="name">
                    </div>
                </div>

                <div class="form-group">
                    <div class="fg-line">

                        <div class="select">
                            <select class="form-control" name="parent_id">
                                <option value="">Parent Category</option>
                                <?php
                                foreach ($categories as $category) {
                                    ?>
                                    <option value="<?php echo $category->id ?>">
                                        <?php echo $category->name; ?>
                                    </option>
                                    <?php
                                }
                                ?>

                            </select>
                        </div>
                    </div>
                </div>

                <button type="submit" class="btn btn-primary btn-sm m-t-10">Submit</button>
            </div>    
        </form>

    </div>
4

2 回答 2

0

$this->validate在您的控制器中使用

public function store(CategoryRequest $request) {
  //validate the data
  $this->validate($request, [
    'name' => 'required|min:4|max:20'
  ]);
  
  //check
  dd($request->name);
});
于 2020-08-16T10:48:52.853 回答
0

你没有在这里给你的路线块。但是如果你使用 Laravel 5.2.27 或更高版本,请从你的路由组中删除“web”中间件。

于 2016-06-06T14:36:36.353 回答