2016-11-13 171 views
1

我現在正在做一個項目,我希望至少需要填寫兩個社交媒體配置文件。但我無法做到這一點。 我有這種形式與這些領域如何使至少兩個字段需要填寫Laravel 5.2

<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> 

我想至少有兩個這些字段必須按要求填寫。 我的控制器是

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); 
       } 
      } 
     } 
    } 

我怎樣才能實現this.Please幫助

回答

1

讓你的社會概況和數組一樣

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

和laravel後端驗證社會計數像

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

中有我的控制器沒有validate方法。那麼我怎麼才能驗證這一點。請看我的控制器。 @Sherif –

+1

I'v更新了答案...我希望這就是你的意思 – Sherif

2

嘗試min規則爲array validation

最小驗證字段必須有一個最小值。字符串,數字和文件的評估方式與size規則相同。

對於數組,大小對應於陣列(從size規則的描述)的計

+0

我的控制器中沒有驗證方法。那麼我怎麼才能驗證這一點。請看我的控制器。 @AlexeyMezenin –

+0

閱讀[表單請求驗證](https://laravel.com/docs/5.3/validation#form-request-validation) –

相關問題