Parsley.js和Laravel表单验证错误 [英] Parsley.js and Laravel Form Validation Errors

查看:80
本文介绍了Parsley.js和Laravel表单验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 parsley.js 进行客户端验证,并且还希望将其用于服务器端验证(Laravel表单验证程序),以使用欧芹样式显示错误.这样它才是统一的.

I am using parsley.js for a client-sided validation and also want to use it for the server-sided validation (Laravel Form Validator) to show the errors using the parsley styling. So that it is uniform.

因此,每当服务器端验证失败时,我都希望显示与正确字段相关的特定错误.我不想使用远程库.

So whenever my server-side validation fails, I would like to show the specific error associated to the correct field. I don´t want to use the remote library.

有一个示例如何执行此操作吗?

Is there an example how to do this?

链接到: Laravel验证程序非常感谢您!

推荐答案

您可以执行以下操作:

<div>
    {{ Form::open(['route' => 'dashboard.testform', 'id' => 'testForm']) }}
    <div class="form-group row">
        {{ Form::label('field1',
            'Field 1',
            ['class' => 'col-xs-2']
        ) }}
        <div class="col-xs-10">
            <?= Form::text('field1', null, ['class' => 'col-xs-12 required ' . ($errors->has('field1') ? 'parsley-error' : '') ]) ?>
            @if ($errors->has('field1'))
                <ul class="parsley-errors-list filled">
                    {{ $errors->first('field1', '<li class="parsley-required">:message</li>') }}
                </ul>
            @endif
        </div>
    </div>
    <div class="form-group row">
        {{ Form::label('field2', 'Field 2', ['class' => 'col-xs-2']) }}
        <div class="col-xs-10">
            <?= Form::text('field2', null, ['class' => 'col-xs-12 ' . ($errors->has('field2') ? 'parsley-error' : '') ]) ?>
            @if ($errors->has('field2'))
                <ul class="parsley-errors-list filled">
                    {{ $errors->first('field2', '<li class="parsley-required">:message</li>') }}
                </ul>
            @endif
        </div>
    </div>
    <div class="form-group row">
        {{ Form::label('field3', 'Field 3', ['class' => 'col-xs-2']) }}
        <div class="col-xs-10">
            <?= Form::text('field3', null, ['class' => 'col-xs-12 required ' . ($errors->has('field3') ? 'parsley-error' : '') ]) ?>
            @if ($errors->has('field3'))
                <ul class="parsley-errors-list filled">
                    {{ $errors->first('field3', '<li class="parsley-required">:message</li>') }}
                </ul>
            @endif
        </div>
    </div>
    {{ Form::submit('Submit form', ['class' => 'btn btn-primary']) }}
    {{ Form::close() }}
</div>

{{ HTML::style('http://parsleyjs.org/src/parsley.css') }}
{{ HTML::script('http://code.jquery.com/jquery-1.11.1.min.js') }}
{{ HTML::script('http://parsleyjs.org/dist/parsley.min.js') }}

<script type="text/javascript">
    $(document).ready(function() {
        $("#testForm").parsley();
    });
</script>

您的控制器将如下所示:

And your controller will look like this:

public function testform() {
    $rules = array(
        'field1' => 'date',
        'field2' => 'required'
    );

    $validator = Validator::make(
        Input::all(),
        $rules
    );
    if ($validator->fails()) {
        return Redirect::back()->withErrors($validator);
    }

    return Redirect::back();
}

最好创建一个回显输入的自定义表单宏和错误(如果存在).这样一来,您的视图就得到了简化.

It might be a good idea to create a custom Form Macro that echos the input and the error, if it exists. That way your view is simplified.

这篇关于Parsley.js和Laravel表单验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