Welcome Guest, Not a member yet? Register   Sign In
Trying to apply a function to form validation set_value [SOLVED, with thanks!]
#1

[eluser]nuwanda[/eluser]
Upon errors, I'm trying to use set_value like this in my view:

Code:
'value' => set_value((preg_replace ('/\r\n|\r|\n/', ' ','link_description')),'')

The link_description is the text in a textarea. I want to strip the new lines and carriage returns before displaying it.

The form displays the text as inputted but without the newlines stripped. The preg_replace (tested separately) works.

I'm thinking the set_value can't get a handle on text_description and instead defaults to the original text.

How can I apply a function to the set_value?

Edit: btw, I'm using strip_tags in the validation rule, but that ain't removing the newlines.

Edit 3: if I could apply preg_replace to my validation rule, I'd be fine. But I can't as the rules only take php functions that accept one param. You can do it with something like trim.

Edit 2: my goal is to take the textarea input and save it to my db as plain text. That actually works fine, it's just redisplaying it in the form that's the problem.
#2

[eluser]stormbytes[/eluser]
Code:
function set_field_value($field_name)
{
    if($foo = set_value($field_name))
    {
        $foo = preg_replace($whatever, $args, $go, $here);
    }

    return $foo;
}


Hope that helps! (partially?)
#3

[eluser]Dennis Rasmussen[/eluser]
Code:
'value' => set_value('field_name', preg_replace('/\r\n|\r|\n/', ' ','link_description'))

[quote author="nuwanda" date="1289999187"]Edit 2: my goal is to take the textarea input and save it to my db as plain text. That actually works fine, it's just redisplaying it in the form that's the problem.[/quote]

Don't do it in the validation stage, because the user will then see the stripped text if there's a validation error. Unless you really want to force that?. In that case you can use a callback for custom validation functions.

Code:
function stripall($str) {
  str = preg_replace('/\r\n|\r|\n/', ' ', $str);
  return true;
}
$this->form_validation->set_rule('field_name', 'callback_stripall');

I'm not sure if that's how it's written (can't test it here), but you get the idea.
Read the form_validation section of the userguide.
#4

[eluser]nuwanda[/eluser]
Thanks, stormbytes.

I assume you were suggesting I apply that to my validation rule, thus:

Code:
$this->form_validation->set_rules('link_description','link_desription','trim|strip_newlines);

then

Code:
function strip_newlines($field_name){
  return preg_replace ('/\r\n|\r|\n/', ' ',$field_name);
}

But, no, didn't help. The set_value still returned the text with breaks in the view. It didn't throw an error so it got processed. Weird. I thought it might work like trim does.
#5

[eluser]nuwanda[/eluser]
Dennis, thanks, but see above. Doesn't seem to work as a callback.

Yes, I want the user to see the stripped input.
#6

[eluser]Dennis Rasmussen[/eluser]
You didn't do as I wrote, but that doesn't matter anymore.
Glad you got it fixed.
#7

[eluser]stormbytes[/eluser]
you need to prepend your callback function (in your validation/rules) with the string 'callback_'

Read the manual dude it's all in there, with examples.
#8

[eluser]nuwanda[/eluser]
And I altered the callback to return TRUE
#9

[eluser]nuwanda[/eluser]
Nope.

Code:
$this->form_validation->set_rules('link_description','link_description','trim|callback_strip_newlines');

Didn't work.
#10

[eluser]stormbytes[/eluser]
do you have a function called strip_newlines($field_value) in your (same) controller?




Theme © iAndrew 2016 - Forum software by © MyBB