rowanwins's avatar

Concatenate strings in a controller?

Hi there,

Im trying to work out if I can concatenate some strings together in my controller.

$oldlat = Form::old('lat');
$oldlong = Form::old('long');
$oldMarker = $oldlat . ' ' . $oldlong;

I'm trying to grab some form data from a form that has been incorrectly submitted and then concatenate it together so I can feed it back into my view however it doesnt seem to be working. At the moment Im getting an error which says "syntax error, unexpected '$oldlong' (T_VARIABLE)"

Any tips or suggestions on a better way to do this? Thanks, Rowan

0 likes
4 replies
aliqsyed's avatar

The way to get to the old input is

Input::old('lat');

Here is the framework documentation about this http://laravel.com/docs/requests#old-input

However Laravel gives you a very simple way to deal with validation. Generally this is what the code in your controller should look like in Laravel 4.2. This would go in the controller method where your form is being submitted.

$rules = []; //specify your validation rules here

$validator = Validator::make(Input::all(), $rules);

if ($validator->fails())
{
        return Redirect::to('register')->withInput()->withErrors($validator);
}

in your view you can get to the errors by using either

{{ $errors->first('email') }} // get error for the email field

or to get all the errors in the form

@foreach ($messages->all('<li>:message</li>') as $message)

    {{ $message }}

@endforeach

Link to validation documentation: http://laravel.com/docs/validation

This is the simplest way to do this. Once you are a little more comfortable with the framework you can use Laracasts/Validation package.

In Laravel 5 the validation is done differently using the FormRequest object. Here is a link to the video https://laracasts.com/series/whats-new-in-laravel-5/episodes/3

1 like
rowanwins's avatar

Hi @aliqsyed ,

Thanks for your response, I'm already doing all those things in terms of validation etc but my problem is a little different.

My form has a map which the user moves around, the results of their movement get saved in 2 fields, lat and long. If the user misfills the form, after the redirect the map returns to the default location.

So in my template I want to do something like

{{ $oldMarker or 'default' }}

where the var is

$oldlat = Form::old('lat');
$oldlong = Form::old('long');
$oldMarker = $oldlat . ' ' . $oldlong;

I seem to be able to bring across a single var just fine (eg $oldlat ) however it's freaking out when I try to concatenate the required fields...

I hope that helps explain it more.

Cheers Rowan

1 like
rowanwins's avatar

bump? Surely I'm missing something simple here....

keevitaja's avatar

Are you saying, that both $oldlat and $oldlong are strings and you can't concat them? It can't be.

do var_dump() on both variables.

also show us the entire code!

Please or to participate in this conversation.