Laravel 4 When i retrieve info from database into

2019-09-07 18:18发布

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.

1条回答
smile是对你的礼貌
2楼-- · 2019-09-07 18:44

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 : '') }}"
查看更多
登录 后发表回答