how to set error message for each field in codeigniter php

我的梦境 提交于 2019-12-23 10:41:04

问题


i want to set different error message for each field in the form.This is my controller..

$this->form_validation->set_rules('fname', 'First Name','required');
$this->form_validation->set_message('required', 'कृपया प्रथम नाव भरा','fname'); 
$this->form_validation->set_rules('address', 'Address','required');
$this->form_validation->set_message('required', 'कृपया पत्ता भरा','address');

but problem is that if validation fails it print only error message for last field.


回答1:


I solved it ...first of all autoload form helper and thern in view file

 <?php if (form_error('gender'))
 {
    echo "कृपया लिंग निवडा";
 } 
 ;?>

where 'gender' is my field name.And I do it for each field in my form.




回答2:


Hi you have to the following thing if you use

<?php echo validation_errors();?> 

this will print all error in one place if you want to print each field with its own error then use this

<? echo form_input('field_name').br().form_error('field_name') ?>

if you are using multi language site then put your all error message in language files

please read form validation doc for complete understanding

http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#errordelimiters



来源:https://stackoverflow.com/questions/21509180/how-to-set-error-message-for-each-field-in-codeigniter-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!