OctoberCMS开发入门教程 — AJAX表单数据验证

2018-01-16 242

OctoberCMS开发入门教程系列:

数据验证页面必须引入{% framework extras %},即framework.extras.js和framework.extras.css 下面是基本使用示例 前端代码:

<form data-request="onDoSomething"
    data-request-validate
    data-request-flash>
    <div>
        <input name="name" />
        <span data-validate-for="name"></span>
    </div>
    <div>
        <input name="email" />
        <span data-validate-for="email"></span>
    </div>
    <button data-attach-loading>
        Submit
    </button>
    <div class="alert alert-danger" data-validate-error>
        <p data-message></p>
    </div>
</form>

后台代码:

function onDoSomething()
{
    $data = post();
    $rules = [
        'name' => 'required',
        'email' => 'required|email',
    ];
    $validation = Validator::make($data, $rules);
    if ($validation->fails()) {
        throw new ValidationException($validation);
    }
    Flash::success('Jobs done!');
}

Laravel5.5表单验证规则

发表评论