how to validation email in codeigniter only use domain "gm.ac.id"? - php

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;
}

Related

yii1 validation rule with special conditions

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 i should update the email?

This is my validation for updating the email..
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__unique_email[email]');
The call back function is
public function _unique_email($email) {
if ($this->crud_model->fetch_user_by_email_user_id($email,$this->session->userdata('user_id'))) {
return true;
} else {
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');
return true;
return false;
}
}
the email should be updated if the user changes the email in profile settings but if the user not makes any changes the email should be the old one and if he changes the email then it should be update by checking weather the email is unique in the database or not??
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|callback_email_check');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('form');
}
else
{
if($this->update($email)){
$this->load->view('formsuccess');
}
}
}
I haven't used codeIgniter for years, So I don't exactly know how things are there. But I'll try to answer your question on a general context and try to express that using the same code in your question with minor changes.
When You want to check if the user changed his email or not you need to have his old email from database first or in a hidden field of the form, then compare the email field value with the old one will make you know if he changed it.
Second you need to check if the email have duplicates on the database, remember you need to do this only if the user changed his email address, so just run a query with the database. if there are no existing email update the user email in database else display an error and quit.
public function is_unique_email($email) {
$old_email = $this->crud_model->fetch_user_email($this->session->userdata('user_id'));
if ($old_email == $email) {
return true;
} else {
$duplicates = $this->crud_model->fetch_user_by_email($email);
if(count($duplicates) > 0)
{
return false;
}
else
{
return true;
}
}
}
The end code should something similar to this, I'm sorry I can't give you the exact code, But I think you can run with this approach.
I suggest you to move to some good frameworks like symfony, laravel or
zend. Codeigniter is not improved much from those early days where PHP
did a lot.

Form validation code igniter

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

Codeigniter Form Validation in Model

Hello all, this is my first CI project.
I have a simple form validation function in my model.
function verify_login()
{
//This method will have the credentials validation
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
var_dump($this->form_validation->run());
die;
if ($this->form_validation->run() == FALSE) {
//Field validation failed. User redirected to login page
$this->load->view('login_view');
} else {
//Go to private area
redirect('home', 'refresh');
}
}
This only works when it's in a controller but not in a model. When I try passing the variables from the controller to the function in the model, the variables get received but won't process.
Can someone enlighten me? Thank you.
its fine to do your form validation in a model. But you want to have the validation return True or False to your controller. Not call a view. So like
// in your Model lets call it Users
function verify_login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
if ($this->form_validation->run() == FALSE) {
return FALSE ;
} else {
return TRUE;
}
}
// Your callback function
// in Controller
function verify(){
if( $this->users->verify_login() == FALSE ){
// $this->errormessage will be available in any view that is called from this controller
$this-errormessage = "There was an error with your Log In. Please try again." ;
$this->showLogin() ; }
else {
// set a session so you can confirm they are logged in on other pages
$this->setLoginSession($this->input->post('username', TRUE)) ;
$this->showUserHome(); }
}
Another thing to think about -- often people know their user name but mess up their password. So if you check for them separately you can adjust the error message accordingly. And if you check for user name and there are no results -- you don't need to check for password and in the error message you can tell them there is no user by that name.
My biggest recommendation to you is to not do validations like this in your model. If you're validating in your model it needs to be against a database value directly and not a form.
Please let me know if that solves your problem, if not please comment and I'll edit my answer.
UPDATE: Please ignore some of the above, as I was going off theory and not fact :)
I'll have to dig deeper into the CI core to get a good idea of what's wrong with this. Your code itself looks ok. Only thing I can see is that your callback may not exist in your model and only in your controller. Echoing the below I do not consider this a good use of the model.
The docs on validations
class Data_model extends CI_Model
{
public function rules()
{
return [
['field' => 'pertanyaan',
'label' => 'pertanyaan',
'rules' => 'required|is_unique[data.pertanyaan]'],
['field' => 'jawaban',
'label' => 'jawaban',
'rules' => 'required']
];
}
}
class Datas extends CI_Controller
{
public function add()
{
$data = $this->data_model;
$validation = $this->form_validation;
$validation->set_rules($data->rules());
if ($validation->run()) {
$data->save();
$this->session->set_flashdata('success', 'Berhasil disimpan');
}
$this->load->view("admin/data/new_form");
}
}

CodeIgniter form validation valid_email not working

I am trying to use CodeIgniter's form validation class. I've used the "valid_email" parameter, as can be seen from the code below, but even when an invalid email address is entered it still passes the validation check. I tested with the string: testing123
public function login()
{
$this->form_validation->set_rules('authEmail', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('authPassword', 'trim|required');
$email = $this->input->post('authEmail');
$password = $this->input->post('authPassword');
if($this->form_validation->run() === FALSE) {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/');
} else {
// Begin authentication
}
}
Anyone have any idea what I'm doing wrong or if this is a CodeIgniter issue?
To note, I am setting a flashdata session as opposed to using:
<?php echo validation_errors(); ?>
... this is because I am doing a redirect back to the homepage (which is the login page as it's a private site).
Try this:
public function login()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('authEmail', 'Email', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('authPassword', 'Password', 'trim|required');
if($this->form_validation->run() !== false){
//validation passed
$email = $this->input->post('authEmail');
$password = $this->input->post('authPassword');
// Begin authentication
}
else {
$this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>'));
redirect('/');
}
}
I'm just learning to use it as well, but don't you need three parameters? This is from their form validation page:
$this->form_validation->set_rules('email', 'Email', 'required');
From http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules
The field name - the exact name you've given the form field.
A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names.
The validation rules for this form field.

Categories