2014-10-03 134 views
1

我想知道如何在表單驗證中包含輔助函數。我是laravel的新手,所以我只對其工作原理有一個基本的瞭解,但我甚至不知道如何包含具有函數的文件,我想用它來驗證表單以進行錯誤檢查。這就是我想用我的所有形式。我怎麼都在全球範圍內此頁面上有形式在刀片模板頁面中包含幫助程序文件

helper.php

<?php 
    public function hasError($error) { 
    if(strlen($error) > 0) { 
     echo "has-error"; 
    } 
} 

和insdie此頁我用網頁hasError create.blade.php

<div class="form-group <?php hasError($errors->get("keywords")) ?>"> 
    {{ Form::label('keywords', 'Keywords', array('class' => 'col-sm-2 control-label')) }} 
      <div class="col-sm-10"> 
       {{ Form::text('keywords', NULL, array(
        'placeHolder' => 'Keywords', 
        'class' => 'form-control' 
       )) }} 

回答

1

如何你在你的控制器中處理驗證嗎?當你確認後返回這樣的:

 // Return error 
     return Redirect::back() 
         ->withInput() 
         ->withErrors($validator); 

可以顯示象這樣的錯誤:

<small class="red">{{{ $errors->first('keywords') }}}</small> 

而且你還可以用這個節目特別類:

{{ $errors->has('keywords') ? 'has-error' : '' }} 

例子:

<div class="form-group {{ $errors->has('keywords') ? 'has-error' : '' }}"> 

-

有關創建輔助文件,並訪問他們 '全球'

創建一個文件夾命名爲:libraries - >app/libraries

創建您的庫文件(類):Helper.php

然後加入此代碼Helper.php

<?php 

class Helper{ 

public function hasError($error) 
{ 
    if(strlen($error) > 0) 
    { 
     echo "has-error"; 
    } 

} 

編輯composer.json在你的[R你的應用程序的OOT,並添加:"app/libraries"

例(composer.json):

"autoload": { 
    "classmap": [ 
     "app/commands", 
     "app/controllers", 
     "app/models", 
     "app/database/migrations", 
     "app/database/seeds", 
     "app/tests/TestCase.php", 
     "app/libraries" 
    ] 
}, 

在您的控制檯類型:

composer dump-autoload 

現在你可以打電話給你的 '功能' 像這個'任何地方':

Helper::hasError($error); 
+0

謝謝你這麼多爲你我有工作歡呼聲 – ONYX 2014-10-03 22:12:44

相關問題