Validation Form Edit Codeigniter.

Selamat siang para suhu dan master, Mohon pencerahannya sudah stuck beberapa hari ini, untuk validasi form edit pada codeigniter berikut saya preview tampilan code saya.

Ini file controller dengan methode create() untuk simpan datanya.



	 public function create()
		{
			if (!$_POST){

				$input = (object) $this->electric->getDefaultValues();

			}else{
				$startdate  = $this->input->post('startdate',true);
				$enddate	= $this->input->post('enddate',true);
				$periode	= $startdate .'-'.$enddate;

				$setting 	= $this->input->post('setting',true);
				$fix 		= $this->input->post('fix',true);
				$usages 	= $this->input->post('usages',true);
				$rates 		= $this->input->post('rates',true);
				$basic 		= $this->input->post('basic',true);
				$budget 	= $this->input->post('budget',true);

				$input		= array('periode' => $periode,'setting' => $setting,'fix' => $fix ,
									'usages' => $usages,'rate' => $rates,'basic' => $basic,
									'plan' => $budget,'status' => 'open');

			}


			if(!$this->electric->validate()){
				$halaman		= $this->halaman;
				$main_view		= 'electric/form';
				$form_action	= 'controlelectric/create';


				$dataawal	  	= $this->input->post('startdate',true);
				$dataakhir		= $this->input->post('enddate',true);
				$datasetting	= $this->input->post('setting',true);
				$datafix		= $this->input->post('fix',true);
				$datausages		= $this->input->post('usages',true);
				$datarates		= $this->input->post('rates',true);
				$databasic		= $this->input->post('basic',true);
				$databudget		= $this->input->post('budget',true);


				$this->load->view('template', compact('halaman','main_view','form_action',
														'dataawal','dataakhir','datasetting',
														'datafix','datausages','datarates','databasic','databudget'));

					return;
			}


			if($this->electric->insert($input)){

				$this->session->set_flashdata('success','Data Cost Basic berhasil disimpan.');

			}else{

				$this->session->set_flashdata('error','Data Cost Basic Gagal disimpan.');

			}
			redirect('controlelectric/index');

		}


File controller dengan method edit().


		public function edit($id = null)
		{

			$electric = $this->electric->where('id', $id)->get();

			if (!$electric) {
				$this->session->set_flashdata('warning', 'Data Basic Electric tidak ada.');
				redirect('controlelectric');
			}

			if (!$_POST) {
				$input = (object) $electric;



			} else {
				$input = (object) $this->input->post(null, true);
			}


			if (!$this->electric->validate()) {
				$halaman     = $this->halaman;
				$main_view   = 'electric/form';
				$form_action = "controlelectric/edit/$id";

				$this->load->view('template', compact('halaman', 'main_view', 'form_action', 'input'));
				return;
			}

			if ($this->electric->where('id', $id)->update($input)) {
				$this->session->set_flashdata('success', 'Data Basic Cost berhasil diupdate.');
			} else {
				$this->session->set_flashdata('error', 'Data Basic Cost gagal diupdate.');
			}

			redirect('controlelectric');

		}

Ini form untuk tambah data sekaligus edit.



<div class="container-fluid">

			<?php $attributes = array('name' => 'myform','class' => 'form-horizontal','id' => 'myform'); ?>
			<?= form_open($form_action,$attributes) ?>


			<?= isset($input->id) ? form_hidden('id', $input->id) : '' ?>


			<?php $profield = array('class' => 'fldset','id' => 'fldset'); ?>
		    <?= form_fieldset('Control Cost Electric',$profield) ?>

			<div class="form-body">

			<div class="form-group">

			<?= form_label('Periode', 'periode',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php
			$prostart = array('name' => 'startdate','class' => 'form-control depth tanggal','placeholder' => 'Start Date');
			?>
			<?= form_input($prostart,$dataawal)?>
			</div>
			<?= form_error('startdate') ?>


			<div class="col-md-2">
			<?php
			$proend = array('name' => 'enddate','class' => 'form-control depth tanggal','placeholder' => 'End Date');
			?>
			<?= form_input($proend,$dataakhir)?>
			</div>
			<?= form_error('enddate') ?>


			</div>

			<div class="form-group">
			<?= form_label('Setting Kva', 'setting',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php
			$prosetting = array('name' => 'setting','class' => 'form-control depth','placeholder' => 'Amount');
			?>
			<?= form_input($prosetting,$datasetting)?>
			</div>
			<?= form_error('setting') ?>
			</div>

			<div class="form-group">
			<?= form_label('Fix Rp/Kva', 'fix',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php $profix = array('name' => 'fix','class' => 'form-control depth','placeholder' => 'Fix'); ?>
			<?= form_input($profix,$datafix)?>
			</div>
			<?= form_error('fix') ?>
			</div>


			<div class="form-group">
			<?= form_label('Usage Per/Kwh', 'usages',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php $prousages = array('name' => 'usages','class' => 'form-control depth','placeholder' => 'Usage'); ?>
			<?= form_input($prousages,$datausages)?>
			</div>
			<?= form_error('usages') ?>
			</div>

			<div class="form-group">
			<?= form_label('Rate', 'rates',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php $prorates = array('name' => 'rates','class' => 'form-control depth','placeholder' => 'Rate'); ?>
			<?= form_input($prorates,$datarates)?>
			</div>
			<?= form_error('rates') ?>
			</div>

				<div class="form-group">
			<?= form_label('Basic Cost', 'basic',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php $probasic = array('name' => 'basic','class' => 'form-control depth','placeholder' => 'Amount'); ?>
			<?= form_input($probasic,$databasic)?>
			</div>
			<?= form_error('basic') ?>
			</div>

			<div class="form-group">
			<?= form_label('Budget Plan', 'budget',['class' => 'col-md-2 control-label']) ?>
			<div class="col-md-2">
			<?php $probudget = array('name' => 'budget','class' => 'form-control depth','placeholder' => 'Amount'); ?>
			<?= form_input($probudget,$databudget)?>
			</div>
			<?= form_error('budget') ?>
			</div>
			</div>

			<div class="form-action">

			<!-- Submit button -->
			<?= form_button(['type' => 'submit', 'content' => 'Simpan', 'class' => 'btn btn-sm btn-primary']) ?>
			<?= form_reset(['type' => 'reset', 'value' => 'Cancel', 'class' => 'btn btn-sm btn-default']) ?>
			<?= form_close() ?>
			<?= form_fieldset_close() ?>
			</div>
</div>


Bagaimana cara validasinya,saya bingung pada inputan form untuk default value di form_input('name',$value). variable $value harus diisi variable apa ya gan...

avatar aankusuka
@aankusuka

28 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban