3

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

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('tian5258@gmail.com', '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>

clipboard.png


后山人
272 声望39 粉丝

这个是一个典型的,前后端分离的开发框架,而且很多前后端代码,都可以很好的生成,无需写代码,大大减少程序员编写代码的数量。提高效率,降低成本!