2015-04-29 31 views
5

Benim ajax komut dosyası aşağıdaki gibi dizi gönderin: Bu dizi birden fields ekleyebilir html parçası kullanıcı olarak Input::get('questions')Doğrula dizisi laravel 5

Array 
(
    [0] => Array 
     (
      [name] => fields[] 
      [value] => test1 
     ) 

    [1] => Array 
     (
      [name] => fields[] 
      [value] => test2 
     ) 

) 

aittir.

  $inputs = array(
       'fields' => Input::get('questions') 
      ); 

      $rules = array(
       'fields' => 'required' 
      ); 
      $validator = Validator::make($inputs,$rules); 

       if($validator -> fails()){ 
        print_r($validator -> messages() ->all()); 
       }else{ 
        return 'success'; 
       } 

cevap

3

Basit: Array için

// First, your 'question' input var is already an array, so just get it 
$questions = Input::get('questions'); 

// Define the rules for *each* question 
$rules = [ 
    'fields' => 'required' 
]; 

// Iterate and validate each question 
foreach ($questions as $question) 
{ 
    $validator = Validator::make($question, $rules); 

    if ($validator->fails()) return $validator->messages()->all(); 
} 

return 'success'; 
0

laravel Özel Doğrulama Elemanları

: Her question ayrı bir for-each kullanarak doğrulamak Böyle bir şey ihtiyaç ile

Bana yardım eder misin

Aşağıdaki dosyayı açın

/resources/lang/en/validation.php 

Ardından özel ileti Şimdi aşağıdaki kodu kullanarak önyükleme işlevi kodunu değiştirmek Böylece, başka bir dosya

/app/Providers/AppServiceProvider.php 

açmak

'numericarray'   => 'The :attribute must be numeric array value.', 
'requiredarray'  => 'The :attribute must required all element.', 

ekleyin.

public function boot() 
{ 
    // it is for integer type array checking. 
    $this->app['validator']->extend('numericarray', function ($attribute, $value, $parameters) 
    { 
     foreach ($value as $v) { 
      if (!is_int($v)) { 
       return false; 
      } 
     } 
     return true; 
    }); 

    // it is for integer type element required. 
    $this->app['validator']->extend('requiredarray', function ($attribute, $value, $parameters) 
    { 
     foreach ($value as $v) { 
      if(empty($v)){ 
       return false; 
      } 
     } 
     return true; 
    }); 
} 

Şimdi gerekli dizi öğesi için requiredarray kullanabilirsiniz. Ayrıca dizi öğesi tamsayı tipi denetimi için numericarray kullanın.

$this->validate($request, [ 
      'arrayName1' => 'requiredarray', 
      'arrayName2' => 'numericarray' 
     ]);