Jahid Mahmud Jahid Mahmud - 26 days ago 11
MySQL Question

How to make At Least Two Field Required to fill in Laravel 5.2

I am now doing a project in which I want that at least two of my social media profile must be required to fill. But I could not achieve this.
I have this form with these fields

<form role="form" id="submit" class="p-md col-8">
<div class="form-group">
<label>Twitter</label>
<div class="social-link-input">
<i class="icon fa fa-twitter"></i>
<input type="text" name="twitter" class="form-control" placeholder="https://twitter.com/username" value="{{!empty($user->twitter)?$user->twitter:''}}">
</div>
</div>

<div class="form-group">
<label>Facebook</label>
<div class="social-link-input">
<i class="icon fa fa-facebook"></i>
<input type="text" name="facebook" class="form-control" placeholder="https://www.facebook.com/username/" value="{{!empty($user->facebook)?$user->facebook:''}}">
</div>
</div>

<div class="form-group">
<label>Linkedin</label>
<div class="social-link-input">
<i class="icon fa fa-linkedin"></i>
<input type="text" name="linkedin" class="form-control" placeholder="https://www.linkedin.com/in/username" value="{{!empty($user->linkedin)?$user->linkedin:''}}">
</div>
</div>

<div class="form-group">
<label>Google Plus</label>
<div class="social-link-input">
<i class="icon fa fa-google-plus"></i>
<input type="text" name="google_plus" class="form-control" placeholder="https://plus.google.com/+UserName" value="{{!empty($user->google_plus)?$user->google_plus:''}}">
</div>
</div>

<div class="form-group">
<label>Github</label>
<div class="social-link-input">
<i class="icon fa fa-github"></i>
<input type="text" name="github" class="form-control" placeholder="https://github.com/username" value="{{!empty($user->github)?$user->github:''}}">
</div>
</div>

<div class="form-group">
<label>Tumblr</label>
<div class="social-link-input">
<i class="icon fa fa-google-plus"></i>
<input type="text" name="dribbble" class="form-control" placeholder="https://www.tumblr.com/" value="{{!empty($user->dribbble)?$user->dribbble:''}}">
</div>
</div>

<div class="form-group">
<label>Youtube</label>
<div class="social-link-input">
<i class="icon fa fa-youtube"></i>
<input type="text" name="youtube" id="youtube" class="form-control" placeholder="https://www.youtube.com/user/username" value="{{!empty($user->youtube)?$user->youtube:''}}">
</div>
</div>

<div class="form-group">
<label>Flicker</label>
<div class="social-link-input">
<i class="icon fa fa-flickr"></i>
<input type="text" name="flicker" class="form-control" placeholder="https://www.flickr.com/people/username/" value="{{!empty($user->flicker)?$user->fliker:''}}">
</div>
</div>

<button type="submit" class="btn btn-info m-t">Save</button>
</form>


I want that at least two of these fields must be required to fill.
My Controller is

public function store_social_media(Request $request){
if(!Auth::Check()){
return redirect('/');
}
$user_id = Auth::user()->id ;
$count = 0 ;
if(!empty($user_id)) {
$user = User::find($user_id);
if (!empty($user)) {

if(!empty($request->input('twitter'))) {
if (strpos($request->input('twitter'), 'https://') !== false) {
$user->twitter = $request->input('twitter');
} else {
$user->twitter = 'https://' . $request->input('twitter');

}
}else {
$user->twitter = '';
}
if(!empty($request->input('facebook'))) {

if (strpos($request->input('facebook'), 'https://') !== false) {
$user->facebook = $request->input('facebook');
} else {
$user->facebook = 'https://' . $request->input('facebook');

}
}else {
$user->facebook = '';
}

if(!empty($request->input('linkedin'))) {

if (strpos($request->input('linkedin'), 'https://') !== false) {
$user->linkedin = $request->input('linkedin');
} else {
$user->linkedin = 'https://' . $request->input('linkedin');

}
}else {
$user->linkedin = '';
}
if(!empty($request->input('google_plus'))) {

if (strpos($request->input('google_plus'), 'https://') !== false) {
$user->google_plus = $request->input('google_plus');
} else {
$user->google_plus = 'https://' . $request->input('google_plus');

}
}else {
$user->google_plus = '';
}
if(!empty($request->input('github'))) {

if (strpos($request->input('github'), 'https://') !== false) {
$user->github = $request->input('github');
} else {
$user->github = 'https://' . $request->input('github');

}
}else {
$user->github = '';
}

if(!empty($request->input('dribbble'))) {
if (strpos($request->input('dribbble'), 'https://') !== false) {
$user->dribbble = $request->input('dribbble');
} else {
$user->dribbble = 'https://' . $request->input('dribbble');

}
}else {
$user->dribbble = '';
}

if(!empty($request->input('youtube'))) {
if (strpos($request->input('youtube'), 'https://') !== false) {
$user->youtube = $request->input('youtube');
} else {
$user->youtube = 'https://' . $request->input('youtube');

}
}else {
$user->youtube = '';
}
if(!empty($request->input('flicker'))) {

if (strpos($request->input('flicker'), 'https://') !== false) {
$user->flicker = $request->input('flicker');
} else {
$user->flicker = 'https://' . $request->input('flicker');

}
}else {
$user->flicker = '';
}
if(!empty($user->twitter )){
++$count;
}
if(!empty($user->facebook )){
++$count;
}
if(!empty($user->linkedin )){
++$count;
}
if(!empty($user->google_plus )){
++$count;
}
if(!empty($user->github )){
++$count;
}
if(!empty($user->dribbble )){
++$count;
}
if(!empty($user->flicker )){
++$count;
}
if(!empty($user->youtube )){
++$count;
}
$user->count_network = $count;
if(!empty($user->level)){
$user->level = $user->level ;
}else{
$user->level = 'silver';
}
$user->save();
$data=array(
'key'=>100,
'count'=>$count,
'val' =>'Adding social media successfully'
);
return response($data);
}
}
}
}


How Can I achieve this.Please help

Answer

make your social profiles and array like

<input type="text" name="social['twitter']" class="form-control" placeholder="https://twitter.com/username" value="{{!empty($user->twitter)?$user->twitter:''}}">

and in laravel back end validate the social count like

$rules = ['social' => 'min:2']
$validator = \Validator::make($request->all(), $rules);
if($validator->fails()) return response()->json($validator->errors(), 422);