Skip to content

Instantly share code, notes, and snippets.

@mertbuldur
Last active November 26, 2021 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mertbuldur/38f5ec8f9384d9d02cf2a6baad0aff83 to your computer and use it in GitHub Desktop.
Save mertbuldur/38f5ec8f9384d9d02cf2a6baad0aff83 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class indexController extends Controller
{
public function index(){
return view('form');
}
public function store(Request $request){
$validator = Validator::make($request->all(),[
'invoice'=>'required|integer',
'isInvoice'=>'required|integer',
]);
$validator->sometimes('name','required',function($request){
return $request->invoice == 1 && $request->isInvoice == 0;
});
$validator->sometimes('phone','required',function($request){
return $request->invoice == 1 && $request->isInvoice == 0;
});
$validator->sometimes('companyName','required|string',function($request){
return ($request->invoice == 2 && $request->isInvoice == 0);
});
$validator->sometimes('taxNumber','required',function($request){
return ($request->invoice == 2 && $request->isInvoice == 0);
});
if($validator->fails()){
dd($validator->errors());
}
/*
$request->validate([
'invoice'=>'required|integer',
'name'=>'required_if:invoice,1',
'phone'=>'required_if:invoice,1',
'companyName'=>'required_if:invoice,2',
'taxNumber'=>'required_if:invoice,2',
]);
*/
$all = $request->all();
dd($all);
}
}
<html>
<head>
<title>Validate</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.1/dist/css/bootstrap.min.css">
<style>
.s-box { display: none}
</style>
</head>
<body>
<div>
<div class="container">
<form action="" method="POST">
@csrf
<input type="hidden" name="isInvoice" value="0">
<h3>Kullanıcı Bilgileri</h3>
<div class="form-group">
<label for="">Kullanıcı Adı</label>
<input type="text" name="username" required class="form-control">
</div>
<div class="form-group">
<label for="">Kullanıcı Email</label>
<input type="text" name="email" required class="form-control">
</div>
<div class="form-group">
<label for="">Kullanıcı Şifre</label>
<input type="text" name="password" class="form-control">
</div>
<h3>Fatura Bilgileri</h3>
<div class="mb-2">
<input type="checkbox" name="isInvoice" value="1" checked> Bireysel bilgilerim ile fatura bilgim aynı
</div>
<div id="invoice-area" style="display: none;">
<div class=" d-flex flex-row" >
<div class="mr-2">
<input type="radio" class="checkbox-select" data-open=".bireysel-alan" name="invoice" checked value="1"> Bireysel
</div>
<div class="ml-2">
<input type="radio" class="checkbox-select" data-open=".kurumsal-alan" name="invoice" value="2"> Kurumsal
</div>
</div>
<div class="mt-2 bireysel-alan s-box" style="display: block">
<div class="form-group">
<label for="">Ad Soyad</label>
<input type="text" name="name" class="form-control">
</div>
<div class="form-group">
<label for="">Telefon</label>
<input type="text" name="phone" class="form-control">
</div>
</div>
<div class="mt-2 kurumsal-alan s-box" >
<div class="form-group">
<label for="">Firma Adı</label>
<input type="text" name="companyName" class="form-control">
</div>
<div class="form-group">
<label for="">Vergi Numarası</label>
<input type="text" name="taxNumber" class="form-control">
</div>
</div>
</div>
<button class="btn btn-success mt-2">Kaydet</button>
</form>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
$(document).ready(function(){
$(".checkbox-select").click(function(){
const divName = $(this).data("open");
if(divName == '.bireysel-alan'){
$(".bireysel-alan").find('input,select,textarea').each(function(){
$(this).prop('required',true);
});
$(".kurumsal-alan").find('input,select,textarea').each(function(){
$(this).prop('required',false);
});
}
if(divName == '.kurumsal-alan'){
$(".bireysel-alan").find('input,select,textarea').each(function(){
$(this).prop('required',false);
});
$(".kurumsal-alan").find('input,select,textarea').each(function(){
$(this).prop('required',true);
});
}
$(".s-box").hide();
$(divName).show();
});
$("input[name=isInvoice]").click(function(){
if($(this).is(":checked")){
$(".bireysel-alan").find('input,select,textarea').each(function(){
$(this).prop('required',false);
});
$(".kurumsal-alan").find('input,select,textarea').each(function(){
$(this).prop('required',false);
});
$("#invoice-area").hide();
}
else
{
$(".bireysel-alan").find('input,select,textarea').each(function(){
$(this).prop('required',true);
});
$("#invoice-area").show();
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment