Laravel 4 When i retrieve info from database into input text, 1 extra space is added

I am doing a form where the user can update his information. The form shows several inputs and the info retrieved from the database is displayed inside them.
I have noticed that, when the info is displayed, one extra space is added. For example, if i retrieve a date ‘1965-08-29’, then in the form i notice that an extra space is added at the end: ‘1965-08-29 ‘.

This is happening in all inputs causing validation fails.

Here is a sample code about how i retrieve the data inside the controller:

$id_user=Auth::user()->id;
    $usuario=User::find($id_user);
    if(isset($usuario->nombre_pila)){$nombrepila=$usuario->nombre_pila;}else{$nombrepila='';}
    if(isset($usuario->ap_paterno)){$ap_paterno=$usuario->ap_paterno;}else{$ap_paterno='';}
    if(isset($usuario->ap_materno)){$ap_materno=$usuario->ap_materno;}else{$ap_materno='';}

Here is how i send these data to the view:
return View::make('profile.edit',array('id_user'=>$id_user, 'nombrepila'=>$nombrepila, 'ap_paterno'=>$ap_paterno,'ap_materno'=>$ap_materno));

Here are some examples of the inputs in the view:
    <div class="form-group @if ($errors->has('nombre_pila')) has-error @endif"><!--Nombre de pila-->
        <label for="nombre_pila" class="col-sm-2 control-label">Nombre(s) de pila:</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="nombre_pila" name="nombre_pila" maxlength="45" value="@if(isset($nombrepila)){{$nombrepila}} @endif">
        </div>    
    @if($errors->has('nombre_pila'))
        {{$errors->first('nombre_pila')}}
    @endif
    </div><!-- ----------fin nombre de pila ------------ -->

    <div class="form-group @if ($errors->has('ap_paterno')) has-error @endif"><!--apellido paterno-->
        <label for="ap_paterno" class="col-sm-2 control-label">Apellido paterno:</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="ap_paterno" name="ap_paterno" maxlength="45" value="@if(isset($ap_paterno)){{$ap_paterno}} @endif">
        </div>
    @if($errors->has('ap_paterno'))
        {{$errors->first('ap_paterno')}}
    @endif    
    </div><!-- ----------fin de apellido paterno ------------ -->

I am using bootstrap 3 forms (i don’t know whether this has to do with the problem or not)

It could be that i should use something like the rtrim() function. But if someone out there could explain me why this is happening or what i am missing to do, i’ll appreciate it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You have a space before your @endifs. To fix that either do

value="@if(isset($ap_paterno)){{$ap_paterno}}@endif"

or (a bit nicer in my opinion)
value="{{ (isset($ap_paterno) ? $ap_paterno : '') }}"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x