Welcome Guest, Not a member yet? Register   Sign In
Possible to amend user input within 'callback' validation?
#1

[eluser]Andy Ingham[/eluser]
Hi,

Within a 'callback' form validation function, is it possible to amend the value that the user has entered, so that they see the updated value when the form is re-displayed?

My situation is that the user enters a postcode in a form and in the 'callback' function it checks whether the postcode already exists on file. In the 'callback' function before doing the check I remove any spaces the user may have entered and convert to uppercase. If the postcode is found to already exist on file then the form will be redisplayed, but when it's redisplayed I want to show the 'updated' version of the field, i.e. with space removed and converted to uppercase.

Does anyone know how to do this?

Many thanks,

Andy
#2

[eluser]Colin Williams[/eluser]
Provided your form is repopulated via the $_POST array, a la set_value() or otherwise, this should do it:

Code:
$_POST['field_to_ammend'] .= ' Ammendment';
#3

[eluser]Andy Ingham[/eluser]
Colin,

Many thanks for extremely prompt response!

I did try updating the $_POST variable directly, but this didn't seem to work. I wonder if my update is being overwritten in some way before the form is redisplayed?

Anyway, I'll try again just to make sure...
#4

[eluser]Fabdrol[/eluser]
No no, you could do this more easily like this:

Code:
$user_postcode = $this->input->post('postcode', true);
$new_postcode = $this->check_postcode($user_postcode);

$data['updated_postcode'] = $new_postcode;
$this->load->view('viewfile', $data);

the check_postcode method
Code:
function check_postcode($pc) {
$pc = trim($pc);
$pc = str_replace(' ', '', $pc);
$pc = strtoupper($pc);
return $pc;
}

or is your callback function an javascript onsubmit event? in that case you'd echo the new postcode and fill the field in JS with the new postcode.

cheers!
#5

[eluser]Colin Williams[/eluser]
Quote:I wonder if my update is being overwritten in some way before the form is redisplayed?

You know, one of the final things the form_validation class does is return its locally stored data back to the post array, so you're probably right. I would look at the form_validation class structure and update its local copy instead of $_POST. Not exactly pretty but might be the only way to play nice.




Theme © iAndrew 2016 - Forum software by © MyBB