如何填写Laravel 5.2至少需要两个字段

时间:2016-11-13 13:12:18

标签: php mysql laravel validation 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);
                }
            }
        }
    }

我如何实现这一目标。请帮助

2 个答案:

答案 0 :(得分:2)

min尝试array validation规则。

  

min 验证字段必须具有最小值。字符串,数字和文件的评估方式与size规则相同。

     

对于数组,size对应于数组的计数(来自size规则说明)

答案 1 :(得分: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);