Support the ongoing development of Laravel.io →
posted 9 years ago
Forms
Last updated 1 year ago.
0

you mean something like a placeholder?

{{ Form::text('statement_email_address', '', array('class'=>'form-control', 'placeholder' => '[email protected]')) }}
Last updated 1 year ago.
0

Thanks for the reply.

Thats great, but how do I output a field from the database as a place holder,

 {{ Form::text('payment_page_url', '', array('class'=>'form-control',
'placeholder' => payment_page_url)) }}

Give me an error:

Use of undefined constant payment_page_url - assumed 'payment_page_url'
Last updated 1 year ago.
0

You passed in a constant. Try $payment_page_url

But if you want to display the data that is actually saved in DB, make sure you have the same names as the database fields. Placeholder attribute is not considered as actual input and won't save anything to DB.

Edit

I just noticed... Change Form::model to something like this

{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
Last updated 1 year ago.
0

Thanks for the reply again, all fields are named the same:

I have added the dollar sign but get an error, Undefined variable: payment_page_url

 {{ Form::text('payment_page_url', '', array('class'=>'form-control',
'placeholder' => $payment_page_url)) }}

I have changed my form to:

	{{ Form::model($organisation, array('route' => array('admin-organisation-details', $organisation-> organisation_id))) }}

Any ideas? I'd like to display current values stored in the db, in my form inputs.

Thanks again

Last updated 1 year ago.
0

One thing I would add is 'method' => 'put' but that probably isn't the reason.

Are you passing in the id in url? Something like localhost:8000/edit/1. If not, edit your route to accept a variable /edit/{id} or change $organisation->organisation_id to the appropriate field name, ie $organisation->organisation_name.

Last updated 1 year ago.
0

Thanks again for the reply.

Here is my controller method:

	// function displays a page where users can update their organisation details
// if called with input GET / POST data, this function will organisation record with that data	
public function updateOrganisationDetails() {
	// Laravel sends a _token with every form it generates to protect against CSRF attacks
	// It does not form part of user data so get all input except this token
	$input = Input::except('_token');

	// session data sometimes seems to persist, so ensure success and error messages are cleared
	Session::forget('success');
	Session::forget('error');

	// only attempt to update organisation details if input data is set
	if (!empty($input)) {
		// get the authorised user and associated organisation
		$organisation = Organisation::find(Auth::user()->organisation_id);

		// ensure organisation url is fully qualified
		if (stripos($input['payment_page_url'], 'http://') !== 0) {
			$input['payment_page_url'] = 'http://' . $input['payment_page_url'];
		}			

		// attempt to update organisation model with input data and generate a success/error message
		if (!$organisation->update($input)) {
			Session::flash('error', 'Sorry, an error occurred. Please try again.');
		}
		Session::flash('success', 'Your organisation details have been updated.');
	}

	// display organisation details page with latest data
	$data['organisation'] = Organisation::find(Auth::user()->organisation_id);
	return View::make('layouts.admin-organisation-details', $data);
}

Here is my model:

// skeleton class to allow access to organisation database table

class Organisation extends Eloquent {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $primaryKey = 'organisation_id';
public $timestamps = false;

// allow the following attributes to be mass-assigned
protected $fillable = array(
'payment_page_url',
 'statement_email_address', 
 'account_name', 
 'sort_code', 
 'account_number', 
 'tx_credit_balance');

}

I'm just learning laravel, can you tell what I need to do from the code above, to get my form populated?

thanks again

Last updated 1 year ago.
0

Here is my route too:

	Route::any ('admin/organisation-details',		array('uses' => 'AdminController@updateOrganisationDetails',
										 			    'as' => 'admin-organisation-details'));
Last updated 1 year ago.
0

You don't need a lot of functionality to view that data. A database entry, which you can create manually if you don't have the functionality yet,

a route something like:

Route::get('organisation/{id}/edit', ['as' => 'organisations.edit', 'uses' => 'OrganisationsController@edit']);
Route::put('organisation/{id}', ['as' => 'organisations.update', 'uses' => 'OrganisationsController@update']);
// or look at resource routing
// Route::resource('organisations', 'OrganisationsController');

Controller

class OrganisationsController extends \BaseController {
    public function edit($id)
    {
        $organisation = Organisation::findOrFail($id);

        return View::make('organisations.edit', compact('organisation');
    }
    
    public function update($id)
    {
        // update code goes here
    }
}

View

{{ Form::model($organisation, array('route' => ['organisations.update', $organisation->organisation_id], 'method' => 'put')) }}
    // Form fields go here
{{ Form::close() }}}

In this case, when you go to page "/organisations/3/edit", you should get the data from organisation with id of 3.

Last updated 1 year ago.
0

Thanks again for your reply.

The route for the view points to this method which uses this view composer:

	$data['organisation'] = Organisation::find(Auth::user()->organisation_id);
	return View::make('layouts.admin-organisation-details', $data);

If my understanding is right, does the form model in the view, need a unique id from this array to populate the form inputs?

Do I need to use the $data array in my form model like this?

	{{ Form::model($organisation, array('route' => array('admin-organisation-details', 
$data[organisation]->id))) }}
Last updated 1 year ago.
0

Also, the form updates the db fine, I just cant seem to display the data in the placeholders on the input edit form.

Last updated 1 year ago.
0

You passed in a $data in view, and have $organisation in form model, and you usually have two different routes. One for create and one for update. With your way you want to do both with one if I understand correctly.

// Controller
$organisation = Organisation::findOrFail($id);
return View::make('layouts.admin-organisation-details', array('organisation' -> $organisation);
// same thing:
return View::make('layouts.admin-organisation-details', compact('organisation');

// View
{{ Form::model($organisation, array('route' => array('admin-organisation-details', 
$organisation->id))) }}

One way would also be something like this

{{ Form::text('statement_email_address', $organisation->statement_email_address, array('class'=>'form-control')) }}

Usually used with Form::open()

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mharrisweb mharrisweb Joined 23 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.