Level 74
You can use the requiredIf validation rule.
Something like
$request->validate(['referal_code' => Rule::requiredIf(function () use ($request) {
return $request->familiar == 4;
})]);
How to creare if you choose marketing, you must enter the code
<div class="form-group row d-none" id="referral_code">
<label for="referral_code">Referral Code</label>
<div class="col-md-10">
<input id="referral_code" type="text" class="form-control" name="referral_code">
</div>
</div>
<input class="form-check-input" type="radio" name="familiar" id="google" value="1" onclick="showReferal(false)">
<input class="form-check-input" type="radio" name="familiar" id="friends" value="2" onclick="showReferal(false)">
<input class="form-check-input" type="radio" name="familiar" id="advertising" value="3" onclick="showReferal(false)">
<input class="form-check-input" type="radio" name="familiar" id="marketer" value="4" onclick="showReferal(true)">
MyRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class MyRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
if (familiar == 4) {
'referral_code' => 'required',
}
];
}
}
Please or to participate in this conversation.