Home Backend Development PHP Tutorial laravel form validation

laravel form validation

Jul 29, 2016 am 09:12 AM
errors gt request requests

Use request method

<code>E:<span>\Laravel</span><span>\blog</span>>php artisan make:request ArticleRequest</code>
Copy after login

Generate ArticleRequest.php under app/http/request and modify it as follows

<code><?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

<span><span>class</span><span>ArticleRequest</span><span>extends</span><span>Request</span></span>
{
    <span>/**
     * Determine if the user is authorized to make this request.
     *
     * <span>@return</span> bool
     */</span>
    public function authorize()
    {
        <span>return</span><span>true</span>;
    }

    <span>/**
     * Get the validation rules that apply to the request.
     *
     * <span>@return</span> array
     */</span>
    public function rules()
    {
        <span>return</span> [
            <span>//</span><span>"name"</span>=><span>"<strong>require</strong>d"</span>,
        ];
    }
}
</code>
Copy after login

rules are verification rules
authorize To return true

<code>{<span>!</span><span>!</span><span>Form</span>::<span>open</span>([<span>"url"</span>=><span>"/site/store"</span>]) <span>!</span><span>!</span>}
{<span>!</span><span>!</span><span>Form</span>::<span>text</span>(<span>"name"</span>,<span>"aa"</span>,[<span>"class"</span>=><span>"form-controller"</span>,<span>"id"</span>=><span>"t"</span>]) <span>!</span><span>!</span>}
{<span>!</span><span>!</span><span>Form</span>::<span>submit</span>() <span>!</span><span>!</span>}
{<span>!</span><span>!</span><span>Form</span>::<span>close</span>() <span>!</span><span>!</span>}
{{--{{$<span>errors</span>}}--}}
{{--{<span>!</span><span>!</span> $<span>errors</span><span>!</span><span>!</span>}--}}
//用于显示验证不通过的错误信息
@<span>if</span>($<span>errors</span><span>-></span><span>any</span>())
    @<span>foreach</span>($<span>errors</span><span>-></span><span>all</span>() <span>as</span> $<span>err</span>)
        {<span>!</span><span>!</span> $<span>err</span><span>!</span><span>!</span>}
    @<span>endforeach</span>
@<span>endif</span></code>
Copy after login

The controller only needs RequestsArticleRequest. The parameter type can be the request above

<code><span>public</span><span><span>function</span><span>store</span><span>(Requests\ArticleRequest <span>$req</span>)</span>{</span>
        dd(<span>"fdsaf"</span>);
    }</code>
Copy after login

Execute the method after the verification is passed, without redirecting the form page

Use validate method

The request with normal controller parameters
The second parameter of validate is the same as the rules above

<code><span>public</span><span><span>function</span><span>store</span><span>(Request <span>$req</span>)</span>{</span><span>$this</span>->validate(<span>$req</span>,[<span>"name"</span>=><span>"<strong>require</strong>d"</span>]);
        dd(<span>"fdsaf"</span>);
    }</code>
Copy after login

Chinese problem

config/app.php

locale changed to Chinese

<code><span>'locale'</span> => <span>'zh'</span>,</code>
Copy after login

Copy the resources/lang/en folder in the project and change it to zh
Correspondingly modified to Chinese
If the field must be modified,

<code><span>'<strong>require</strong>d'</span>             => <span>'The :attribute field 必须.'</span>,</code>
Copy after login

').addClass('pre-numbering').hide(); in

resources/lang/zh/validation.php; $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces laravel form validation, including require aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

How to use the Python crawler Requests library How to use the Python crawler Requests library May 16, 2023 am 11:46 AM

1. Install the requests library. Because the learning process uses the Python language, Python needs to be installed in advance. I installed Python 3.8. You can check the Python version you installed by running the command python --version. It is recommended to install Python 3.X or above. After installing Python, you can directly install the requests library through the following command. pipinstallrequestsPs: You can switch to domestic pip sources, such as Alibaba and Douban, which are fast. In order to demonstrate the function, I used nginx to simulate a simple website. After downloading, just run the nginx.exe program in the root directory.

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

How Python uses Requests to request web pages How Python uses Requests to request web pages Apr 25, 2023 am 09:29 AM

Requests inherits all features of urllib2. Requests supports HTTP connection persistence and connection pooling, the use of cookies to maintain sessions, file uploading, automatic determination of the encoding of response content, and automatic encoding of internationalized URLs and POST data. Installation method uses pip to install $pipinstallrequestsGET request basic GET request (headers parameters and parmas parameters) 1. The most basic GET request can directly use the get method 'response=requests.get("http://www.baidu.com/&quot

How to use python requests post How to use python requests post Apr 29, 2023 pm 04:52 PM

Python simulates the browser sending post requests importrequests format request.postrequest.post(url,data,json,kwargs)#post request format request.get(url,params,kwargs)#Compared with get request, sending post request parameters are divided into forms ( x-www-form-urlencoded) json (application/json) data parameter supports dictionary format and string format. The dictionary format uses the json.dumps() method to convert the data into a legal json format string. This method requires

How to use the urllib.request.urlopen() function to send a GET request in Python 3.x How to use the urllib.request.urlopen() function to send a GET request in Python 3.x Jul 30, 2023 am 11:28 AM

How to use the urllib.request.urlopen() function in Python3.x to send a GET request. In network programming, we often need to obtain data from a remote server by sending an HTTP request. In Python, we can use the urllib.request.urlopen() function in the urllib module to send an HTTP request and get the response returned by the server. This article will introduce how to use

See all articles