gpt4 book ai didi

php - Laravel 4 中的自定义验证

转载 作者:行者123 更新时间:2023-12-02 04:38:35 25 4
gpt4 key购买 nike

为了向 Laravel 添加新的验证,我这样做了:

app/start/ 中创建了一个名为 customValidation.php 的新文件

然后将其包含在 app/start/global.php 中,并使用 echo() 对其进行测试并且它有效。所以它现在被加载到应用程序中。

然后我写了下面的代码来验证 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);
});

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

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

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

最佳答案

然后我会为此创建自定义 app/validators 文件夹。

1、创建app/validators/CustomValidate.php

<?php

class CustomValidate extends Illuminate\Validation\Validator
{

public function validateCheckbox($attribute, $value, $parameters)
{
echo "this is the: " . $value;
}

}

2、运行php artisan optimizecomposer dumpautoload

3, 在某处注册您的自定义验证器。或许将 app/validators.php 添加到 start/global.php

Validator::resolver(function($translator, $data, $rules, $message){
return new CustomValidate($translator, $data, $rules, $message);
});

4,验证

$rules = ['agreed' => 'checkbox'];
$data = ['agreed' => 0];

$v = Validator::make($data, $rules);

dd($v->passes());

关于php - Laravel 4 中的自定义验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com