I have made an email validation.
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|callback_email_check');
function email_check($str)
{
if (stristr($str,'#uni-email-1.com') !== false) return true;
if (stristr($str,'#uni-email-2.com') !== false) return true;
if (stristr($str,'#uni-email-3.com') !== false) return true;
$this->form_validation->set_message('email', 'Please provide an acceptable email address.');
return FALSE;
}
After submitting my form, it says "Unable to access an error message corresponding to your field name." is there something wrong with my code?
it should be
$this->form_validation->set_message('email_check', 'Please provide an acceptable email address.');
go to documentation for reference HERE
To set your own custom message you can use the following function:
$this->form_validation->set_message('rule', 'Error Message');
but you haven't named the rule correctly in your code it should be email_check instead of email
$this->form_validation->set_message('email_check', 'Please provide an acceptable email address.');
Related
I'm trying to validate credit card numbers and so far I am able to get a result from it. However, whenever it fails, the validation message for the form won't turn up. I've been trying several methods including setting a callback. I'm not sure what I'm missing. Hope someone can take a look and help me.
Controller
public function next(){
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
$this->form_validation->set_rules('inputcardtype','Select Card Type','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean');
$this->form_validation->set_rules('inputexpirationdatemonth','Select Month','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the month of expiration');
$this->form_validation->set_rules('inputexpirationdateyear','Select Year','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Please select the year of expiration');
$this->form_validation->set_rules('inputnameoncard', 'Name on Card', 'trim|required|xss_clean');
$inputcardnumber = $this->input->post('inputcardnumber');
$inputcardtype = $this->input->post('inputcardtype');
// var_dump($this->cardnumber_validation($inputcardnumber,$inputcardtype));
if($this->form_validation->run()==false||$this->cardnumber_validation($inputcardnumber,$inputcardtype)==FALSE){
$this->index();
}else{
}
}
function cardnumber_validation($string = NULL,$cardtype=NULL) {
$this->load->helper('creditcardvalidation');
if(checkCreditCard ($string, $cardtype, $ccerror, $ccerrortext)) {
return TRUE;
}
else{
$this->form_validation->set_message("inputcardnumber", 'Invalid Card Number');
return FALSE;
}
}
function check_default($post_string){
return $post_string == '0' ? FALSE : TRUE;
}
So I found out you can do this to pass 2 variables into a callback
$this->form_validation->set_rules('inputcardnumber', 'Card Number', 'trim|required|xss_clean|callback_cardnumber_validation['.$this->input->post('inputcardtype').']');
Im having trouble with Yii1 validation. I have listbox with contact types and i want email validation to work only when contact via email is choosed. So Im using custom rule to check if its not empty:
public function customEmailValidation($attribute, $params)
{
if(!$this->hasErrors())
{
if($this->contact_type == 2)
{
if($this->attribute == "") $this->addError($attribute, "Enter email address");
}
}
}
But after that I want to use second rule to check if email format is good, how i can achieve it? In main rules i can check it by this:
['email', 'email', 'message' => 'wrong email format'],
but how i can check it only when $this->contact_type == 2 ? I need to write custom rule also and I need to write regex to check email format? Or somehow i can use main validation rules in custom validations?
Thank you.
First remove email validator from rules().
Using your same code, in your custom validation, you can 'attach' any existing Yii validator or create your own / custom validator. In your case, Yii email validator is enough and we will attach it to your custom validation:
public function customEmailValidation($attribute, $params)
{
if(!$this->hasErrors())
{
if($this->contact_type == 2)
{
if($this->attribute == "")
{
$this->addError($attribute, "Enter email address");
}
if( strlen($this->attribute) > 0 )
{
$emailValidator = new CEmailValidator;
if ( ! $emailValidator->validateValue($this->attribute) )
{
$this->addError($attribute, 'Wrong email');
}
}
}
}
}
how to validation email only use domain #gm.ac.id. i try but validation its not working.
this is code
public function email_check($email)
{
if (valid_email('email#mail.ugm.ac.id'))
{
return TRUE;
}
else
{
$this->form_validation->set_message('email_check', '%s gunakan email ugm');
return FALSE;
}
}
$this->form_validation->set_rules('email', 'email', 'required|callback_email_check');
please help me what to do.
thank you.
Valid_email has no way of knowing what domains it should allow or not.
An easier way would be:
$this->form_validation->set_rules('email', 'email', 'required|valid_email|callback_email_check');
This way you pass to email_check() only those emails which are validly constructed. Now you are just left to check the domain. Something like:
public function email_check($email)
{
return strpos($email, '#gm.ac.id') !== false;
}
I've made a utility, now when a user applies for forget password, he's shown one text box, where he can enter his email address or password, how to validate email address, because i've i apply valid_email, it'll reject password OR i should show two fields and user has to enter either one, but how to validate that he must enter one of the fields?
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('email', 'Email', 'valid_email');
$this->form_validation->set_rules('email', 'Email', 'callback_email_check');
if ($this->form_validation->run() == FALSE)
{
//fail
}
else
{
//success
}
}
function email_check($str)
{
if (stristr($str,'#uni-email-1.com') !== false) return true;
if (stristr($str,'#uni-email-2.com') !== false) return true;
if (stristr($str,'#uni-email-3.com') !== false) return true;
$this->form_validation->set_message('email', 'Please provide an acceptable email address.');
return FALSE;
}
Try this. or for more details check:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I have a form that I submit with jQuery ajax and have it being sent to a controller function called submit to validate and do any other tasks I need to with the form data. I'm trying to find out why my form validation library isn't showing an error when the username doesn't contain only lowercase letters and numbers.
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
POST Value after form submission:
username TestingUSER
EDIT:
As far as I know it gets to the php server side properly.
PHP:
public function submit()
{
$output_status = 'Notice';
$output_title = 'Not Processed';
$output_message = 'The request was unprocessed!';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
if ($this->form_validation->run() == TRUE)
{
}
else
{
$output_status = 'Error';
$output_title = 'Form Not Validated';
$output_message = validation_errors();
}
echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}
EDIT 2:
Based off of Sheikh answer. I am getting a response back that says "Unable to access an error message corresponding to your field name." It does say Form Not Validated for the title so the message isn't working.
public function check_username($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('username', 'This is not have an accepted value!');
return FALSE;
}
EDIT 3:
What I'm wanting to do is have it report back that there there are validation errors but not the specific errors in the pnotify response. However I do want it to display the specific errors under the form elements.
jQuery Code:
http://pastebin.com/1KehMJkh
Login Form:
http://pastebin.com/EfpBfbfN
I think you can use a callback function in your controller
public function check_username($str)
{
if (preg_match('#[a-z0-9]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
Validation rules for username
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
You may like this too.