请求验证,如何显示错误?

时间:2016-08-10 15:56:45

标签: php validation laravel

我有一个允许用户上传3个文件的表单。

这是Controller函数:

public function registerUpdate(CardAvsRequest $request){
        $id = Auth::user()->id;
        $first_name = User::find($id)->student->first_name;
        $last_name = User::find($id)->student->last_name;
        $name = $first_name . " " . $last_name;
        $message = "";
        if ($request->hasFile('carte-id'))
        {
            $image1 = $request->file('carte-id');
            if($image1->isValid())
            {
                if ($request->hasFile('avs'))
                {
                    $image2 = $request->file('avs');

                    if($image2->isValid())
                    {
                        if ($request->hasFile('permit'))
                        {
                            $image3 = $request->file('permit');

                            if($image3->isValid())
                            {
                                $path = config('card.path')."/$id";
                                $name = "carte-id.".$image1->getClientOriginalExtension();
                                $image1->move($path, $name);
                                $path = config('card.path')."/$id";
                                $name = "avs.".$image2->getClientOriginalExtension();
                                $image2->move($path, $name);
                                $path = config('card.path')."/$id";
                                $name = "permit.".$image3->getClientOriginalExtension();
                                $image3->move($path, $name);
                                $message = "Super ! Vous avez importé tous les fichiers nécessaires.";
                                //ici on dit dans la DB que l'utilisateur à uploadé tous les fichiers
                            }
                        }
                    }
                }
            }

        }
        return redirect()->route('account', $id)->with('message', $message);
    }

因此验证规则在CardAvsRequest.php中设置:

class CardAvsRequest 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 [
            'carte-id' => 'mimes:jpg,png,pdf,gif,jpeg,tiff,doc,docx,odt|max:10000',
            'avs' => 'mimes:jpg,png,pdf,gif,jpeg,tiff,doc,docx,odt|max:10000',
            'permit' => 'mimes:jpg,png,pdf,gif,jpeg,tiff,doc,docx,odt|max:10000',
        ];
    }
}

我想知道如果文件未经过验证,如何显示错误。

它不应该像这样工作吗?

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这是Laravel 5.2文档建议如何验证输入和返回错误(如果有的话)。视图看起来很好,如果发生则会显示错误。

public List<ElectricityBillSiteExceeding> GetAllElectricityBillSiteExceedings()
{
    var groupedBillSitesThatExceed = from billSites in _context.ElectricityBillSites

    join billMains in _context.ElectricityBillMains on billSites.ElectricityBillMainId equals
    billMains.ElectricityBillMainId

    join sites in _context.ElectricitySites on billSites.SiteNo equals sites.SiteNumber

    //TODO: group by total, mainId, siteNo, GroupInv, MaxAmt and Having no total = 0 and total > max

    select new 
    {
        groupInv = billMains.GroupInvoiceNumber,
        mainId = billMains.ElectricityBillMainId,
        siteNo = billSites.SiteNo,
        total = billSites.ElectricityBillSiteTotal,
        max = sites.MaximumAmount
    };

    //TODO: Map the result set of the linq to my model and return

    throw new NotImplementedException();
}

答案 1 :(得分:0)

是的,但在这种情况下,您必须记住您必须放置该代码:

@if (count($errors) > 0)
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

在表单文件中。