2011-11-14 49 views

回答

-2

只是做服務器端對客戶端的一個JS驗證和PHP驗證(或使用CI的表單輔助函數)...無論如何,PHP應該是這個樣子:

if ($_POST['cellphone'] == '' OR $_POST['phone' == '') { 
// do stuff like make an notification, alert, etc... and take users back to the form 
} 
+0

我已經在配置文件夾中創建了所有驗證的數組。所以我應該怎麼做這種類型的驗證。 @Shomz –

+0

我們需要一個不是核心的規則php –

+0

@Darshanambaliya你可以問另一個問題。 – Shomz

7

你可以做它爲:

 
$this->form_validation->set_rules('phone', 'Your validation message.', 'callback__phone_check'); 

並做好功能:

 
function _phone_check() { 
    //check for phone and cellphone field. 
    //make sure one field is not empty and return accordingly 
} 

希望幫助

+0

我應該在哪裏做?鑑於,控制器? –

+0

@ jQuery.PHP.Magento.com應該在你的控制器..!在這裏閱讀更多:: https://ellislab.com/codeIgniter/user-guide/libraries/form_validation.html#callbacks –

4

只是想用自己的方式拋出一些快速代碼片段。我想完成這個任務,而這個問題是我在搜索時首先在谷歌的條目之一。我把靈感來自於其他的答案,在這裏就是我所做的這些工作對我來說(因人而異):

$this->form_validation->set_message('contactVerify', 'Either Phone or Email is required'); 
... 
$this->form_validation->set_rules('phone', 'Phone', 'trim|callback_contactVerify[email]|xss_clean'); 
$this->form_validation->set_rules('email', 'Email', 'trim|callback_contactVerify[phone]|valid_email|xss_clean'); 
... 
public function contactVerify($contact, $otherField) { 
    return ($contact != '' || $this->input->post($otherField) != ''); 
} 

由於這是一個很簡單的驗證功能,我預先設定的錯誤信息,所以我不必將其設置在驗證功能中。我通過方括號將其他字段作爲第二個參數傳入。

希望這是有用的。

+0

不幸的是,我不認爲這工作了。我認爲如果規則中不存在「必需」,則不會檢查其他規則(爲了效率,我假設)。 – twistedpixel

相關問題