Welcome Guest, Not a member yet? Register   Sign In
Return back values to view if any error in form fields
#1

Hello all
I created view and controller and model successfully
Every thing is fine except one enhancement:

When submit data to controller if error found in sent input values from view then the controller redirect to view again(form).
Now how can i return back the sent data to view !!
Can anyone send a link to example or idea of doing that, thanks ..
Reply
#2

(This post was last modified: 04-04-2017, 12:21 PM by PaulD.)

It is well documented here in form_validation library docs:
https://www.codeigniter.com/user_guide/l...g-the-form

And here in the form_helper helper docs:
https://www.codeigniter.com/user_guide/h...#set_value

Basically, if your input name is 'user_name' you repopulate the value using something like:

PHP Code:
<input type="text" name="user_name" value="<?php echo set_value('user_name', ''); ?>" /> 

Where the second value is the default value, commonly blank.

Paul.
Reply
#3

Quote: It is well documented here in form_validation library docs:

Thanks Paul for replaying me. Yes I got that but I built the form filed like this:
Code:
                            <?php echo form_input($in_VINn_attributes); ?>

and input attributes from array:

Code:
                    $in_VINn_attributes = array(
                        'class' => 'form-control',
                        'placeholder' => 'like 4455',
                        'id' => 'vin-no',
                        'name' => 'vin-no',
                        'type' => 'text');

How can I process set_value method with that!!
Reply
#4

Hello thanks Paul
But I used the following to create input:
Code:
<?php echo form_input($in_cylinder_attributes); ?>



and I test that :
Code:
 <?php echo form_input($in_cylinder_attributes, set_value('cylinder')); ?>

it give me no thing !
Reply
#5

(This post was last modified: 04-05-2017, 11:00 AM by PaulD.)

It is not something I use but try just adding the value as an attribute in your attributes list, as it is just an attribute pair. Something like:

PHP Code:
// set default
$in_VINn_attributes['value'] = '';

// or with set_value try
$in_VINn_attributes['value'] = set_value('user_name'''); 

I hope that helps.

Paul.

PS Not sure about this but will test for you now. Back soon...
Reply
#6

Yes works fine.

Controller:
PHP Code:
// set data for test input field
$page_data['input_data'] = array(
 
  'value' => set_value('test_input'''),
 
  'class' => 'input_field',
 
  'name' => 'test_input',
);
// set some rule to trigger an error and repopulate the field
$this->form_validation->set_rules('test_input''Test Input''max_length[3]');
$this->form_validation->run(); 

View:
PHP Code:
<?php echo form_open(); ?>
   <?php echo form_input($input_data); ?>
   <?php echo form_error('test_input'); ?>
   <?php echo form_submit(array('value'=>'save')); ?>
<?php 
echo form_close(); ?>

Just make sure you have both the form helper loaded and the form_validation library loaded.

Best wishes,

Paul
Reply
#7

Thanks Paul, Yes it work fine.
I have found I used redirect() instead of $this->load->view('view.php', $data)
So it never got data back to view.

Now : Don't use redirect() to go view use: $this->load->view so set_value() work fine ..
Reply




Theme © iAndrew 2016 - Forum software by © MyBB