How to validate address field in Codeigniter - php

I am trying to apply some validation rules to my form data in CodeIgniter.
Expected Allowed output example like this: 22-some society, some street, city. 223399
What I Entered for check the validation: 42-some Society-3, some street. arcade ###*
This is my function which I use to validate the address.
function addr_line1($addr_line1) {
if (preg_match('/^[a-z0-9 .\-]+$/i',$addr_line1) !== FALSE)
return TRUE;
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
return FALSE;
}
Now I put all my validation in the config/form_validation.php
array(
'field' => 'addr_line1',
'label' => 'Address Line One',
'rules' => 'required|max_length[100]|callback_addr_line1'
),
After all this,I didn't get any validation error.
Am I not following the proper process?
or what should the regex code to validate this type of data?

change from
function addr_line1($addr_line1) {
if (preg_match('/^[a-z0-9 .\-]+$/i',$addr_line1) !== FALSE)
return TRUE;
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
return FALSE;
}
to
function addr_line1($addr_line1) {
if (preg_match('/[\'^£$%&*()}{##~?><>,|=_+¬-]/', $addr_line1))
{
$this->form_validation->set_message('addr_line1', 'allow only space,comma,dot,numbers and alphabets.');
}else{
return true;
}
}
Note:- you can replace £$%&*()}{##~?><>,|=_+¬- with your disallowed character

After Your suggestion and help, I finally found the correct Function.
function _validAddressCheck($addr_line1) {
if (preg_match('/^[0-9a-zA-Z .,-]+$/',$addr_line1)){
return TRUE;
} else {
$this->form_validation->set_message('_validAddressCheck', 'Only Allowed space, comma, dot, dash, numbers and alphabets.');
return FALSE;
}
}
I found that some rules which we have to follow if we are applying callback to the validation.
I have created config validation array at the application/config/form_validation.php
Put the callback function at the controller where I called that validations.
Find this link for creating a regex and test that. Link

<tr>
<td>
<label for="address">Address:</label></td><td>
<textarea name="address" placeholder="Write
something.."><?php echo set_value('address'); ?> </textarea>
</td>
<td>
<p class="err_msg">
<?php
echo form_error('address');
?>
</p>
</td>
in route page:-
$this->form_validation->set_rules('address','add','required|exact_length[18]',array('required'=>"Please Enter Address",'exact_length'=>"Please Enter At Least 10 Character"));

Related

How to set validation message for field having ajax validation in scenarios? - Yii2

I'm using one model file for 2 forms. One for SIGNUP & Other for Adding members.
I didn't set any scenario for SIGNUP form. But, scenario for Adding members form is set.
Model
public function rules() {
return [
//Add Members
['first_name', 'required','message'=>'Please enter first name.','on'=>'addteammembersidebar'],
['email', 'required','message'=>'Please enter email address.','on'=>'addteammembersidebar'],
['mobile','required','message'=>'Please enter mobile number.','on'=>'addteammembersidebar'],
//Common
['first_name', 'required','message'=>'Please enter your first name.'],
['email', 'required','message'=>'Please enter your email address.'],
['mobile','required','message'=>'Please enter your mobile number.'],
];
}
View
Here, I set scenario like $modelTeamMembers->scenario = 'addteammembersidebar';.
<?php foreach ($modelsTeamMembers as $indexMember => $modelTeamMembers):
$modelTeamMembers->scenario = 'addteammembersidebar';
?>
<tr class="house-item">
<td class="vcenter">
<?php
// necessary for update action.
if (! $modelTeamMembers->isNewRecord) {
echo Html::activeHiddenInput($modelTeamMembers, "[{$indexMember}]id");
}
?>
<?php
$modelTeamMembers->first_name = $first_name;
echo $form->field($modelTeamMembers, "[{$indexMember}]first_name")->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->last_name = $last_name;
echo $form->field($modelTeamMembers, "[{$indexMember}]last_name")->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->email = $email;
echo $form->field($modelTeamMembers, "[{$indexMember}]email",['enableAjaxValidation' => true])->label(false);
?>
</td>
<td>
<?php
$modelTeamMembers->mobile = $mobile_number;
echo $form->field($modelTeamMembers, "[{$indexMember}]mobile",
['inputOptions' => ['class' => 'form-control', 'maxlength'=>"10"]])->label(false);
?>
</td>
</tr>
<?php endforeach; ?>
All validation error message working except for email field. If, I remove 'enableAjaxValidation' => true from field, it works. But, for me 'enableAjaxValidation' => true is required.
Image
As in image, it is clearly visible that error message coming "Please enter your email address." Which should be "Please enter email address.". Only email field validation error message not coming correct. Except all are fine.
How to set validation message for email field for scenarios? Any help/hint/suggestions are appreciable.
May I know why exactly do you need to use email validation with AjaxValidation in here? For this type it is enough to write without it since AjaxValidation is more suitable when you want to search and retrieve data from database or other models, not model itself.
However, if you feel you need AjaxValidation, you must set up a few different things since you're current code won't work.
Setting up AjaxValidation in View:
// Set to: index.php?r=profile/email-validation
$form = ActiveForm::begin(['validationUrl' => ['profile/email-validation']]);
// This is set correctly (no changes are needed comparing to your attempt)
echo $form->field($modelTeamMembers, "[{$indexMember}]email", ['enableAjaxValidation' => true])->label(false);
Why this needed? You have set AjaxValidation to be active, but you haven't set URL that this Ajax will work on. It is set in ActiveForm::begin() in most cases.
Setting up AjaxValidation in Controller (required):
// Method that renders your view file
public function actionSomethingThatRendersView()
{
// Code here
$user->scenario = 'addteammembersidebar'; // Custom scenario name
return $this->render(/* the remaining code goes here */);
}
// This is the method that Ajax will send request to ($_POST[])
public function actionEmailValidation()
{
$post = Yii::$app->request->post();
if (!empty($post))
{
Yii::$app->response->format = Response::FORMAT_JSON; // Must be in JSON format
$profile = new User(); // Edit your class name in here
// Custom scenario (must be the same as above otherwise you might get unexpected response)
$profile->scenario = 'addteammembersidebar';
$profile->load($post);
return ActiveForm::validate($profile);
}
}
Why this needed? Ajax will send a request but request without any actions will do nothing. This will "create new" object with same rules and attributes and will attempt to validate with new set of data. For rendering method, $obj->scenario must also be set because otherwise it would use default scenario.
There are no changes to Model. Everything should remain the same as in your example.
In case you want to make it unique email, you have to make changes to Model as well:
public function rules()
{
// ...
['email', 'unique', 'message' => 'Email must be unique'],
// If your attribute is not in the same table as defined in class, then:
['email', 'unique', 'message' => 'Email must be unique', 'targetClass' => User2::className()],
}

Structuring controllers to submit a form in Codeigniter 3

I'm learning Codeigniter and I have a controller named Admin controller
class Admin extends CI_Controller{
/* skipped */
//This function is used to generate changepassword form
public function changepassword(){
$this->data['sessiondata'] = $_SESSION['logged_in'];
$this->data['mainview'] = 'components/admin/changepassword';
$this->load->view($this->layout, $this->data);
}
//changepassword form will be submitted to this function ('admin/checkpassword')
public function checkpassword(){
$error = array(
'required' => '%s tidak boleh kosong',
'matches' => '%s tidak sama, dumb ass'
);
/* some validations skipped */
if($this->form_validation->run($this) == FALSE){
$this->data['mainview'] = 'components/admin/changepassword';
$this->load->view($this->layout, $this->data);
} else {
$tobesent = array(
"oldpassword" => $this->input->post('oldpassword'),
"newpassword" => $this->input->post('newpassword'),
"verifynewpasswprd" => $this->input->post('verifynewpassword')
);
$this->admincrud->changepassword($tobesent);
$this->data['result'] = "Password sukses diubah";
$this->data['mainview'] = 'components/admin/changepassword';
$this->load->view($this->layout, $this->data);
}
}
}
the result is, each time I go to base_url('admin/changepassword'), fill the provided form and then submit the form, my url changes from base_url('admin/changepassword') into base_url('admin/checkpassword'), which I know came as the result of submitting the form. Also each time I type base_url('admin/checkpassword') directly on my address bar, it opens the form, which I know came as the result of the if-else condition in checkpassword function. My question is, from the security standpoint, is it okay if I keep using this structure? and how can I prevent users from directly accessing base-url('admin/checkpassword') and instead redirecting them to base_url('admin/changepassword') ?
well if you don't want the URL to be changed after submitting the form.
You can use redirect('admin/changepassword'); and since you need to provide
messages accordingly, you can use $this->session->set_flashdata('msg','Your message'); before redirection and use it in view like this:
<?php if($this->session->flashdata('msg') <> NULL){echo $this->session->flashdata('msg');} ?>
Solution to your problem is $_SERVER['REQUEST_METHOD'] if i understood correctly...
For example :-
if($_SERVER['REQUEST_METHOD'] == 'POST')//form method is post
{
//checkpassword code
}
else
{
redirect(base_url('admin/changepassword'));
}

Laravel custom password validation

Hi gladly I want to use a custom validation rule for my textfield:
{{Form::label('password', 'Wachtwoord: ', array('class' => 'col-xs-4'))}}
<div class="col-xs-2">{{ Form::password('password', array('class' => 'form-control', 'placeholder'=>'Password')) }}</div>
And I already had followed the instructions on the following link: http://laravel.com/docs/4.2/validation#custom-validation-rules
But I get stuck when I want to use my custom validation.. I think that I have done something wrong in my code.
Gladly I would like to use the validatePassword() method (in validation.php) for my password textfield.
Here is my UserController.php:
public function update($id)
{
$user_old_data = User::find($id);
$input_password = Input::get('password');
Validator::resolver(function($translator, $data, $rules, $messages)
{
return new validation($translator, $data, $rules, $messages);
});
/*Validator::extend('validatePassword', function($input_passwords,$input_passwords){
});*/
/*$validator = Validator::make($data = $thisUser, User::$rules);
if ($validator->passes()) {
if($user_old_data['email'] != $new_email){
$email_exists = User::FindEmailOrFail2(Input::get('email') );
if($email_exists)
return Redirect::back()->withErrors($validator)->withInput();
}
//maak message variabel aan in je user index view.
return Redirect::to('user/users')->with('message', 'Update with new password succesfull');
} */
return Redirect::back()->withErrors($validator)->withInput();
}
Here is my validation.php:
<?php
// app/validators/validation.php
class validation extends Illuminate\Validation\Validator
{
public function validateFoo($attribute, $value, $parameters){
//return $value == 'foo';
echo "this is the: " . $value;
}
//{4,} will match strings of length 4 or more.
protected function validatePassword( $attribute, $value ) {
return (bool) preg_match('/^[a-z]{4,}+$/', $value);
die('test');
}
}
If your code reflects the scope of your project you may not need a custom validator at all. You can use pattern:/^[a-z]{4,}$/ as one of your $rules in your User model.
You can see examples in Laravel's documentation: http://laravel.com/docs/4.2/validation#basic-usage
The pattern you used : /^[a-z]{4,}+$/ can be interpreted as "a string beginning with at least 4 characters matching lowercase a through z and a plus sign at the end of the string". Removing the plus sign (which regex is interpreting literally because of the quantifier before it) should make this work properly.
Remember to artisan optimize after you make changes to make sure the compiled.php is recompiled.

Codeigniter form validation on mutli dimentional form data

I am currently having an issue with validation on dynamic forms in code igniter.
<input name="item1_name[0][PostTitle]">
<input name="item1_name[0][PostSubject]">
<input name="item1_name[0][PostMessage]">
<input name="item1_name[0][PostSlug]">
<input name="item1_name[1][PostTitle]">
<input name="item1_name[1][PostSubject]">
<input name="item1_name[1][PostMessage]">
<input name="item1_name[1][PostSlug]">
Above is a part of my form. This form submits the data as an array. What I want to do is be able to use the codeigniter form validator to validate all the fields. The problem with this currently is that the form is dynamic. The front end allows these sets of inputs to be multiplied an infinite amount of times using javascript. Does anyone have any ideas on how I could solve this issue?
You can use the below method...
//get the array
$item1_name = $this->input->post('item1_name', TRUE);
foreach ($item1_name as $key => $item1_name)
{
// Set the rules
$this->form_validation->set_rules($key."[PostTitle]", "PostTitle", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSubject]", "PostSubject", "trim|required");
$this->form_validation->set_rules($key."[PostSlug]", "PostSlug", "trim|required");
}
Write your own validation function and handle the array however you'd like:
$this->form_validation->set_rules('item1_name', 'Item Name', 'callback_item_name_check');
function item_name_check($value)
{
// evaluate $value and return TRUE or FALSE with error message
if ($value == 'test')
{
$this->form_validation->set_message('item_name_check', 'You need to enter something else.');
return FALSE;
}
else
{
return TRUE;
}
}
More in CodeIgniter documentation.

Custom form validation error message for Codeigniter 2

I have a drop down named "business_id".
<select name="business_id">
<option value="0">Select Business</option> More options...
</select>
Here comes the validation rule, user must select an option.
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]');
Problem being the error message says: The Business field must contain a number greater than 0. Not very intuitive! I want it to say "You must select a business".
I tried:
$this->form_validation->set_message('Business', 'You must select a business');
But CI complete ignores this. Does anyone have a solution for this?
I had the same requirement for adding custom form validation error messages in codeigniter 2 (e.g. "You must agree to our Terms & Conditions"). Naturally it would be wrong to override the error messages for require and greater_than as it would erroneously produce messages for the rest of the form. I extended the CI_Form_validation class and have overridden the set_rules method to accept a new 'message' parameter:
<?php
class MY_Form_validation extends CI_Form_validation
{
private $_custom_field_errors = array();
public function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
// Execute the parent method from CI_Form_validation.
parent::_execute($row, $rules, $postdata, $cycles);
// Override any error messages for the current field.
if (isset($this->_error_array[$row['field']])
&& isset($this->_custom_field_errors[$row['field']]))
{
$message = str_replace(
'%s',
!empty($row['label']) ? $row['label'] : $row['field'],
$this->_custom_field_errors[$row['field']]);
$this->_error_array[$row['field']] = $message;
$this->_field_data[$row['field']]['error'] = $message;
}
}
public function set_rules($field, $label = '', $rules = '', $message = '')
{
$rules = parent::set_rules($field, $label, $rules);
if (!empty($message))
{
$this->_custom_field_errors[$field] = $message;
}
return $rules;
}
}
?>
With the above class you would produce your rule with a custom error message like so:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', 'You must select a business');
You may also use '%s' in your custom message which will automatically fill in the label of fieldname.
If you'd like to customize the error messages that are displayed with each rule, you can find them in an array at:
/system/language/english/form_validation_lang.php
Try not setting the value attribute on the default select...
<select name="business_id">
<option value>Select Business</option> More options...
</select>
and then just using required for your form validation rule...
$this->form_validation->set_rules('business_id', 'Business', 'required');
I suppose you could try editing the way that you're trying to set the message also...
$this->form_validation->set_message('business_id', 'You must select a business');
instead of
$this->form_validation->set_message('Business', 'You must select a business');
I'm not entirely sure if that will do the trick though.
You should extend the Form_validation library as Anthony said.
For instance, I do something like this in a file called MY_Form_validation.php which should be put on /application/libraries
function has_selection($value, $params)
{
$CI =& get_instance();
$CI->form_validation->set_message('has_selection', 'The %s need to be selected.');
if ($value == -1) {
return false;
} else {
return true;
}
}
In your case, because your first option (Guidance option - Please select ...) has the value of 0, you may want to change the conditional statement from -1 to 0. Then, from now on, you could have this line to check selection value:
$this->form_validation->set_rules('business_id', 'Business', 'has_selection');
Hope this helps!
Here's a simple CI2 callback function that I used. I wanted something other than just 'required' as a default param of the validation. The documentation helps: http://codeigniter.com/user_guide/libraries/form_validation.html#callbacks
public function My_form() {
...Standard CI validation stuff...
$this->form_validation->set_rules('business_id', 'Business', 'callback_busid');
...
if ($this->form_validation->run() == FALSE) {
return false;
}
else {
...process the form...
$this->email->send();
}
} // Close My_form method
// Callback method
function busid($str) {
if ($str == '') {
$this->form_validation->set_message('business_id', 'Choose a business, Mang!');
return FALSE;
}
else {
return TRUE;
}
} // Close the callback method
For your case you could change the callback to check for if($str<0) - I'm assuming you've used numbers in your selection/dropdown menu.
If the callback returns false, the form is held and the error message is shown. Otherwise, it's passed and sent to the 'else' of the form method.
A little hack might not good for you but I have done this for a little change.
Example,
I want to change message 'The Email field must be a unique value'.
I have done this as
<?php
$error = form_error('email');
echo str_replace('field must be a unique value', 'is already in use.', $error);
// str_replace('string to search/compare', 'string to replace with', 'string to search in')
?>
If string found then it prints our custom message else it will display error message as it is like 'The Email field must be a valid email' etc...
for those of you working on CodeIgniter 3 you can do the following:
$this->form_validation->set_rules('business_id', 'Business', 'greater_than[0]', array(
'greater_than' => 'You must select a business',
));
And if you are using CodeIgniter 2, you will need to extend and override the CI_Form_validation class (https://ellislab.com/codeigniter/user-guide/general/creating_libraries.html for more info on how to do so) with the new CodeIgniter 3 CI_Form_validation class and use the function above.
The name of the rule is the last parameter.
Please try:
$this->form_validation->set_message('greater_than[0]', 'You must select a business');
More info:
https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
I extended the form_validation library with a simple function that makes sure a drop down box does not have its default value selected. Hope this helps.
application/libraries/MY_Form_validation.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed.');
class MY_Form_validation extends CI_Form_validation {
function __construct()
{
parent::__construct();
$this->CI->lang->load('MY_form_validation');
}
/**
* Make sure a drop down field doesn't have its default value selected.
*
* #access public
* #param string
* #param field
* #return bool
* #author zechdc
*/
function require_dropdown($str, $string_to_compare)
{
return ($str == $string_to_compare) ? FALSE : TRUE;
}
}
application/language/english/MY_Form_validation_lang.php
$lang['require_dropdown'] = 'The %s field must have an item selected.';
How to Use:
1) Make your form drop down box:
<select name="business_id">
<option value="select">Select Business</option> More options...
</select>
2) Create Validation Rule. You might be able to set the value to 0 and use require_dropdown[0] but I've never tried that.
$this->form_validation->set_rules('business_id', 'Business', 'require_dropdown[select]');
3) Set your custom message: (Or skip this step and use the one in the language file.)
$this->form_validation->set_message('business_id', 'You must select a business');
Create Method username_check call back function
01.
public function username_check($str)
{
if ($str=="")
{
$this->form_validation->set_message('username_check', 'Merci d’indiquer le nombre d’adultes');
return FALSE;
}
else
{
return TRUE;
}
}
--
02. Then Put this Validation Code on your class
$this->form_validation->set_rules('number_adults', 'Label Name','Your Message',) 'callback_username_check');
This may help you

Categories