laravel验证回调不起作用

时间:2016-04-05 14:50:20

标签: php laravel laravel-5.2

我正在尝试使用回调来简单地检查我的表单输入,官方代码在这里:https://laravel.com/docs/5.2/validation

以下是我的功能

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;
}
刀片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

num_opt错误永远不会打印出来,不知道吗?

2 个答案:

答案 0 :(得分:0)

checkOpt()返回FALSE,因此代码永远不会输入if语句。

Function HasHiddenCell(source As Range) As Boolean
  Dim rg As Range

  'check the columns
  If VBA.IsNull(source.ColumnWidth) Then
    For Each rg In source.Columns
      If rg.ColumnWidth = 0 Then
        HasHiddenCell = True
        Exit Function
      End If
    Next
  End If

  ' check the rows
  If VBA.IsNull(source.RowHeight) Then
    For Each rg In source.rows
      If rg.RowHeight = 0 Then
        HasHiddenCell = True
        Exit Function
      End If
    Next
  End If
End Function

Sub UsageExample()
  If HasHiddenCell(selection) Then
    Debug.Print "A cell is hidden"
  Else
    Debug.Print "all cells are visible"
  End If
End Sub

答案 1 :(得分:-1)

你的checkOpt()总是返回false,所以你的状况不会令人满意。