检查php中数组的动态变量是否包含值



这是我在create中创建联系人的基本表单。vue -


<div class="form-group mb-3 ">
<label class="form-label">Name</label>
<div>
<input type="text" class="form-control" placeholder="Enter Name" v-model="form.name" required>
</div>
</div>
<div class="form-group mb-3 ">
<label class="form-label">Contact Id</label>
<div>
<input type="text" class="form-control" placeholder="Enter Contact Id" v- 
model="form.contact_id" required>
</div>
</div>   

然后我给出了以相同形式添加地址的选项,但这不是必需的。但是如果用户想要,在创建联系人时,用户也可以添加多个地址。


<div class="form-group row" v-for="(item, k) in form.addresses" :key="k" >
<label for="exampleFormControlInput1" class="form-label">Street 1:</label>
<div class="form-group mb-3">
<input class="form-control" type="text" v-model="item.street1" name="street1" 
placeholder="Enter Street 1">  
</div>
<div class="form-group mb-3">
<label for="exampleFormControlInput1" class="form-label">State</label>
<select class="form-control"  v-model="item.state_id" name="state_id">
<option value="">Choose State</option>
<option value="1">Manitoba</option>
<option value="2">Winnipeg</option>
<option value="3">Punjab</option>
<option value="4">Other</option>
</select>   
</div>
<div class="form-group mb-3">
<label for="exampleFormControlInput1" class="form-label">Country</label>
<select class="form-control"  v-model="item.country_id" name="country_id">
<option value="">Choose Country</option>
<option value="1">Canada</option>
<option value="2">U.S</option>
<option value="3">India</option>
<option value="4">Other</option>
</select>   
</div>
<div class="form-group mb-3">
<button class="btn btn-sm btn-danger" type="button" 
@click="removeAddress(k)">Remove</button>  
</div>
</div>
<div class="form-group row">
<div class="col-12">
<input class="btn btn-primary form-control" type="button" value="Add Another Address" 
@click="addAddress()"> 
</div>
</div>

但是当我在数据库中存储数据时,如果只给出了名称,则应该存储数据。

在控制器中,如果我使用return $request->all()——,我的数据就像这样被获取{"name"y"contact_id":"y","addresses":"[{"street1"y","state_id":"1","country_id":"1"}]"}

下面是我的控制器代码——


public function store(Request $request)
{
//return $request->all();
$contact = Contacts::create([
'name' => $request->name,
'company_id' => Auth::user()->company_id,
]);
$addresses= json_decode($request->addresses, true);
//return $addresses;
//[{"street1":"","state_id":"","country_id":""}]
if (! empty($addresses)) {
foreach($addresses as $ad) {
$address = new Address;
$address->contact_id = $contact->id;
$address->street1 = $ad['street1'];
$address->state_id = $ad['state_id'];
$address->country_id = $ad['country_id'];
$address->save();
} 
}

如何检查街道,州和国家的变量值是否为空,不应该存储数据。它返回错误

无效的日期时间格式:1366不正确的整数值:"对于列clientchief_dbaddressesstate_idat row 1 (SQL: insert intoaddresses(contact_id,street1,state_id,country_id,updated_at,created_at) values (14,,,, 2022-06-14 19:26:49, 2022-06-14 19:26:49))">

您应该验证您的请求:

$request->validate([
'addresses' => ['required', 'array'],
'addresses.*.street1' => ['required', 'string'],
'addresses.*.state_id' => ['required', 'numeric'],
'addresses.*.country_id' => ['required', 'numeric'],
]);

如果我没理解错的话

  • 用户可以直接填写name,提交表单-数据应该保存在数据库
  • 用户可以填写多个地址,但如果用户不填写完整的详细信息(街道,州&;国家/地区)作为地址,它不应该在数据库中保存

一种方法是

  • 验证地址只数据,如果用户将地址添加到表单
  • ,如果street1, state_id或country_id为null,则返回验证错误
public function store(Request $request)
{
//return $request->all();
$validated = $request->validate([
'name' => ['required', 'string'],
'addresses' => ['sometimes', 'array'],
'addresses.*.street1' => ['sometimes', 'required', 'string'],
'addresses.*.state_id' => ['sometimes', 'required', 'numeric'],
'addresses.*.country_id' => ['sometimes', 'required', 'numeric'],
]);
//The above will pass validation if name is filled as non empty string & addresses is not present in the request data
//But if the addresses is present in the request, it will fail if either of  street1, state_id or country_id is null
$contact = Contacts::create([
'name' => $request->name,
'company_id' => Auth::user()->company_id,
]);
$addresses= json_decode($request->addresses, true);
//return $addresses;
//[{"street1":"","state_id":"","country_id":""}]
if (! empty($addresses)) {
foreach($addresses as $ad) {
$address = new Address;
$address->contact_id = $contact->id;
$address->street1 = $ad['street1'];
$address->state_id = $ad['state_id'];
$address->country_id = $ad['country_id'];
$address->save();
} 
}
//return response
}

Laravel Docs - Validation - Validation When Present

相关内容

  • 没有找到相关文章