2015-09-14 115 views
1

可以說我有一個這樣的形式:Laravel 5.1,嵌套驗證

(該O公司代表複選框,輸入)

 
O - Pizza option 
O - Free beer with pizza 
O - I want extras on my pizza 
---- O - cheese 
---- O - funghi 
---- O - ham 
---- O - something else 

用戶可以選擇比薩餅和啤酒免費,但也有一些附加功能。除了這些額外功能外,他還需要選擇至少一種配料,但前提是選擇了額外的配件。

我的驗證如下:

'extras.cheese' => 'required_without_all:funghi,ham,something_else', 
'extras.funghi' => 'required_without_all:cheese,ham,something_else', 
'extras.ham' => 'required_without_all:cheese,funghi,something_else', 
'extras.something_else' => 'required_without_all:cheese,funghi,ham', 

不過,我想只有extraOption選擇了這些申請。換句話說,我想嵌套這些規則在required_if:extraOption,1規則裏面。

編輯:

我想我需要一個自定義的驗證,這將是這個樣子:

'extras.cheese' => 'required_if_then_required_without_all:extraOption,funghi,ham,something_else'

,其中第一選擇會去功能required_if一部分,其餘選項required_without_all部分。

我會在移動時編輯我的問題。

編輯2:

繼文檔(http://laravel.com/docs/5.1/validation#custom-validation-rules)我添加的代碼下面app/Providers/AppServiceProvider.php

Validator::extend('required_if_then_required_without_all', function($attribute, $value, $parameters) { 
     return true; 
    }); 

現在我需要弄清楚什麼是什麼,以及如何實現的邏輯。

EDIT3:

這就是我認爲應該工作,但事實並非如此。它總是通過驗證。

Validator::extend('required_if_then_required_without_all', function($attribute, $value, $parameters) { 
     $required_if = $parameters[0]; 
     $required_without_all = array_except($parameters, [0]); // remove entry with key 0 from $parameters array 
     $value = false; 
     if($required_if != 1) 
     { 
      $value = true; 
     } 
     else 
     { 
      foreach ($required_without_all as $r) 
      { 
       if ($r == 1) { $value = true; } 
      } 

     } 
     return $value; 
    }); 

我叫它爲:

'cheese' => 'required_if_then_required_without_all:extraOption,funghi,ham,something_else', 

UPDATE ON EDIT3:

添加新的驗證器爲:

Validator::extend('false', function($attribute, $value, $parameters) { 
     return false; 
    }); 

,把它作爲 '奶酪'=> 'false',

什麼都不做。 我想我在這裏做的事情非常錯誤,任何幫助表示讚賞。

月2日更新TO EDIT3:

我認爲問題是,如果不選擇,以便驗證沒有被觸發Form::checkbox不發送任何數據。

調用falseForm::text作爲例外。

3日更新到EDIT3:

複選框前添加

{!! Form::hidden('cheese',0) !!}

做出響應false驗證領域。但驗證器required_if_then_required_without_all仍然不會被觸發。

回答

0

結構:

O - Pizza option 
    O - Free beer with pizza 
    O - I want extras on my pizza 
    ---- O - cheese 
    ---- O - funghi 
    ---- O - ham 
    ---- O - something else 

驗證

Validator::extend('required_with_one_of', function($attribute, $value, $parameters, $validator) 
    { 
     $data = $validator->getData(); 
     $count = []; 
     foreach ($parameters as $p) { 
      if (array_get($data,$p) != null) {$count[] = array_get($data, $p);} 
     } 
     if(count($count) > 0) { return true; } else { return false;} 
    }); 

施加到extraOption

 'extraOption => 'required_with_one_of:options.cheese,options.funghi,options.ham,options.something_else', 

複選框是:

{!! Form::checkbox('extraOption', '1', false, ['id' => 'extraOption']) !!} 

    <div class="form-group col-sm-12"> 
     {!! Form::label('options[cheese]','Cheese:', ['class' => 'padding-right-10']) !!} 
     {!! Form::checkbox('options[cheese]', '1', false) !!} 
    </div> ...... 
+0

如果有人有時間,請檢查是否一切正常。這是工作,但我想知道是否可以改進。謝謝。 ps。我的代碼並不那麼簡單,但我認爲我根據這個簡化的示例編輯了所有內容。 – dbr