By accessing the website and accepting the Cookie Policy, you agree to use the cookies provided by the Site in accordance with to analyze traffic, remember your preferences, and optimize your experience.
October CMS - 快速入门 19 - 表单验证
2018-10-24 13:23:08    225    0    0
emengweb

针对上一节的表单添加验证功能

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('从联系人表单提交的邮件');

            });
        }

    }

显示错误信息

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

<form data-request="onSend">

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

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

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

    <button type="submit" class="btn btn-success form-control">发送</button>

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

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

        {% endfor %}
    </ul>


</form>


上一篇: OctoberCMS快速入门-20-前端表单

下一篇: October CMS - 快速入门 18 - 创建联系表单

225 人读过
文档导航