getting_started_with_laravel

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
getting_started_with_laravel [03.12.2020 15:29] Pascal Sutergetting_started_with_laravel [03.12.2020 16:10] – [Form validation] Pascal Suter
Line 148: Line 148:
   * foreign keys in should be named in the singular, lowercase and underscored form of the target table plus ''_id'' appended to it. so for example ''notebook_part_id'' if the target table is ''notebook_parts'' or ''pizza_id'' if the target table is ''pizzas''    * foreign keys in should be named in the singular, lowercase and underscored form of the target table plus ''_id'' appended to it. so for example ''notebook_part_id'' if the target table is ''notebook_parts'' or ''pizza_id'' if the target table is ''pizzas'' 
  
 +
 +===== Form validation =====
 +Laravel provides a nice Method for validating forms. Typically the validator is added in the Controller ''store()'' method. here is an example: 
 +<code php>
 +    public function store(Request $request){
 +        $validData = $request->validate([
 +            'name' => ['required', 'string', 'max:255'],
 +            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
 +            'password' => ['required', 'string', 'min:8', 'confirmed'],
 +        ]);
 +        // anything from here on will only be executed if the above validation was successful
 +     }
 +</code>
 +there are many [[https://laravel.com/docs/8.x/validation#available-validation-rules|validation rules]] available already in Laravel and custom rules can be added if needed. 
 +
 +also note, that the ''$validData'' variable now contains an array with the validated data in a format that is compatible with the ''create'' methods for the laravel models. So if you set up your model's ''$fillable'' property correctly and your form field names correspond to your database field names, you can pass this array straight to the ''create()'' method of your model and create a new database entry like that!
 +
 +now here are a few cool things about the validator: 
 +  * if it finds an invalid field, it will stop right there and laravel will show you the form page again without any further routes needed. 
 +  * in addition to bringing the user back to the form, laravel will provide an ''$error'' variable which can be used in the balde 
 +  * even better, there is a ''@error(fieldname)'' blade syntax which will do whatever follows after it if the specified field is invalid
 +  * you also get a ''old(fieldname)'' function which you can use to populate your input fields with the original inputs, so the user won't have to enter eveything again.. see the eample below. 
 +here's an example part of a (bootstrap based) blade with a form that will show some info if the validation fails: 
 +
 +<code html>
 +    @if ($errors->any())
 +      <div class="alert alert-danger col-12" role="alert">
 +        You did not fill in all the form fields correctly, please correct or complete the Information given below
 +      </div>
 +    @endif
 +  <form action="{{ route('address.create') }}" method="POST">
 +    @csrf
 +    <div class="row pt-4">
 +      <div class="form-group col-12">
 +        <label for="inputName">Name</label>
 +        <input type="text" class="form-control @error('name') is-invalid @enderror" id="inputName" name="name" value="{{ old('name') }}">
 +        <div class="invalid-feedback">Please enter your name here</div>
 +      </div>
 +      .... 
 +</code>
  
 ===== Tricks ===== ===== Tricks =====
Line 184: Line 224:
 @extends('layouts.mylaout', [ 'title' => "Details for ".$section->name, 'image' => 'section_details.jpg' ]) @extends('layouts.mylaout', [ 'title' => "Details for ".$section->name, 'image' => 'section_details.jpg' ])
 </code> </code>
-and in the layout you would then simply use ''{{ $title }}'' to echo the title you passed along. +and in the layout you would then simply use <code>{{ $title }}</code> to echo the title you passed along. 
  
 === 2.) create a mini-section === === 2.) create a mini-section ===
  • getting_started_with_laravel.txt
  • Last modified: 04.12.2020 11:44
  • by Pascal Suter