Laravel显示验证错误和修剪名称

时间:2017-03-07 16:30:00

标签: jquery ajax laravel laravel-5 laravel-5.3

如果验证失败,我想向用户显示错误,它会发回状态代码为442的消息,但我想在div中显示错误消息。怎么能实现呢?它不需要用session('status')来完成。

ImageRequest

public function rules()
{   
return [
   'img' => 'file|image',
];   
}
public function messages()
{
    return [
        'img.image' => 'File type is not supported! Use files with extension .jpg/.jpeg/.gif',
    ];
}

控制器:

   public function testing(Requests\ImageRequest $request) {
        if($request->hasFile('img'));
        {
            $image = Input::file('img');
            $filename = time() . '.' . $image->getClientOriginalExtension();
            $path = public_path('images/' . $filename);
            Image::make($image->getRealPath())->resize(200, 200)->save($path);
            $file = $request->file('img');
            return ['url' => url('images/' . $filename)];
        }
           if($imgErrors = $errors->first('img'));
        {
           return redirect('template')->with('status', 'File type is not supported');
        }
        }

template.blade.php:

@if (session('status'))
<div id="mydiv" class="alert alert-success">
    {{ session('status') }}
</div>

JS:

function submitImage(){
    var fd = new FormData($("#upload_form")[0]);
        fd.append( 'img', $('#img') );

$.ajax({
      url:'template',
      data: fd,
      dataType:'json',
      async:false,
      type:'post',
      processData: false,
      contentType: false,
      success: function (data) {
        $("#image").attr("src", data.url);
                }
            });
        }

另一个问题是我正在验证网站的名称,我只想允许字母和空格中的字符,但是如果有空格我想将单词修剪成一个但是由于某种原因它不会这样做,并再次显示错误。

NameRequest

public function rules()
{
    return [
        'newName' => 'required|alpha',
    ];
}

控制器:

    public function postDB(Requests\NameRequest $request) {
    $newName = trim($request->input('newName'));
    $newLat = $request->input('newCode');
    $websites = new Website();
    $websites->name = $newName;
    $websites->html = $newLat;
    $websites->save();
    return redirect('template')->with('status', 'Website has been saved successfully!');
}

JS:

function updateDatabase()
{
    code2 = document.getElementById("content-link2").innerHTML;
    var name = document.getElementById("website_name").value;
    var newCode = document.getElementById('code').value = code2;
    var newName = document.getElementById('name').value = name;
    web_name = ($('#website_name').val());
        console.log(newName);
        console.log(newCode);
}

1 个答案:

答案 0 :(得分:0)

我发现向用户返回消息的最简单方法是使用Jeffrey Way构建的Flash

https://github.com/laracasts/flash

这可用于以错误类型向用户返回消息。

对于和示例,在控制器中,您可以执行类似这样的操作

public function myFunction(Request $request)
{
    // Do your validation with the request

    if ($valid) {
        Flash::success('Congrats! Everything was fine');
    } else {
        Flash::error('Oops! Something went wrong');
    }

    return redirect()->back()->withInput();
}

可以保留一个请求的内容并将其闪存到视图中以重新填充表单。我强烈建议您阅读Laravel文档,以便更好地理解。

https://laravel.com/docs/5.4/requests#old-input