2012-06-30 110 views
5

在codeIgniter中有一個文件,我可以在其中編輯以便自定義表單驗證消息?如何在codeIgniter中自定義表單驗證錯誤

enter image description here

我只是想將它們放置在一個項目符號列表消耗更少的空間。

下面是我使用輸出錯誤消息的代碼:基本上$alert['sub_message']只是從笨validation_errors()功能,其從形式輸出該驗證錯誤獲取其數據

<div class="alert <?php echo $alert['alert_type']; ?> min-form"> 
     <button type="button" class="close" data-dismiss="alert">x</button> 
     <h4><?php echo $alert['main_message']; ?></h4> 
     <?php echo $alert['sub_message']; ?> 
</div> 

。通過創建application/libraries/MY_form_validation.phphttp://ellislab.com/codeigniter/user-guide/librarie/form_validation.html#errordelimiters

+0

如果沒有準備好,我會建議客戶端驗證圖層,所以CodeIgniter圖層只有在它們仍然能夠提交表單時才能看到。 –

+0

此外,我會查閱CI文檔:[回調:您自己的驗證功能](http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks)。 –

+0

@JaredFarrish謝謝我已經使用liveValidation進行了客戶端驗證http://livevalidation.com/ –

回答

8

可以擴展form_validation類最大的控制,以增加額外的驗證規則 - 我附:

10

您可以通過這種方式改變錯誤分隔符:

<ul> 
<?php echo validation_errors('<li>', '</li>'); ?> 
</ul> 

文檔下面的例子。

直接編輯系統庫是不好的做法; CI提供更好的選項(覆蓋/定製,通過MY_類,libraries,hooks)。這爲您提供輕鬆升級CI版本的好處&可讓您的應用程序可移植/自定義代碼與核心框架隔離。

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

/** 
* CodeIgniter Form Validation Extension 
*/ 
class MY_Form_validation extends CI_Form_validation { 

    /** 
    * MY_Form_validation::valid_url 
    * @abstract Ensures a string is a valid URL 
    */ 
    function valid_url($url) { 
     if(preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) { 
      if(filter_var($url, FILTER_VALIDATE_URL)) return TRUE; 
     } 
     $this->CI->form_validation->set_message('valid_url', 'The %s must be a valid URL.'); 
     return FALSE; 
    } 

    /** 
    * MY_Form_validation::alpha_extra() 
    * @abstract Alpha-numeric with periods, underscores, spaces and dashes 
    */ 
    function alpha_extra($str) { 
     $this->CI->form_validation->set_message('alpha_extra', 'The %s may only contain alpha-numeric characters, spaces, periods, underscores & dashes.'); 
     return (! preg_match("/^([\.\s-a-z0-9_-])+$/i", $str)) ? FALSE : TRUE; 
    } 

    /** 
    * MY_Form_validation::numeric_comma() 
    * @abstract Numeric and commas characters 
    */ 
    function numeric_comma($str) { 
     $this->CI->form_validation->set_message('numeric_comma', 'The %s may only contain numeric & comma characters.'); 
     return (! preg_match("/^(\d+,)*\d+$/", $str)) ? FALSE : TRUE; 
    } 

    /** 
    * MY_Form_validation::matches_pattern() 
    * @abstract Ensures a string matches a basic pattern 
    */ 
    function matches_pattern($str, $pattern) { 
     if (preg_match('/^' . $pattern . '$/', $str)) return TRUE; 
     $this->CI->form_validation->set_message('matches_pattern', 'The %s field does not match the required pattern.'); 
     return FALSE; 
    } 

} 

/* End of file MY_form_validation.php */ 
/* Location: ./{APPLICATION}/libraries/MY_form_validation.php */ 
0

看看系統/語言/英文/ form_validation_lang.php

我相信你可以對其進行編輯,或將其複製到應用程序/語言/英文/ form_validation_lang.php

+0

擴展它是正確的,但編輯'system /'是一個禁止;) – sekati

+0

我們可以將同一個文件複製到'application/language/english/form_validation_lang.php'(Applicatoin的語言文件夾)並進行任何定製想 – masterFly

-8

在爲了使用JavaScript來驗證的頁面的DevExpress控件,使用以下代碼:

ASPxClientEdit.ValidateGroup(null); 

ASPxClientEdit.ValidateGroup('validationgroup'); 
2

您可以使用 <?php echo form_error('field name', '<div class="error">', '</div>'); ?> 單獨顯示錯誤。加載表單驗證類後立即

$this->form_validation->set_error_delimiters('<li>', '</li>'); 

你應該這樣做:

Documentation

2

您可以通過使用CodeIgniter的set_error_delimiters功能改變從<div>的分隔符<li>

這將改變顯示validation_errors()form_error('field_name')的方式。所以,你需要添加ulol如下:

echo '<ul>' . validation_errors() . '</ul>'; 
1

與上面相同的答案,只是如果你想與引導:

<ul> 
<?php echo validation_errors('<li><span class="label label-danger">', '</span></li>'); ?> 
</ul> 
相關問題