Welcome Guest, Not a member yet? Register   Sign In
set_value() apostrophe problem with add/edit form
#1

[eluser]Future Webs[/eluser]
I have a form view that i use for both the add and edit instance of my form.

I use an admin controller and tell the form which action it is and where to submit etc.

The admin controller has all the validation options set up as they are the same for both add and edit.

Here is a very stripped down version of the form to illustrate what im doing

Code:
echo form_open('/users/admin', array('class' => 'form-main', 'id' => 'user_admin'));

// user desc
$user_desc = array(
'name'        => 'user_desc',
'id'          => 'user_desc',
'value'       => set_value('user_desc', (isset($user_info->user_desc)) ? $user_info->user_desc : ''),
'rows'     => '5',
'cols'        => '50'
);

echo form_fieldset();
echo form_label('Description', 'user_desc');
echo form_textarea($user_desc);
echo form_error('user_desc');
echo form_fieldset_close();

// submit button
$submit_button_data = array(
'name' => 'submit',
'id' => 'submit',
'type' => 'submit',
'value' => 'submit'
);
echo form_fieldset();
echo form_button($submit_button_data, 'Submit' , 'class="form-button"');
echo form_fieldset_close();
  
echo form_close() ;

The problem is that if the text area contains any special characters such as & or ' etc then when we view the form again in its update state or even during an error it shows the special characters converted values such as & and '


in the database it stored the characters as they were first input
if i echo the values to the view they display as they were first input

its only when I show the value in the text area of the form the problem occurs

if I remove the set_value function from the value it shows up fine.

Code:
'value'       => set_value('user_desc', (isset($user_info->user_desc)) ? $user_info->user_desc : ''),

I use the above so that if the form validation errors it shows the posted value, if the form view is update and the field has a value it shows that and if its an insert or nothing is entered it defaults to empty

I have the encoding in config and database set as follows

Code:
config.php
$config['charset'] = 'UTF-8';

database.php
$db['default']['char_set'] => 'utf8',
$db['default']['dbcollat'] => 'utf8_general_ci',

header meta
<meta http-equiv="Content-Type" c charset=UTF-8" />

Any ideas where im going wrong ?

Do you need me to tell you anything else in order to work out the problem ?

Thanks in advance for your help
#2

[eluser]CroNiX[/eluser]
Check out the "prep_for_form" function for when you are displaying the data in your form.
http://ellislab.com/codeigniter/user-gui...greference
#3

[eluser]Future Webs[/eluser]
thanks for the reply.

Either Im not using prep_for_form correctly or its not what were looking for. maybe give an example of where the prep_for_form would be used

As i mentioned the data is stored in the db the same as its input so nothing is being converted there. Its only when it then gets shown in the form again for the edit instance and if i remove the set_value function it works ok.

problem is the set_value function is part of the validation so that the form is repopulated with the data that has failed.
#4

[eluser]InsiteFX[/eluser]
This is what I use with my editor.
Code:
public function encode_html($html)
{
    $html = preg_replace('/&/i', '&', $html);
    $html = preg_replace('/</i', '&lt;', $html);
    $html = preg_replace('/>/i', '&gt;', $html);
    return $html;
}
#5

[eluser]Future Webs[/eluser]
would you say there is a problem in my methods though especially the way im using set_value

if I echo the data straight out to the view its fine, just the way its stored in the db. its only when its run through set_value that it comes out bad
#6

[eluser]InsiteFX[/eluser]
Your problem is that a textarea donot have a value property, hence the code I posted above. So set_value will not work on it.

You may also want to read this article:

Add Text to a Textarea or Text Field
#7

[eluser]Future Webs[/eluser]
Thanks again for sticking with it. The set_value does work (for showing the entered value on validation fail) apart from the fact that it changes any special characters.

I did try your function though but no luck.

ill have to try on a fresh install and set up a basic form. There must be something funky with how I have things set up as its such a basic thing

Has anyone else seen the same problem ?

#8

[eluser]Future Webs[/eluser]
this is something that has changed in the develop version as the stable version does not do it

the set_value function has been changed
#9

[eluser]InsiteFX[/eluser]
I looked at it and they have added this to set_value

Code:
set_value($field = '', $default = '', $is_textarea = FALSE)

// So you need to set:
$is_textarea = TRUE

//
'value'       => set_value('user_desc', (isset($user_info->user_desc)) ? $user_info->user_desc : '', TRUE),

It also modifies the for_prep.
#10

[eluser]Future Webs[/eluser]
ive followed it through some more and can see that html_escape is being processed twice

once during the set_value and once during the form_textarea




Theme © iAndrew 2016 - Forum software by © MyBB