October CMS – quick start 19 – form validation

Time:2021-9-15

Add validation for the form in the previous section

File:plugins\raiseinfo\contact\components\ContactForm.php

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;

...

public function onSend()
    {

        $validator = Validator::make(
            [
                'name' => Input::get('name'),
                'email' => Input::get('email')
            ],
            [
                'name' => 'required|min:5',
                'email' => 'required|email'
            ]
        );


        if($validator->fails()){

            return Redirect::back()->withErrors($validator);

        }else{
            $vars = [
                'name' => Input::get('name'),
                'email' => Input::get('email'),
                'content' => Input::get('content')
            ];

            // views/mail/message/htm
            Mail::send('raiseinfo.contact::mail.message', $vars, function($message) {

                $message->to('[email protected]', 'Admin Person');

                $message - > subject ('mail submitted from contact form ');

            });
        }

    }

Display error message

File:plugins\raiseinfo\contact\components\contactform\default.htm

<form data-request="onSend">

    < label > Name: < / label >
    <input type="text" name="name" class="form-control">
    {{ errors.first('name') }}

    < label > email address: < / label >
    <input type="email" name="email" class="form-control">
    {{ errors.first('email') }}

    < label > information: < / label >
    <textarea name="content" class="form-control"></textarea>

    < button type = "submit" class = "BTN BTN success form control" > send < / button >

    <ul>
        {% for error in errors.all() %}

            <li>{{ error }}</li>

        {% endfor %}
    </ul>


</form>

October CMS - quick start 19 - form validation

Recommended Today

Performance comparison and pressure test before and after Nacos 2.0 upgrade

Introduction:Nacos 2.0 improves the performance by about 10 times by upgrading the communication protocol, framework and data model, and solves the performance problems gradually exposed after the release of Nacos 1.0. In this paper, through the pressure test of Nacos 1.0, Nacos 1.0 and the process of upgrading Nacos 2.0, Nacos 2.0 makes a comprehensive […]