Laravel 4中的自定义验证 [英] Custom validation in Laravel 4

查看:45
本文介绍了Laravel 4中的自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要向Laravel添加新的验证,我已经这样做:

To add a new validation to Laravel I have done this:

app/start/

,然后将其包含在app/start/global.php中,并使用echo()对其进行了测试,并且可以正常工作.因此,现在将其加载到应用程序中.

and then included it in app/start/global.php and tested it with an echo() and it worked. So it is loaded into the application now.

然后,我编写了以下代码来验证Laravel中的复选框:

Then I wrote the following code to validate checkboxes in Laravel:

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class customValidate extends Illuminate\Validation\Validator
{
    public function validateCheckbox($attribute, $value, $parameters)
    {
        //if(isset($value)) { return true; } else { return true; }
        echo "this is the: " . $value;
    }
}

/**
 * Resolvers for Custom Validations
 */
Validator::resolver(function($translator, $data, $rules, $message){
 return new customValidate($translator, $data, $rules, $message);   
});

但是,在我的验证规则中,我定义了:

However, in my validation rules, I define :

`array('sex'=>'checkbox')`

但是它不起作用.什么都没发生.没有引发任何错误.该应用程序的行为就好像它根本没有执行该功能一样.同样,当我从函数内部回显某些内容时,什么也没有得到回显,这是该函数根本没有被调用的另一个证据.

But it does not work. Nothing happens. No error is thrown. The application behaves as if it has not executed the function at all. Also when I echo something from within the function,nothing get echoed, another evidence for the function not being called at all.

推荐答案

提交表单时,单选按钮未设置为初始状态,这使Laravel不必寻找该项目的验证者.因此,它没有任何解决方法.我所做的是,我只是向收音机添加了初始状态,然后收音机就开始工作了.

When submitting the form, the radios were not set in their initial states, and this made Laravel not to look for the validator of the item. So, it doesn't have any workaround for that. What I did was I simply added an initial state to the radios and they worked.

很像安德烈科(Andreyco)所说的

Much Like said by Andreyco

这篇关于Laravel 4中的自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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