为什么我在这个无效的 Laravel 8 表单中没有任何验证错误?

拉兹万赞菲尔

我正在使用Laravel 8和 Bootstrap 5 制作博客应用程序。

我在尝试验证我的添加文章表单时遇到了问题。它无法验证,这意味着即使未填写所需的表单数据,也不会出现错误消息

ArticleController控制器中,我有:

namespace App\Http\Controllers\Dashboard;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\Article;
use Illuminate\Http\Request;

class ArticleController extends Controller
{
    
    private $rules = [
        'category_id' => 'required|exists:categories,id',
        'title' => 'required|string',
        'short_description' => 'required|string',
        'content' => 'required|longText'
    ];

    private $messages = [
        'category_id.required' => 'Please pick a category for the article',
        'title.required' => 'Please provide a title for the article',
        'short_description.required' => 'The article needs a short description',
        'content.required' => 'Please add content'
    ];
    
    public function categories() {
        return ArticleCategory::all();
    }

    public function add(Request $request) {
        // Pass the categories to the articles form
        return view('dashboard/add-article',
            ['categories' => $this->categories()]
        );

    // Validate form (with custom messages)
    $validator = Validator::make($request->all(), $this->rules, $this->messages);

    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator->errors());
    }

    $fields = $validator->validated();

    // Data to be added
    $form_data = [
        'user_id' => Auth::user()->id,
        'category_id' => $fields['category_id'],
        'title' => $fields['title'],
        'slug' => Str::slug($fields['title'], '-'),
        'short_description' => $fields['short_description'],
        'content' => $fields['content'],
        'image' => $fields['image'],
        'featured' => $fields['featured']
    ];

    // Insert data in the 'articles' table
    $query = Article::create($form_data);

        if ($query) {
            return redirect()->route('dashboard.articles')->with('success', 'Article added');
        } else {
            return redirect()->route('dashboard.articles')->with('error', 'Adding article failed');
        }

    }

}

表格:

<form method="POST" action="{{ route('dashboard.articles.add') }}">
    @csrf
    <div class="row mb-2">
            <label for="title" class="col-md-12">{{ __('Title') }}</label>

            <div class="col-md-12 @error('title') has-error @enderror">
                    <input id="title" type="text" placeholder="Title" class="form-control @error('title') is-invalid @enderror" name="title" value="{{ old('title') }}" autocomplete="title" autofocus>

                    @error('title')
                            <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                            </span>
                    @enderror
            </div>
    </div>

    <div class="row mb-2">
            <label for="short_description" class="col-md-12">{{ __('Short description') }}</label>

            <div class="col-md-12 @error('short_description') has-error @enderror">
                    <input id="short_description" type="text" placeholder="Short description" class="form-control @error('short_description') is-invalid @enderror" name="short_description" value="{{ old('short_description') }}" autocomplete="short_description" autofocus>

                    @error('short_description')
                            <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                            </span>
                    @enderror
            </div>
    </div>

    <div class="row mb-2">
        <label for="category" class="col-md-12">{{ __('Category') }}</label>
    
        <div class="col-md-12 @error('category_id') has-error @enderror">
    
            <select name="category_id" id="category" class="form-control @error('category_id') is-invalid @enderror">
                <option value="0">Pick a category</option>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}">{{ $category->name }}</option>
                @endforeach
            </select>
                
    
                @error('category_id')
                        <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                        </span>
                @enderror
        </div>
    </div>

    <div class="row mb-2">
        <div class="col-md-12 d-flex align-items-center switch-toggle">
                <p class="mb-0 me-3">Featured article?</p>
                <input class="mt-1" type="checkbox" name="featured" id="featured">
                <label class="px-1" for="featured">{{ __('Toggle') }}</label>
        </div>
    </div>

    <div class="row mb-2">
        <label for="image" class="col-md-12">{{ __('Article image') }}</label>
    
        <div class="col-md-12 @error('image') has-error @enderror">
            <input type="file" name="image" id="file" value="{{ old('image') }}" class="file-upload-btn">
    
            @error('image')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>

    <div class="row mb-2">
        <label for="content" class="col-md-12">{{ __('Content') }}</label>

        <div class="col-md-12 @error('content') has-error @enderror">

            <textarea name="content" id="content" class="form-control @error('content') is-invalid @enderror" placeholder="Content" cols="30" rows="6">{{ old('content') }}</textarea>

            @error('content')
                    <span class="invalid-feedback" role="alert">
                            <strong>{{ $message }}</strong>
                    </span>
            @enderror
        </div>
    </div>
    
    <div class="row mb-0">
            <div class="col-md-12">
                    <button type="submit" class="w-100 btn btn-primary">
                            {{ __('Save') }}
                    </button>
            </div>
    </div>
</form>

路线:

// Article routes
Route::group(['prefix' => 'articles'], function() {
  Route::get('/', [ArticleController::class, 'index'])->name('dashboard.articles');
  Route::match(['get', 'post'],'/add', [ArticleController::class, 'add'])->name('dashboard.articles.add');
  Route::get('/delete/{id}', [ArticleController::class, 'delete'])->name('dashboard.articles.delete');
});

为什么我的表单无法验证?

斯内皮

public function add()从返回声明开始。

其余代码无关紧要,因为此时您从函数返回。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在这个SpringAOP语法错误@Pointcut没有弹出任何错误?

为什么在这个解析器序列中出现类型错误(Erik Meijer的第8讲)?

为什么我在这个kubernetes示例中得到CrashLoopBackOff?

Angular8:为什么我在这些Angular Material卡之间没有空间?

为什么在这个简单的示例中我的背景定位错误

为什么我在这个div中没有滚动?

为什么我的 Django 表单没有引发任何错误?

为什么我的 POST 在这个简单的 VueJS 表单中失败?

我现在没有,为什么我在这个 sql 请求中有错误

为什么我的 QLabel 小部件没有出现在这个线程中?

我不知道为什么这个错误会出现在 Laravel 中?

为什么我在这个 TypeScript 箭头函数中得到这个错误?

为什么在这个方法中声明这个变量会覆盖我的类成员(C++)?

这个 Laravel 8 路由有什么问题?

为什么我在这个双向链表中出现错误?

为什么在 laravel 8 中 foreach 不起作用,它没有循环

Laravel 8:为什么会话没有提交

如果表单中没有编辑任何内容,Laravel 8 更新数据透视表

是什么导致在这个 Laravel 8 应用程序中无法从服务器物理删除图像?

Laravel 8:为什么 dd() 没有出现在方法中

运行片8。为什么我收到这个错误。线程与它有什么关系?

为什么我的代码在这个异步函数中没有等待输出?

我不明白为什么在这个给定的程序中 a 的值没有增加

为什么我的函数没有在这个类中定义?

为什么我在这个 Bash 脚本中收到“没有这样的文件或目录”错误?

Laravel 8 中的 put 方法没有任何更新

为什么 old() 方法在这种 Laravel 8 形式中失败?

为什么我的 while 循环在这个 switch 案例中没有重复?

当我没有在这个 pygame 脚本中按下任何东西时,为什么精灵图像没有重置为默认值?