code igniter personal form validation rule does not work - codeigniter

I have this problem on Code Igniter, with a library containing my own validation rules (MY_Form_validation).
I want to check if a field is populate, only if another one is populate. Field2 is required only if field1 is filled.
Here the lines on my controller :
for($j = 1 ; $j < $i ; $j++) {
$this->form_validation->set_rules('day' . $j, 'Jour ' . $j, 'dateIsPosteriorToField[start_date]|trim|xss_clean');
$this->form_validation->set_rules('alert' . $j, 'Alerte jour ' . $j, 'conditionalRequired[day' . $j . ']|trim|xss_clean');
}
(i have multiple fields like that, so I get them with a loop)
And my form validation :
function conditionalRequired($valueTocheck, $conditionalField)
{
$this->set_message('conditionalRequired', 'Le champ %s est requis.');
$conditionalValue = $this->CI->input->post($conditionalField);
if( ! empty($conditionalValue) && empty($valueTocheck)) {
return false;
}
return true;
}
Problem : if field2 is filled, CI goes into the rule and match it. If field2 is empty, CI does NOT go into the rule. I can see that by dumping any value in the rule, it simply ignores the rule if field2 is empty.
There must be something obvious I don't see, please help me !

This might not be the solution you are looking for but you could get around your problem by doing the following.
for($j = 1 ; $j < $i ; $j++) {
$this->form_validation->set_rules('day' . $j, 'Jour ' . $j, 'dateIsPosteriorToField[start_date]|trim|xss_clean');
if ($this->input->post('day' . $j)) {
$this->form_validation->set_rules('alert' . $j, 'Alerte jour ' . $j, 'required|trim|xss_clean');
}
}
this way if the 'day' . $j POST is set the 'alert' . $j is set to be required.
Doing it like this means that you don't need to extend the form_validation class.

Related

see value according to the array

I do a select and get several results in array but I need to get the correct value for each step and set up a condition.
$step = DB::table('records')->where('id_user',$userId)->get();
for($i = 0; $i < count($step); $i++)
{
echo $step[$i]->id_step;
}
Id_step returns me values for each step where on the blade I need to get and see if id_step = 1 is true id_step = 2 is true.
This for is returning me only one value and it has 3 records in the table.
First of all. After a select you get an instance of Eloquent\Collection
Not an array.
So that said to loop do this:
$steps = DB::table('records')->where('id_user',$userId)->get();
foreach($steps as $row) {
echo $row;
}
Since you are familiar with arrays do this:
$steps->toArray();
Now your result is an array
Working insert in view this code.
#for($i = 0; $i < count($step); $i++)
#if($step[$i]->id_step == 2)
working
#else
not working
#endif
#endfor

LARAVEL, data increment

I would like to make an auto increment with a date + a number. Example: 16022017-1. However I can not add the date + the dash + the number.
1- I retrieve the latest invoices in the database
$exist = Contrats::where('number','like','%'.$dateNow->format('dmY').'%')->orderBy('number', 'desc')->get();
2- Here my condition adds value after the date however I can not add the "-" and the number.
if (count($exist) == 0){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY');
} elseif (count($exist) == 1){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY'), '-', 1;
} else {
echo "pb";
}
Do you have an idea how I can increment my date, dash and number? Thank you for your answers.
Quick shoot from the hip answer as i am munching on my salad... Seems that your suffix (number after the date) is the actual count since you seem to be skipping the first instance (0). Eloquent returns a collection... use the ->count() ...
if ($exist->count() > 0){
$date = new \DateTime(null);
$contrat->number = $date->format('dmY') . '-' . $exists->count();
} else {
$date = new \DateTime(null);
$contrat->number = $date->format('dmY');
}

Data in loop is repeated

