model not inserting select option - codeigniter

My model is not inserting my modify and access data that I select on my view.If my select option on my view is select option 1 it inserts value 0 even though 1 selected. Not sure why on model or view its not inserting the correct one?
Each controller has its own modify and access select option i would like to be able to insert if select 1 for enable or 0 disbale?
How can I make the both select options work with my model so inserts correct what have chosen.
Model Function
<?php
class Model_user_group extends CI_Model {
public function addUserGroup() {
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$access = $this->input->post('access');
$modify = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $access,
'modify' => $modify
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
}
View
<?php echo Modules::run('admin/common/header/index');?>
<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><?php echo $title;?></h1></div>
<div class="pull-right">
Cancel
<button type="submit" onclick="submit()" class="btn btn-primary">Save</button>
</div>
</div>
<div class="panel-body">
<?php echo validation_errors('<div class="alert alert-warning text-center">', '</div>'); ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open_multipart('admin/users_group_add', $data);?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php $data1 = array('id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => set_value('name'));?>
<?php echo form_input($data1);?>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Controller Name</td>
<td>Access</td>
<td>Modify</td>
</tr>
</thead>
<?php foreach ($controllers as $controller) {?>
<tbody>
<tr>
<td>
<?php echo ucfirst(str_replace("_"," ", $controller));?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
<td>
<select name="modify" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<?php echo form_close();?>
</div>
</div>
</div><!-- # Page Inner End -->
</div><!-- # Page End -->
</div><!-- # Wrapper End -->
<?php echo Modules::run('admin/common/footer/index');?>

Your view file seems has access and modify option for each controller. But your input name is same.So the value of $access and $modify is the last input value. You can solve this way.Give access and modify input name as array
<select name="access[]" class="form-control">
...
<select name="modify[]" class="form-control">
Now your model
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$accesses = $this->input->post('access');
$modifies = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $accesses[$i],
'modify' => $modifies[$i]
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
Hope you understand and solves your problem

Related

How to accept or reject the data using checkbox in codeiginter?

I am accepting and rejecting some data while clicking checkbox.From my code accept code is working well but rejected is same type of code but it is not working well.
My view page code:
<form class="form-horizontal" method="POST" action="<?=site_url('Request/Update_Event')?>">
<div class="panel panel-flat" id="id">
<div class="panel-heading">
<center> <h4 class="panel-title">Request Portal</h4></center>
</div> <button type="submit" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-thumbs-up"></span>Approved
</button> <a href="<?=site_url('Request/Rejected')?>" class="btn btn-info btn-lg" id="but">
<span class="glyphicon glyphicon-thumbs-down"></span> Rejected
</a><div>
My Controller Code:
public function Update_Event(){
$empid = $this->input->post('empid');
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'green',
'event_status' =>'Approved',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
}
public function Rejected(){
$empid = $this->input->post('empid');
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'red',
'event_status' =>'Rejected',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
print_r($empid);
}
My table code:
<table id="tb2" class="table datatable-responsive" >
<thead><tr><h6><th></th><th align="center">Name</th>
<th class="col-sm" >Leave Date</th>
<th class="col-sm" >Reason</th>
</tr></thead>
<?php foreach ($query as $row): ?>
<tr><td><input type="checkbox" name="empid[]" value="<?php echo $row['empid'];?>"></td>
<td>Leave Applied By <?php echo $row['first_name'];?> <?php echo $row['last_name'];?></td>
<td><?php echo date('d/m/Y', strtotime($row['event_date']));?></td>
<td><?php echo $row['title'];?> </td>
</tr></tbody><?php endforeach ?>
</table>
The rejected code is not working in my code and the empid is not passed to Rejected code when i print the empid value
You are facing this issue because at approval time you are submitting form and in rejection time you are only clicking on link. So when you click on reject button it will not post any value to controller.
You can do something like this. just pass extra parameter to reject function.
<a href="<?=site_url('Request/Rejected/123')?>" class="btn btn-info btn-lg" id="but">
<span class="glyphicon glyphicon-thumbs-down"></span> Rejected
</a>
public function Rejected($empid = ''){
for($i=0; $i< sizeof($empid); $i++)
{
$data = array(
'backgroundColor' => 'red',
'event_status' =>'Rejected',
);
$this->db->where(empid,$empid[$i]);
$this->db->update('events',$data);
}
redirect('Request','refresh');
print_r($empid);
}

Laravel filter reset after next page click in paginate

I have created a table were showing data. I also add few filters to filter data. Using paginate I show 20 records per page. After select filter and click search records in table filter with paginating on the first page but as soon as I click next page filters getting reset. How to stop filters from getting reset?
Below is my code,
public function index()
{
$agos = DB::table('orders')
->leftJoin('companies', 'orders.company_id', '=', 'companies.id')
->select(DB::raw('orders.id, companies.name, orders.type, orders.data, orders.currency, orders.price, orders.status, DATE_FORMAT(orders.created_at,"%M %d, %Y") as created_at '))
->where('orders.merchant', '=', 'agos')
->where(function ($query) {
$status = Input::has('status') ? Input::get('status') : null;
$company = Input::has('company') ? Input::get('company') : null;
$from = Input::has('from_date') ? Input::get('from_date') : null;
$to = Input::has('to_date') ? Input::get('to_date') : null;
$from = date("Y-m-d", strtotime($from));
$to = date("Y-m-d", strtotime($to));
if ( isset($status) ) {
$query->where('orders.status', '=', $status);
}
if ( isset($company) ) {
$query->where('companies.name', '=', $company);
}
if ( !empty($from) && !empty($to) ) {
$query->whereBetween('orders.created_at', [$from, $to]);
}
})->orderBy('orders.created_at', 'desc')
->paginate(20);
return $agos;
}
Blade file code,
#extends('layouts.agos')
#section('title', Translator::transSmart('app.Common Clerk(AGOS)', 'Common Clerk(AGOS)'))
#section('styles')
#parent
{{ Html::skinForVendor('jquery-textext/all.css') }}
#endsection
#section('scripts')
#parent
{{ Html::skinForVendor('jquery-textext/all.js') }}
#endsection
#section('content')
<div class="admin-managing-member-index">
<div class="row">
<div class="col-sm-12">
{{ Form::open(array('route' => array('agos::index'), 'class' => 'form-search')) }}
<div class="row">
<div class="col-sm-3">
<div class="form-group">
#php
$name = 'company';
$translate = Translator::transSmart('app.Company', 'Company');
#endphp
<label for="{{$name}}" class="control-label">{{$translate}}</label>
{{ Form::select($name, $companies->pluck('name', 'name'), Request::get($name), array('id' => $name, 'title' => $translate, 'class' => 'form-control', 'title' => $name, 'placeholder' => '')) }}
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
#php
$name = 'status';
$translate = Translator::transSmart('app.Status', 'Status');
#endphp
<label for="{{$name}}" class="control-label">{{$translate}}</label>
{{Form::select($name, Utility::constant('agos_status', true), Request::get($name), array('id' => $name, 'class' => 'form-control', 'title' => $translate, 'placeholder' => ''))}}
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
#php
$name = 'from_date';
$translate = Translator::transSmart('app.From', 'From');
#endphp
<label for="{{$name}}" class="control-label">{{$translate}}</label>
<div class="input-group schedule">
{{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
#php
$name = 'to_date';
$translate = Translator::transSmart('app.To', 'To');
#endphp
<label for="{{$name}}" class="control-label">{{$translate}}</label>
<div class="input-group schedule">
{{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 toolbar">
<div class="btn-toolbar pull-right">
<div class="btn-group">
{{
Html::linkRouteWithIcon(
null,
Translator::transSmart('app.Search', 'Search'),
'fa-search',
array(),
[
'title' => Translator::transSmart('app.Search', 'Search'),
'class' => 'btn btn-theme search-btn',
'onclick' => "$(this).closest('form').submit();"
]
)
}}
</div>
</div>
</div>
</div>
{{ Form::close() }}
</div>
</div>
<div class="row" >
<div class="col-sm-12">
<hr />
</div>
</div><br>
<div class="row" style="background-color:#FFFFFF">
<div class="col-sm-12">
<div class="table-responsive">
<table class="table table-condensed table-crowded">
<thead>
<tr>
<th>{{Translator::transSmart('app.#', '#')}}</th>
<th></th>
<th>{{Translator::transSmart('app.Company', 'Company')}}</th>
<th>{{Translator::transSmart('app.Products', 'Products')}}</th>
<th>{{Translator::transSmart('app.Total Price', 'Total Price')}}</th>
<th>{{Translator::transSmart('app.Status', 'Status')}}</th>
<th>{{Translator::transSmart('app.Created At', 'Created At')}}</th>
<th></th>
</tr>
</thead>
<tbody>
#if($orders->isEmpty())
<tr>
<td class="text-center empty" colspan="14">
--- {{ Translator::transSmart('app.No Record.', 'No Record.') }} ---
</td>
</tr>
#endif
<?php $count = 0;?>
#foreach($orders as $order)
<tr>
<td>{{++$count}}</td>
<td></td>
<td>{{$order->name}}</td>
<td>
#php
$json = $order->data;
$json = json_decode($json, true);
$products = $json['order_info']['products'];
$data = '';
foreach ($products as $hitsIndex => $hitsValue) {
$data .= $hitsValue['name']. ', ';
}
$data = rtrim($data, ', ');
#endphp
{{$data}}
</td>
<td>
#if(empty($order->price) || $order->price == 0)
{{'Quotation'}}
#else
{{CLDR::showPrice($order->price, $order->currency, Config::get('money.precision'))}}
#endif
</td>
<td>{{Utility::constant(sprintf('agos_status.%s.name', $order->status))}}</td>
<td>{{$order->created_at}}</td>
<td class="item-toolbox">
{{
Html::linkRouteWithIcon(
'agos::edit',
Translator::transSmart('app.Edit', 'Edit'),
'fa-pencil',
['id' => $order->id],
[
'title' => Translator::transSmart('app.Edit', 'Edit'),
'class' => 'btn btn-theme'
]
)
}}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="pagination-container">
#php
$query_search_param = Utility::parseQueryParams();
#endphp
{!! $orders->render() !!}
</div>
</div>
</div>
</div>
#endsection
Controller Code,
public function index(Request $request){
try {
$companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
$orders = (new Agos())->index();
} catch (InvalidArgumentException $e) {
return Utility::httpExceptionHandler(500, $e);
} catch (Exception $e) {
return Utility::httpExceptionHandler(500, $e);
}
$view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));
return $view;
}
Can someone help me?
Try to append the request in the results:
public function index(Request $request){
try {
$companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
$orders = (new Agos())->index();
$queryArgs = Input::only(['status','company','from_date', 'to_date']);
$orders->appends($queryArgs);
} catch (InvalidArgumentException $e) {
return Utility::httpExceptionHandler(500, $e);
} catch (Exception $e) {
return Utility::httpExceptionHandler(500, $e);
}
$view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));
return $view;
}
Then in your template:
<div class="pagination-container">
{!! $orders->render(); !!}
</div>
try to use in your Blade view
$orders->links()

get selected form_dropdown codeigniter from database

i am new with codeigniter, and i am trying to set selected value from database,
this code doesn't work,
<?php
$options = array(
'one' => 'one',
'two' => 'two',
);
$selected = $teach->number;
echo form_dropdown('number', $options, $selected);
?>
but when i try to manual, it work.
<?php
$options = array(
'one' => 'one',
'two' => 'two',
);
$selected = 'one';
echo form_dropdown('number', $options, $selected);
?>
I don't know why,
Any advice would be great. Thanks
this my controller
public function index() {
$this->load->helper('url');
$id = $this->uri->segment(3);
$this->data['teach'] = $this->EditTeachModel->getPosts(); // calling Post model method getPosts()
$this->data['singleTeach'] = $this->EditTeachModel->getPostsId($id); // calling Post model method getPosts()
//view
$this->load->view('edit_teach', $this->data); // load the view file , we are passing $data array to view file
}
public function update(){
$id = $this->input->post('kd');
$this->load->library('form_validation');
$data = array(
'kd' => $this->input->post('kd'),
'name' => $this->input->post('name'),
'date' => $this->input->post('date'),
'number' => $this->input->post('number'),
'pass' => $this->input->post('pass')
);
$this->EditTeachModel->updatePosts($id, $data);
$this->index();
}
This my model
public function getPosts(){
$this->db->from('teach');
$query = $this->db->get();
return $query->result();
}
public function getPostsId($data){
$this->db->select('*');
$this->db->from('teach');
$this->db->where('kd', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function updatePosts($id, $data){
$this->db->where('kd', $id);
$this->db->update('teach', $data);
}
this my view
<?php foreach($singleTeach as $teach) {?>
<form method="post" action="<?php echo base_url() . "edit_teach/update"?>">
<div class="card-body"><td>
<div class="row">
<div class="col-6">
<div class="form-group">
<label id="kd">Kode :</label>
<input type="text" id="kd" name="kd" class="form-control" value="<?php echo $teach->kd; ?>">
</div>
<div class="form-group">
<label id="name">Nama:</label>
<input type="text" id="name" name="name" class="form-control" value="<?php echo $teach->name; ?>">
</div>
<div class="form-group">
<label id="date">Date :</label>
<input type="text" id="date" name="date" class="form-control" value="<?php echo $teach->date; ?>">
</div>
<div class="form-group">
<?php echo form_label('Number '); ?> <?php echo form_error('number'); ?>
<?php
$options = array(
'one' => 'one',
'two' => 'two',
);
$selected = $teach->number;
print_r($teach);
echo form_dropdown('number', $options, $selected);
?>
</div>
<div class="form-group">
<label id="pass">Password :</label>
<input type="text" id="pass" name="pass" class="form-control" value="<?php echo $teach->pass; ?>">
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<?php echo form_submit(array('id' => 'submit','class'=> 'btn btn-primary', 'value' => 'Submit')); ?>
</div>
</form>
<?php } ?>

Input Multiple Data Codeigniter

Sorry if this question has been asked before. I would like to ask for help to check my script below. Because when I tried to submit my form with multiple input field it only results in one data, whereas, there should be two data entered into the database.
So, which part of my script is wrong?
Controller
public function add() {
// ... some script before 'else' ...
} else {
$post = $this->input->post();
$result = array();
$total_input = count($post['input_acc_code']);
foreach ($post['input_acc_code'] as $key => $value) {
$result[] = array(
'trans_type' => 'journal',
'form_type' => NULL,
'acc_code' => $post['input_acc_code'][$key],
'acc_type_id' => $post['input_acc_type_id'][$key],
'refference' => '',
'customer_ID' => NULL,
'acc_side' => '',
'debet' => $post['input_debet'][$key],
'credit' => $post['input_credit'][$key],
'summary' => $post['input_note'][$key],
'files' => NULL,
'create_at' => date("Y-m-d H:i:s", strtotime("now"))
);
if($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {
$this->session->set_flashdata('alert', 'Success');
redirect(base_url().'admin/transaction');
} else {
$this->session->set_flashdata('alert', 'Failed');
redirect(base_url().'admin/transaction');
}
}
}
}
Model
function savedata($table, $data = array()) {
$this->db->insert_batch($table, $data);
if($this->db->affected_rows() > 0) {
return TRUE;
}
return FALSE;
}
View
<?php $attributes = array('class' => 'form-horizontal', 'id' => '');
echo form_open_multipart(base_url().$this->session->userdata('user_status').'/transaction/add', $attributes);?>
<div class="row">
<div class="col-sm-12 col-md-12 panel-form-input">
<div class="form-group form-group-sm">
<label for="input_datetime" class="col-sm-2 control-label">Tanggal Transaksi</label>
<div class="col-sm-10">
<input type="text" class="input-date form-control" name="input_datetime[]" id="input-date">
<?php echo form_error('input_datetime');?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 panel-form-input">
<div class="panel panel-default">
<div class="table-responsive">
<table class="table table-unbordered">
<thead>
<th class="col-25">Account</th>
<th class="col-5">Account Type</th>
<th class="col-35">Notes</th>
<th class="col-15">Debet</th>
<th class="col-15">Credit</th>
<th class="col-5"></th>
</thead>
<tbody>
// First Input Field Form Table
<tr>
<td class="col-25">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
<?php if ($account_list != NULL): ?>
<option>— Choose Account Number —</option>
<?php foreach ($account_list as $value): ?>
<option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
<?php endforeach;?>
<?php else:?>
<option>— No Data —</option>
<?php endif;?>
</select>
<?php echo form_error('input_acc_code[]');?>
</div>
</div>
</td>
<td class="col-5">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
<?php echo form_error('input_acc_type_id[]');?>
</div>
</div>
</td>
<td class="col-35">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_note[]">
<?php echo form_error('input_note[]');?>
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_debet[]">
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_credit[]">
</div>
</div>
</td>
<td class="col-5"></td>
</tr>
// Second Input Field Form Table
<tr>
<td class="col-25">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
<?php if ($account_list != NULL): ?>
<option>— Choose Account Number —</option>
<?php foreach ($account_list as $value): ?>
<option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
<?php endforeach;?>
<?php else:?>
<option>— No Data —</option>
<?php endif;?>
</select>
<?php echo form_error('input_acc_code[]');?>
</div>
</div>
</td>
<td class="col-5">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
<?php echo form_error('input_acc_type_id[]');?>
</div>
</div>
</td>
<td class="col-35">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_note[]">
<?php echo form_error('input_note[]');?>
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_debet[]">
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_credit[]">
</div>
</div>
</td>
<td class="col-5"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="menu-bar">
<button class="btn btn-md btn-primary" type="submit">Save</input>
</div>
</div>
</div>
<?php echo form_close();?>
Thank you for your help....
Try moving the insert_batch related model call out of the foreach, as otherwise you could just run a straight insert there. First you are building the queries to insert, then after the foreach you insert the multidimensional array!
public function add()
{
$post = $this->input->post();
$result = array();
$total_input = count($post['input_acc_code']);
foreach ($post['input_acc_code'] as $key => $value) {
$result[] = array(
'trans_type' => 'journal',
'form_type' => NULL,
'acc_code' => $post['input_acc_code'][$key],
'acc_type_id' => $post['input_acc_type_id'][$key],
'refference' => '',
'customer_ID' => NULL,
'acc_side' => '',
'debet' => $post['input_debet'][$key],
'credit' => $post['input_credit'][$key],
'summary' => $post['input_note'][$key],
'files' => NULL,
'create_at' => date("Y-m-d H:i:s", strtotime("now"))
);
}
if ($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {
$this->session->set_flashdata('alert', 'Success');
redirect(base_url() . 'admin/transaction');
} else {
$this->session->set_flashdata('alert', 'Failed');
redirect(base_url() . 'admin/transaction');
}
}

CakePHP validationErrors empty (validation = true; saveAll = false; validationErrors = null)

Good afternoon.
2 days I have been reading different forums and similar situations regarding the problem that occurs to me, however I have not succeeded in correcting it.
This is my situation:
Order Model
class Order extends AppModel {
public $belongsTo = array("Client", "OrderType");
public $hasMany = array(
'OrderDetail' => array(
'className' => 'OrderDetail',
'foreignKey' => 'order_id',
'counterCache' => true
)
);
public $validate = array(
'client_id' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Favor seleccione un Cliente'
)
),
'date_from' => array(
'required' => array(
'rule' => array('email', 'notEmpty'),
'message' => 'Favor ingresar correo'
)
)
);
}
OrderDetail Model
class OrderDetail extends AppModel {
public $belongsTo = array('Product', 'Smell', 'Order');
public $validate = array(
'product_id' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Debe ingresar un Producto'
),
'quantity' => array(
'required' => true,
'rule' => array('notEmpty'),
'message' => 'Debe ingresar una Cantidad'
)
);
}
OrdersController Controller
class OrdersController extends AppController {
public function index() {
$this->set('title_for_layout', 'Ordenes');
$this->layout="panel";
}
/* Function para Pruebas */
public function test($mode = null) {
$this->set('title_for_layout', 'Ordenes de Prueba');
$this->layout="panel";
$data = $this->Order->Client->find('list', array('fields' => array('id', 'nombre'), 'order' => array('nombre ASC')));
$this->set('clients', $data);
$data = $this->Order->OrderDetail->Product->find('list', array('fields' => array('id', 'name'), 'order' => array('name ASC')));
$this->set('products', $data);
$data = $this->Order->OrderDetail->Smell->find('list', array('fields' => array('id', 'name'), 'order' => array('name ASC')));
$this->set('smells', $data);
if(is_null($mode)) {
$this -> render('Tests/index');
}
if($mode == 'new') {
$this -> render('Tests/new');
// Guardar Nueva Orden
if ($this->request->is('post')) {
/*
$validates = $this->Order->validates($this->request->data);
debug($validates);
$saved = $this->Order->saveAll($this->request->data);
debug($saved);
debug($this->validationErrors);
*/
if($this->Order->saveAll($this->request->data)) {
$this->Session->setFlash('Nueva Orden Creada', 'flash_success');
return $this->redirect('/orders/test');
} else {
$this->Session->setFlash('La orden no pudo ser creada, favor revise el formulario','default',array('class' => 'alert alert-danger center'));
return $this->redirect('/orders/test/new');
}
}
}
}
/* Function para Servicio */
public function service($mode = null) {
$this->set('title_for_layout', 'Ordenes de Servicio');
$this->layout="panel";
if(is_null($mode)) {
$this -> render('Services/index');
}
if($mode == 'new') {
$this -> render('Services/new');
}
}
/* Function para Retiros */
public function removal($mode = null) {
$this->set('title_for_layout', 'Ordenes de Retiro');
$this->layout="panel";
if(is_null($mode)) {
$this -> render('Removals/index');
}
if($mode == 'new') {
$this -> render('Removals/new');
}
}
}
OrderDetailController Controller
Do not exist (I assumed that I do not need it for this purpose)
Order/Test/new.ctp
<div class="matter">
<div class="container">
<!-- Today status. jQuery Sparkline plugin used. -->
<!-- Today status ends -->
<div class="row">
<div class="col-md-12">
<div class="widget">
<div class="widget-head">
<div class="pull-left">Nueva Orden de Prueba</div>
<div class="clearfix"></div>
</div>
<div class="widget-content">
<div class="padd">
<!-- Content goes here -->
<!-- Flash Message -->
<div class="form-group">
<div class="col-lg-12">
<?php echo $this->Session->flash(); ?>
</div>
</div>
<?php echo $this->Form->create('Order', array('class'=>'form-horizontal', 'novalidate'=>'novalidate')); ?>
<h3>Datos del Cliente</h3>
<div class="widget">
<div class="widget-content">
<div class="padd">
<!-- Cliente y Tipo Orden -->
<div class="form-group">
<label class="control-label col-lg-1">Cliente *</label>
<div class="col-lg-5">
<?php echo $this->Form->input('Order.0.client_id', array('label'=>false, 'class'=>'form-control chosen-select', 'placeholder'=>'Seleccione Cliente', 'type'=>'select','options' => $clients, 'empty' => 'Seleccione Cliente')); ?>
</div>
<div class="col-lg-1">
<?php echo $this->Html->link('Nuevo Cliente', '/clients/add', array('class'=>'btn btn-primary')); ?>
</div>
<?php echo $this->Form->input('Order.0.order_type_id', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Ingrese Tipo de Orden', 'type'=>'hidden', 'value'=>3)); ?>
</div>
</div>
</div>
</div>
<h3>Vigencia tentativa de la Prueba</h3>
<div class="widget">
<div class="widget-content">
<div class="padd">
<div class="form-group">
<label class="control-label col-lg-1">Desde</label>
<div class="col-lg-2">
<?php echo $this->Form->input('Order.0.date_from', array('label'=>false, 'class'=>'form-control datepicker', 'placeholder'=>'Desde', 'type'=>'text')); ?>
</div>
<label class="control-label col-lg-1">Hasta</label>
<div class="col-lg-2">
<?php echo $this->Form->input('Order.0.date_to', array('label'=>false, 'class'=>'form-control datepicker', 'placeholder'=>'Hasta', 'type'=>'text')); ?>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<h3 class="pull-left">Elementos a incorporar en Prueba</h3>
<button type="button" class="btn addRow pull-right btn-primary"><i class="fa fa-plus"></i> Agregar Item</button>
</div>
</div>
<div class="widget">
<div class="widget-content">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>#</th>
<th>Item</th>
<th>Cantidad</th>
<th>Aroma</th>
<th>Nº Registro</th>
<th width="90px;">Acciones</th>
</tr>
</thead>
<tbody>
<?php for ($i=0;$i<10;$i++) { ?>
<tr>
<td><?php echo $i+1; ?></td>
<td><?php echo $this->Form->input('OrderDetail.'.$i.'.product_id', array('label'=>false, 'class'=>'form-control chosen-select', 'type'=>'select', 'options' => $products, 'empty' => 'Seleccione Item', 'disabled' => 'disabled')); ?></td>
<td><?php echo $this->Form->input('OrderDetail.'.$i.'.quantity', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Cantidad', 'type'=>'text', 'disabled' => 'disabled')); ?></td>
<td><?php echo $this->Form->input('OrderDetail.'.$i.'.smell_id', array('label'=>false, 'class'=>'form-control chosen-select', 'placeholder'=>'Aromas', 'type'=>'select', 'options' => $smells, 'empty' => 'Seleccione Aroma', 'disabled' => 'disabled')); ?></td>
<td><?php echo $this->Form->input('OrderDetail.'.$i.'.product_number', array('label'=>false, 'class'=>'form-control', 'placeholder'=>'Identificador de Item', 'type'=>'text', 'disabled' => 'disabled')); ?></td>
<td>
<center>
<?php if($i!=0) { ?>
<button type="button" class="btn removeRow btn-xs btn-danger"><i class="fa fa-times"></i> Borrar</button>
<?php } ?>
</center>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<!-- Content ends here -->
</div>
</div>
<div class="widget-foot">
<!-- Botones -->
<div class="form-group">
<div class="col-lg-12">
<?php echo $this->Form->button('Crear Orden', array('class' => array('btn', 'btn-success', 'pull-right'), 'type'=>'submit')); ?>
</div>
</div>
</div>
<?php echo $this->Form->end(); ?>
</div>
</div>
</div>
</div>
</div>
And Now --- The Problem
When I press "Crear Orden" (Save) Button, and Debug this situation I Obtain this...
/app/Controller/OrdersController.php (line 36)
true -> This is debug($this->Order->validates($this->request->data))
/app/Controller/OrdersController.php (line 38)
false -> This is debug($this->Order->saveAll($this->request->data))
/app/Controller/OrdersController.php (line 39)
null -> This is debug($this->validationErrors)
If I enter all correctly, the form is saved successfully. Each field in its respective table, even as a "multiple records".
The problem is that I can not display error messages, because it indicates that the validation is "true"
Please, if you can help me.
Thanks a lot.
Regards,

Resources