使用 Laravel 在验证期间检查输入中是否存在字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17450658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if field exists in Input during validation using Laravel
提问by eski009
I want to make sure that certain fields are posted as part of the form but I don;t mind if some are empty values.
我想确保某些字段作为表单的一部分发布,但我不介意某些字段是否为空值。
The 'required' validation rule won't work as I am happy to accept empty strings. I have tried the below, but as the 'address2' field is never sent, the validator doesn't process it.
“必需”验证规则将不起作用,因为我很乐意接受空字符串。我已经尝试了下面的方法,但是由于从未发送过“address2”字段,因此验证器不会处理它。
Any ideas?
有任何想法吗?
$rules = array(
'address2' => 'attribute_exists'
);
class CustomValidator extends Illuminate\Validation\Validator {
public function validateAttributeExists($attribute, $value, $parameters)
{
return isset($this->data[$attribute]);
}
}
采纳答案by EThaizone Jo
You should make custom validator like this.
您应该像这样制作自定义验证器。
use Symfony\Component\Translation\TranslatorInterface;
class CustomValidator extends Illuminate\Validation\Validator {
public function __construct(TranslatorInterface $translator, $data, $rules, $messages = array())
{
parent::__construct($translator, $data, $rules, $messages);
$this->implicitRules[] = 'AttributeExists';
}
public function validateAttributeExists($attribute, $value, $parameters)
{
return isset($this->data[$attribute]);
}
}
This will make AttributeExists work without to use require. For more explain about this. When you want to create new validator rule. If you don't set it in $implicitRules, that method will not work out if you don't use require rule before it. You can find more info in laravel source code.
这将使 AttributeExists 在不使用 require 的情况下工作。有关这方面的更多解释。当您想要创建新的验证器规则时。如果您没有在 $implicitRules 中设置它,那么如果您在它之前不使用 require 规则,则该方法将不起作用。您可以在 laravel 源代码中找到更多信息。
回答by CodeBull
You can use Input::has('address2')
to check if something is posted by address2 input name. See the example:
您可以使用Input::has('address2')
来检查是否通过 address2 输入名称发布了某些内容。看例子:
if(Input::has('address2')) {
// Do something!
}
回答by DJ Far
In Laravel 5,
在 Laravel 5 中,
if($request->has('address2')){
// do stuff
}
回答by Trying Tobemyself
When you submit a form each and every field is posted, matter of fact is if you leave some filed empty then that field value is null or empty. Just check the POST parameters once, to do so open the firebug console in firefox and submit the form, then check the post parameters. As you want to accept empty string what is the use of any rule?
当您提交表单时,每个字段都已发布,事实上,如果您将某些字段留空,则该字段值为空或空。只需检查 POST 参数一次,为此在 firefox 中打开 firebug 控制台并提交表单,然后检查 post 参数。当你想接受空字符串时,任何规则有什么用?
else You can do this
否则你可以这样做
$addr2=Input::get('address2');
if(isset($addr2)){
//do here whatever you want
}else{
//do something else
$addr2='';//empty string
}
回答by Carlos Arauz
Actually, Laravel has a method to validate if an attribute exists even if not filled.
实际上,Laravel 有一种方法可以验证属性是否存在,即使没有填充。
$rules = [
'something' => 'present'
];
All the validation rules are stored in Validator class (/vendor/laravel/framework/src/Illuminate/Validation/Validator.php), you can check for the implementation of each rule, even no documented rules.
所有的验证规则都存储在 Validator 类(/vendor/laravel/framework/src/Illuminate/Validation/Validator.php)中,您可以检查每个规则的执行情况,甚至没有记录的规则。