How i can pass the third parameter into the form validation callback function which is field name? - codeigniter

here is the validation code :
public function registration () {
//
//
// -- code
$this->form_validation->set_rules('password', 'Password', 'required|callback__check_length[6,10]');
}
function _check_length($input, $min, $max)
{
$length = strlen($input);
if ($length <= $max && $length >= $min)
{
return TRUE;
}
elseif ($length < $min)
{
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
}
elseif ($length > $max)
{
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
it is giving me error :
Message: Missing argument 3 for Person::_check_length(), called in C:\wamp64\www\abc\system\libraries\Form_validation.php on line 744 and defined

You can do so by exploding the 2nd param. CI doesn't seem to support 3 params in function args for form validation rules:
public function _check_length($input, $minmax) {
$minmax = explode(',', $minmax);
$min = $minmax[0];
$max = $minmax[1];
$length = strlen($input);
if ($length <= $max && $length >= $min) {
return TRUE;
} elseif ($length < $min) {
$this->form_validation->set_message('_check_length', 'Minimum number of characters is ' . $min);
return FALSE;
} elseif ($length > $max) {
$this->form_validation->set_message('_check_length', 'Maximum number of characters is ' . $max);
return FALSE;
}
}
You also don't have to have your own function to do this. You can simply use min_length[x] and max_length[x] rules.
https://www.codeigniter.com/userguide3/libraries/form_validation.html#rule-reference

Related

Find the error that array_combine(): Both parameters should have an equal number of elements

I am importing CSV file to Laravel controller and insert data into two tables, but I got error-
array_combine() Both parameters should have an equal number of elements
function csvToArray ($filename = '', $delimiter = ',')
{
If (! file_exists ($filename) ||!is_readable($filename))
return false;
$header = null;
$data = array ();
if (($handle = fopen ($filename,'r')) !== false)
{
while (($row = fgetcsv($handle, 300000, $delimiter)) !== false)
{
if (!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
public function importCsv()
{
$file = public_path('file/city_master.csv');
$customerArr_data = $this->csvToArray($file);
for ($i = 0; $i < count($customerArr_data); $i++)
{
dd($customerArr_data);
}
return 'not in array';
}
Please try using array_merge() instead. Hope this helps

How to compare value from table in call back function in codeigniter form validaiton

This is my Callback function for form validation in CodeIgniter
public function user_check($str)
{
if ($str == 'sounitkar13')
{
return TRUE;
}
else
{
$this->form_validation->set_message('user_check', 'The %s is not found in our record');
return FALSE;
}
}
right now i am comparing $str with "sounitkar13" but i have a table with 100 username stored so i want to compare with those 100 user names and if it matches with any of those 100 then return TRUE otherwise FALSE. ( it is reverse of is_unique function.
you have to do condition on table rows using where condition
$query = $this->db->get_where('table', array(
'username' => $str
));
$count = $query->num_rows();
if ($Count != 0)
{
return TRUE;
}
else
{
$this->form_validation->set_message('user_check', 'The %s is not found in our record');
return FALSE;
}

Unique Case of "Call to a member function set_type() on null"

Hello guys i have gone through all 25 questions relating to the title of my question and i am made to believe my scenario is totally different.
Here is the error
Fatal error: Uncaught Error: Call to a member function set_type() on null in /path_to_file/login.php:55 Stack trace: #0 /path_to_call_page/login.php(24): login->authenticate() #1 {main} thrown in /path_to_file/login.php on line 55
Here is my code
<?php
class login
{
public $username;
public $password;
public $err;
public $table_name;
public $session_data;
public $md5 = true;
public $username_column = "username";
public $password_column = "password";
public $builder;
function _construct($username,$password)
{
$this->username = $username;
$this->password = $password;
$this->builder = new queryBuilder();
}
function set_table($tablename)
{
$this->table_name = $tablename;
}
/*
* Tells the login class where to find the username and password in database table
*/
function set_columns($username_col,$password_col)
{
$this->username_column = $username_col;
$this->password_column = $password_col;
}
function md5_on()
{
$this->md5 = true;
}
function md5_off()
{
$this->md5 = false;
}
function authenticate()
{
$db = new mySQLConnection();
$db->select();
// if md5 is turned on
if($this->md5)
$this->password = md5($this->password);
$this->builder->set_type("SELECT");
$this->builder->set_table_name($this->table_name);
$this->builder->set_where("WHERE $this->username_column = '$this->username' and $this->password_column = '$this->password'");
$query = $this->builder->build_query();
if($db->execute_query($query))
$data = $db->fetch($db->result);
else
{
die($db->error);
}
if($db->rows_affected() == 1)
{
$this->session_data = $data[0];
return true;
}
else
{
$this->err = "Invalid username or password. Please try again";
return false;
}
}
function get_error()
{
return $this->err;
}
}
?>
The error occurs everywhere i have
$this->builder
And i have defined it in the _construct method.
This is the queryBuilder class
class queryBuilder
{
var $data;
var $field;
var $tableName;
var $databaseName;
var $where;
var $order;
var $group;
var $limit;
var $queryString;
var $error;
private function put_quotes1($field)
{
$field = trim($field);
$field = "`".$field."`";
return $field;
}
private function put_quotes2($field)
{
$field = trim($field);
$field = "'".$field."'";
return $field;
}
function set_type($type)
{
$this->type = $type;
}
function set_data($data)
{
$this->data = $data;
}
function set_field($field = null)
{
$this->field = $field;
}
function set_where($where)
{
$this->where = $where;
}
function set_limit($limit)
{
$this->limit = $limit;
}
function set_order($order)
{
$this->order = $order;
}
function set_table_name($name)
{
$this->tableName = $name;
}
function prepare_data($data)
{
if(is_array($data))
{
foreach($data as $k => $v)
{
$this->field[] = $k; //setting the column names
$this->data[] = $v; // setting the values
}
}
}
function build_query()
{
switch($this->type)
{
case 'SHOW':
$database_name = $this->put_quotes1($this->databaseName);
$this->queryString = "SHOW ";
if(!isset($this->field) || is_null($this->field))
$this->queryString .= "DATABASES LIKE 'thirdeye%'; ";
else{
$noFields = count($this->field); //no of fields in table
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1)) // if on the last field
$this->queryString .= $this->put_quotes1($this->field[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
}
break;
case 'INSERT':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "INSERT INTO ".$table_name." (";
$noFields = count($this->field);
$noData = count($this->data);
if($noFields > 0 && $noData > 0)
{
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1))
$this->queryString .= $this->put_quotes1($this->field[$i]).')';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
$this->queryString.= " VALUES (";
for($i = 0; $i < $noData; $i++)
{
if($i == ($noData -1))
$this->queryString .= $this->put_quotes2($this->data[$i]).');';
else
$this->queryString .= $this->put_quotes2($this->data[$i]).',';
}
}
else
{
$this->error = "No column name or data was supplied";
}
break;
case 'SELECT':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "SELECT ";
if(!isset($this->field) || is_null($this->field))
$this->queryString .= "* ";
else{
$noFields = count($this->field); //no of fields in table
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields- 1)) // if on the last field
$this->queryString .= $this->put_quotes1($this->field[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i]).',';
}
}
$this->queryString .= "FROM ".$table_name;
if(isset($this->where))
$this->queryString .= " ".$this->where;
if(isset($this->order))
$this->queryString .= " ".$this->order;
if(isset($this->limit))
$this->queryString .= " ".$this->limit;
else
$this->queryString .= ";";
break;
case 'UPDATE':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "UPDATE ". $table_name. " SET ";
$noFields = count($this->field); //no of fields in table
if(is_array($this->field) && is_array($this->data) && isset($this->where))
{
for($i = 0; $i < $noFields; $i++)
{
if($i == ($noFields -1))
$this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).' ';
else
$this->queryString .= $this->put_quotes1($this->field[$i])." = ". $this->put_quotes2($this->data[$i]).',';
}
$this->queryString .= " ".$this->where.";";
}
else
{
$this->error = "Cannot build query. One of the following was not set";
}
break;
case 'DELETE':
$table_name = $this->put_quotes1($this->tableName);
$this->queryString = "DELETE FROM ".$table_name;
if(isset($this->where))
{
$this->queryString .= " ".$this->where.";";
}
else
{
$this->error = "Connot build. No condition was set";
}
break;
}
return $this->queryString;
}
}
Any pointers would help. Remember i have been through previous questions so a suggested edit or code answer would be great.

add parameter to Laravel Accessor

I have an accessor that is defined like that:
public function getNameAttribute($name)
{
return trans($name);
}
Now, I would like to add parameters to my translation, so I would like to have something like that:
public function getNameAttribute($name)
{
return trans($name, ['age' => $age]);
}
Is it possible? How should I define accessor, and how should I call send the param?
EDIT 1: Here are my model attributes:
protected $fillable = [
'id',
'name',
'gender',
'isTeam',
'ageCategory',
'ageMin',
'ageMax',
'gradeCategory',
'gradeMin',
'gradeMax',
];
And here is how I get $age variable inside my model:
public function getAgeString()
{
$ageCategoryText = '';
$ageCategories = [
0 => trans('core.no_age'),
1 => trans('core.children'),
2 => trans('core.students'),
3 => trans('core.adults'),
4 => trans('core.masters'),
5 => trans('core.custom')
];
if ($this->ageCategory != 0) {
if ($this->ageCategory == 5) {
$ageCategoryText = ' - ' . trans('core.age') . ' : ';
if ($this->ageMin != 0 && $this->ageMax != 0) {
if ($this->ageMin == $this->ageMax) {
$ageCategoryText .= $this->ageMax . ' ' . trans('core.years');
} else {
$ageCategoryText .= $this->ageMin . ' - ' . $this->ageMax . ' ' . trans('core.years');
}
} else if ($this->ageMin == 0 && $this->ageMax != 0) {
$ageCategoryText .= ' < ' . $this->ageMax . ' ' . trans('core.years');
} else if ($this->ageMin != 0 && $this->ageMax == 0) {
$ageCategoryText .= ' > ' . $this->ageMin . ' ' . trans('core.years');
} else {
$ageCategoryText = '';
}
} else {
$ageCategoryText = $ageCategories[$this->ageCategory];
}
}
return $ageCategoryText;
}
You have 2 options,
First:
If you want to get as attribute name a message translated, you can try this:
public function getNameAttribute($name)
{
return trans($name, ['age' => $this->getAgeString()]);
}
Second:
If you want a translate as additional field you can use Mutator adding an append to your model:
protected $appends = array('nameWithAge');
and define the method to get the name
public function getNameWithAgeAttribute()
{
return trans($this->attributes['name'], ['age' => $this->getAgeString()]);
}
This will do to nameWithAge an attribute more of the model and you can access it as such.

fatal error call to undefined function

i've wrote this code,
<?php
class fizzbuzz{
function mod3($angka)
{
$a = $angka % 3;
if($a==0) return true;
else return false;
}
function mod5($angka)
{
$b = $angka % 5;
if($b==0) return true;
else return false;
}
function index(){
for ($i=1; $i < 101; $i++) {
if(mod3($i) == true && mod5($i) == true){
echo "fizzbuzz, ";
}else if(mod3($i) == true){
echo "fizz, ";
}else if(mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}
}
$show = new fizzbuzz;
$show->index();
?>
and then it came up with this error
Fatal error: Call to undefined function mod3() in C:\xampp\htdocs\tes-bimasakti\fizzbuzz.php on line 19
please help me with this error..
You forgot $this->:
if($this->mod3($i) == true && $this->mod5($i) == true){
^^^^^^^--- here ^^^^^^^---here
without $this->, php is looking for a top-level global function. It will NOT look for a method in your object.
use this keyword
function index(){
for ($i=1; $i < 101; $i++) {
if($this->mod3($i) == true && $this->mod5($i) == true){
echo "fizzbuzz, ";
}else if($this->mod3($i) == true){
echo "fizz, ";
}else if($this->mod5($i) == true){
echo "buzz, ";
}else echo $i.", ";
}
}

Resources