Laravel study notes (6) form validation

  1. To add a form must be submitted @csrf, to prevent forgery form

    <form method="POST" action="/profile">
        @csrf
    
        ...
    </form>
    
  2. Forms can not be issued PUT, PATCH DELETE request and needs to be added to the hidden _method domain mimic these HTTP verbs. which is:@method('PUT')

    <form action="/foo/bar" method="POST">
        @method('PUT')
    
        ...
    </form>
    

3.old function gets the value stored in the one-time session is used in the form of the value attribute. So that when we output error callback page, enter the value of the original still exists.

Adding value = {{old ( 'name')}} In the form input, this variable is a variable flash

<input type="text" class="form-control" name="name" value={{old('name')}}>
  1. Method store respective logic controller write verification form. That the use of Illuminate \ Http \ validate method of the Request object provides. If validated, the code can be normal operation. If the verification fails, an exception is thrown, and automatically the corresponding error response returned to the user. In a typical case the HTTP request, it generates a redirect response, and a request for AJAX JSON response is sent.

    public function store(Request $request)
    {
        $validatedData = $request->validate([
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
    }
    

    If the verification fails, Laravel will automatically generate a corresponding response . It will automatically redirect the user to the previous position . In addition, all validation error messages will be automatically stored in the session . If verified, that our controller will continue to operate normally.

    However, it is not necessary to verify a route error messages in the GET explicitly bound to a view. Because Lavarel checks the error message in the Session data, and automatically bind it to view (if the view file exists). Of which $ errors variable is Illuminate \ Support \ MessageBag an instance of. ($ Errors variable can be called directly in the returned view, which contains the error message)

    Flash memory is a variable $ errors

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

    If you want to stop running a property validation rules after the first validation fails, you will need additional bail rules to the property:

    $request->validate([
        'title' => 'bail|required|unique:posts|max:255', // 添加了Bail规则
        'body' => 'required',
    ]);
    
  2. Common rules

rule Explanation
confirmed Verify fields and field values ​​must match the foo_confirmation. For example, if the field is a password to be validated, there must password_confirmation field matches the input.
size Validation field must have a size that matches the given value. For strings, value corresponding to the number of characters. For digital speaking, value corresponding to a given integer value. For For arrays, size corresponding to the count value array. The document is, size corresponds to the file size (kb).
max Verification field must be less than or equal value. Calculation strings, numbers, or the file size of the array are evaluated by the method of size
me Verification field must have a minimum value. Calculation strings, numbers, or the file size of the array are evaluated by the method of size
unique unique: table, column, except, idColumn authentication field must be unique within a given database table. If you do not specify a column, use the name of the field itself will
required Validation field must exist in the input data, rather than empty. If one of the following conditions are met, then the field is considered "empty": the value is null value is an empty string. This value may be null or empty array number of objects. This is not the path to upload files.
email Field must meet the verification e-mail address format.
sometimes Only when the field is present, perform field validation fishes
nullable If you do not want the program to verify the null value as invalid, it would be "optional" request fields are marked as nullable, it can be understood as if there verification value.
$request->validate([
    'title' => 'required|unique:posts|max:255',
    'body' => 'required',
    'publish_at' => 'nullable|date',
]);

In this example, we specify publish_at field can be null or a valid date format. If nullable qualifiers have not been added to the rule definition, validation will consider null is an invalid date format.

  1. Error language pack (default is English)
    default form prompts in English, we can install Chinese language pack be finished.

    composer require caouecs/laravel-lang:~3.0
    

    Comprising most languages, the language pack located vendor / caouecs / larvel-lang / src directory.

    use

    According to need to copy the language packs to resources / lang directory.

    Modify config / app.php profile

    	'locale' => 'zh-CN',
    
Published 40 original articles · won praise 0 · Views 787

Guess you like

Origin blog.csdn.net/qj4865/article/details/104170375