如何在 Laravel 中验证数组?
在 Laravel 中验证输入数据的方法有很多种。其中一种是使用 Validator 类。第一步是包含此类,如下所示:
use Illuminate\Support\Facades\Validator;
当您使用数组时,需要在数组中指定键和要使用的规则。例如,考虑以下数组。在这里,我们添加了键 firstname、lastname 和 address 以及它们的值。使用规则,我们定义所有这些元素都是必填的,并且数据类型为字符串。
$formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string';
示例 1
现在让我们使用验证器测试数组,如下所示:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz', ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successful"; } } }
输出
执行上述代码后,将生成以下输出。
Array Validation is Successful
示例 2
现在,让我们检查另一个示例,我们将删除 address 字段并进行测试:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni' ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }
在上述情况下,数组中没有 address 字段,但规则规定需要 address 字段,因此当您使用验证器对其进行测试时,它将失败,并且此程序会打印一条消息,提示“无效数组”,如下所示。
输出
Invalid Array
示例 3
如果指定了键并且没有定义规则,则验证器将忽略该键并使其通过。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array( 'firstname' => 'Siya', 'lastname' => 'Nadkarni', 'address' => 'xyz', ); $rules['firstname'] = 'required|string'; $rules['lastname'] = 'required|string'; // $rules['address'] = 'required|string'; $validator = Validator::make($formData , $rules); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }
输出
此处删除了 address 的规则,测试后,上述代码的输出如下:
Array Validation is Successful
示例 4
下面显示了如何验证数字数组。其规则如下:
$formData = array(10,20,30,40); $validator = \Validator::make(compact('formData'), [ 'formData' => 'required|array', 'formData.*' => 'integer' ]);
应用的规则是数组必须有元素,并且这些元素必须是整数值。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class testuserip extends Controller { public function index() { $formData = array(10,20,30,40); $validator = \Validator::make(compact('formData'), [ 'formData' => 'required|array', 'formData.*' => 'integer' ]); if ($validator->fails()) { echo "Invalid Array"; } else { echo "Array Validation is Successfull"; } } }
输出
执行上述程序后,将生成以下输出:
Array Validation is Successful
广告