I am trying to work something out. I have the following
$fileString = "";
if (Input::hasFile('filePath')) {
$files = Input::file('filePath');
dd($files);
foreach($files as $file) {
$file->move(public_path('uploads'), $file->getClientOriginalName());
$fileString .= public_path('uploads') . '/' . $file->getClientOriginalName();
$uploadDoc = new ReportingDocUpload();
$uploadDoc->filename = $file->getClientOriginalName();
$uploadDoc->mime = $file->getClientOriginalExtension();
$uploadDoc->filepath = $fileString;
$uploadDoc->documentId = $dsReportingDoc->id;
$uploadDoc->save();
}
}
For each uploaded file, it should create a new row in the database. The dd I have displays something like the following if I upload 3 documents
array:3 [▼
0 => UploadedFile {#29 ▶}
1 => UploadedFile {#30 ▶}
2 => UploadedFile {#31 ▶}
]
So this all looks good. So, I have a loop which should loop each UploadedFile and save it to the database.
At the moment, this kind of works. A new row is added for each uploaded document, and everything besides one thing looks correct.
The thing that is wrong is the filePath. For the first row, this is fine. The second row in the database contains the filePath for both the first file and the second file. And the third row shows the filePath for all three rows. This should only contain the filePath for the current file.
I have looked over it, and not sure what I am missing, but for some reason this is occuring.
Am I missing something obvious here?
Thanks
Because you are setting the $fileString="" outside the loop, every time it goes in the loop, it concatenates to the exists.
Example
$fileString = ""
Loop 1:
$fileString .= a
Loop 2:
$fileString .= b
$fileString = ab
If you clear the the value in each loop, the result is what you need,
Example:
Loop 1:
$fileString = ""
$fileString = a
Loop 2:
$fileString = ""
$fileString = b
If you use the = instead of the .=, you might not need to assign it to an empty string inside the loop
Move $fileString = ""; inside the foreach loop or change $fileString .= public_path('uploads') . '/' . $file->getClientOriginalName(); to $fileString = public_path('uploads') . '/' . $file->getClientOriginalName();

Make unique invoice numbers with mysql and use

At the moment this is my function to create a random unique invoice number which is stored in a form's hidden field
function generate_invoice_number() {
global $wpdb;
$lastVisitor = $wpdb->get_results("SELECT visitorID FROM event_visitors_2014 ORDER BY visitorsID DESC LIMIT 1", ARRAY_A);
$nr_last = $lastVisitor[0]['visitorID'];
$nr = 501 + $nr_last;
$value = sprintf( '%04d', $nr );
$number = 'LEDEXPO'.date('Y').'-'.uniqid().'-'.$value;
return $number;
}
I have a problem when multiple people are using the form at the same time, say 3 people are using the form they all have the same number generate.
So i added uniqid(), so $value could be duplicated but $number should be unique? Is this correct or is there a better way?
How can i make test function to test this function on uniqueness?
regards
Try This:
function generate_invoice_number()
{
global $wpdb;
$lastVisitor = $wpdb->get_results("SELECT visitorID FROM event_visitors_2014 ORDER BY visitorsID DESC LIMIT 1", ARRAY_A);
$nr_last = $lastVisitor[0]['visitorID'] + 1;
$number = date('Ymd') . $nr_last;
return $number;
}

How to use model object in Yii Controller and View

I have following method:
public function actionIndex() {
$companyModel = Company::model()->findAll();
$supplierProductModel = SupplierProduct::model()->findAll();
$this->render('index', array(
'companyData' => $companyModel,
'supplierProductData' => $supplierProductModel,
));
}
Here I have passed model objects to render function and want to access these objects in view (Active Relational Type) but when I am accessing its in view its showing error:
Trying to get property of non-object
view file (index.php)
echo $companyData->name . "\n";
echo $this->companyModel->phone . "\n";
echo $this->companyModel->fax . "\n";
echo $this->companyModel->cell . "\n";
Any help would be appreciated.
you need to declare $this->companyModel in your controller/action
$this->companyModel = Company::model()->findByPk($companyId);
with Company::model()->findAll() you get an array of Company-Models you can iterate over in your view-file.
foreach ($companyData as $companyModel) {
var_dump($companyModel->attributes);
}
It is happening becoz of findAll()
findAll() reruns all rows of company table in multidimensional array, so here
$companyData is multidimensional Array, now change your code in index like bellow,
<?php
foreach ($companyData as $compSub)
{
echo $compSub->name . "\n";
echo $compSub->phone . "\n";
echo $compSub->fax . "\n";
echo $compSub->cell . "\n";
}
?>
If you want a company data(single row), change your query like this
$companyModel = Company::model()->findByPk($id_Of_company);
//$companyModel is single dimensional array, it has all the info of a company.
Send this to view
$this->render('index', array(
'companyData' => $companyModel,
....................
));
Now you can show the data using bellow code
echo $companyData->name . "\n";
echo $companyData->phone . "\n";
echo $companyData->fax . "\n";
echo $companyData->cell . "\n";
You are trying to get all the entries from the database as findAll() returns all data in multidimensional array of objects.If you need all the entries you could iterate over it in the view file and get the results as shown
In the View File do as shown
<?php foreach($companyData as $value){
echo $vlaue->name . "\n";
echo $value->phone . "\n";
echo $value->fax . "\n";
echo $value->cell . "\n";
?>
With this you get all the entries from the table
If you want to get a particular record use condition using Cdbcriteria and pass the object and get the single result

Resources