0

I'm making some form validation and this is how I output it:

if(isset($myvalidation)) {
echo $myvalidation->form_errors('pass');
}

And it works.

The problem arise when I try to concatenate it with string, like so:

if(isset($myvalidation)) {
echo "First" . $myvalidation->form_errors('pass') . "Last";
}

The output is odd: I see "First" at the end of my outputted error.

Did I made some mistake or something?

  • 2
    Do you echo inside `$myvalidation->form_errors('pass')` ? – Dormilich Apr 23 '18 at 12:34
  • where are you doing this concatenation in view? – Haider Ali Apr 23 '18 at 12:35
  • What do you mean Dormilich? I store errors in array and then output them by key-value – zohigekid Apr 23 '18 at 12:36
  • 1
    @Dormilich pointed it well, would you show us form_errors() ? – Cid Apr 23 '18 at 12:36
  • Yes, Haider Ali. In the view, next to the form fields – zohigekid Apr 23 '18 at 12:37
  • _“Did I made some mistake or something?”_ - yes, you are writing functions that echo output directly. Don’t do that, use `return` inside the function to have it _return_ the error message, instead of outputting it directly. – CBroe Apr 23 '18 at 12:41
  • I use class, and inside of it public function with the logic like this: if(condition) {switch cases... in each, I have if(condition){$this->myerrors(array($item=>"$item should be at least $myvalues characters's."));} just for example – zohigekid Apr 23 '18 at 12:43
  • CBroe, I am not. I use public function with return value of my errors. But it's not the problem, really... I can get the errors. I just don't get it why I can't concatenate them with string – zohigekid Apr 23 '18 at 12:45
  • do you use var_dump? don't. – delboy1978uk Apr 23 '18 at 12:46
  • @zohigekid The observed behaviour can be easily explained, if `->form_errors()` contains echo statements. – Dormilich Apr 23 '18 at 12:47
  • @Dormilich you're right! :) I did echo for some reason in that function... I switched it for return, and it worked. But I noticed some odd character at the end of the output: "" I didn't copy anything and I am writing in UTF-8... Do you have any idea where is this coming from? – zohigekid Apr 23 '18 at 12:56
  • @zohigekid That could be the HTML-encoded BOM. cf. https://stackoverflow.com/questions/6784799/what-is-this-char-65279 – Dormilich Apr 23 '18 at 13:00
  • Thousand of thanks to you, @Dormilich! – zohigekid Apr 23 '18 at 13:04

2 Answers2

0

If you are doing it in blade do it like this

@if(isset($myvalidation)) 
    <p>First {{$myvalidation->form_errors('pass')}} Last</p>
@endif
Haider Ali
  • 1,081
  • 8
  • 23
0

The reason for the error may be that form_errors() does not return a string value.

Depending on the framework you're using, try one of the following:

if(isset($myvalidation)) {
  echo "First" . (string) $myvalidation->form_errors('pass') . "Last";
}

or

if(isset($myvalidation)) {
  echo "First" . $myvalidation->form_errors('pass')->__toString() . "Last";
}
mike
  • 5,047
  • 2
  • 26
  • 32