2016-04-05 1 views
-1

ich versuche Rückruf zu verwenden, um einfach meine Formulareingabe zu überprüfen, ist die offizielle Code hier: https://laravel.com/docs/5.2/validationLaravel Validierung Rückruf funktionieren nicht

folgende ist meine Funktion

public function addthread(Request $request) { 


    $input = $request->all(); 
    $rules = array('title' => 'required|unique:thread|max:255'); 
    $message = array('title.required' => 'The :attribute field is aaa required.'); 
    $validator = Validator::make($input, $rules, $message); 

    $validator->after(function($validator) { 
     if ($this->checkOpt()) { 
      $validator->errors()->add('num_opt', 'Something is wrong with this field!'); 
      echo 'test';   
     } 
    }); 

    if ($validator->fails()) { 
     return redirect('addthreadhtml')->withErrors($validator)->withInput(); 
    } 

} 

public function checkOpt() { 
    return false; 
} 

die Klinge tpl:

@if (count($errors) > 0) 
<div class="container" stytle="max-width:80%"> 
    <div class="alert alert-danger"> 
     <ul> 
      @foreach ($errors->all() as $error) 
       <li>{{ $error }}</li> 
      @endforeach 
     </ul> 
    </div> 
</div> 
@endif 

Der num_opt Fehler nie ausdrucken, keine Ahnung?

Antwort

0

checkOpt() gibt FALSE zurück, so dass der Code niemals die if-Anweisung eingeben wird.

if ($this->checkOpt()) { // this is returning false, right ?? so, its not adding the error 
     $validator->errors()->add('num_opt', 'Something is wrong with this field!'); 
     echo 'test';   
    } 
-1

Ihre checkOpt() gibt immer false zurück, so dass Ihre Bedingung nicht erfüllt wird.