I have a get method search form with a field like this:
{{ Form::text("name", null, array('class' => 'form-control')) }}
My controller look like this:
public function index()
{
...
$result= $this->repo->search($data, $page, $perPage);
return View::make('index', compact('result'));
}
The route look like this:
Route::get('/search', 'controller#index');
and the form:
<form action="/search" id="searchForm" class="search-form">
I want to repopulate the name field to keep his value even after the search submit.
To do this I added to my controller before the view::make:
Input::flash();
I have problems with this flashing, because when I open one of my result line from the search for editing, the old input is not empty and cause false values on the form model binding of this editing page.
How can I repopulate the search form in an other way ? (No model binding possible for this search form)
Finally, I found what I was searching:
{{ Form::text("name", Input::get("name"), array('class' => 'form-control')) }}
With all the magical in Laravel, I thought there is a possibility to do this automatically, but this what I was searching.
Related
I Have this code
{{Form::select('area_id', Area::lists('area_description','area_id') ,'',array('class' => 'form-control'))}
How do I get the current value of Select box from database. BTW I'm using Laravel with Blade Template.
You can use use something like this:
{{
Form::select(
'area_id',
Area::lists('area_description','area_id'),
old('area_id'), // or Input::old('area_id')
['class' => 'form-control']
)
}}
In this case, instead of using Area::lists('area_description','area_id') inyour template use pass it from your controller, for example (in your controller):
$area_ids = Area::lists('area_description','area_id');
return view('view_name')->with('area_ids', $area_ids);
Then use the following in your template:
{{ Form::select('area_id', $area_ids, old('area_id'), ['class' => 'form-control']) }}
For Laravel-5.1.x
The lists method now returns a Collection instance instead of a plain
array for Eloquent queries. If you would like to convert the
Collection into a plain array, use the all method:
$area_ids = Area::lists('area_description','area_id')->all();
Check the upgrade guide for details.
I Have already solved my own problem wew.
with this code:
{{Form::select('area_id', $area_ids , $person->area_id ,array('class' => 'form-control'))}}
I forgot that it should be the id or the option value not the option content. The option that I mean is the <option> tag.
I have Laravel. I have a form. I have a MySQL database. There are some dates in it. When I bind the model and the form, the form is dutifully populated with raw MySQL dates. This is obviously not what I need.
My question is: How do I get a model bound form to display dates in a more readable way?!? There is not chance to intercept and format the data. Or maybe for a more general solution, is there any way to do some proessing on any data between the model and the form, before the user sees it?
Am I thinking of this all wrong? A billion thanks!
You may add an accessor method in your model like this:
public function getCreatedAtAttribute($date)
{
$date = new \Carbon\Carbon($date);
// Now modify and return the date
}
This will be called for created_at. Also check date-mutators if you need to override defaults. Check Carbon Docs.
You can format the date in the form using the format() method.
If you're using it in a form element:
{{ Form::text('name', $model->created_at->format('d/m/Y H:i')) }}
If you're displaying it as plain text:
{{ $model->created_at->format('d/m/Y H:i') }}
If you would like the user to be able to modify the date in a nice way, you could use three different select fields. Here's an excerpt from a project I worked on that allows the user to change the date of birth:
<div class="form-group {{ $errors->has('date_of_birth') ? 'has-error' : '' }}">
{{ Form::label('date_of_birth', 'Date of Birth', ['class' => 'control-label']) }}
<div class="form-inline">
{{ Form::selectRange('date_of_birth[day]', 1, 31, null, ['class' => 'form-control']) }}
{{ Form::selectMonth('date_of_birth[month]', null, ['class' => 'form-control']) }}
{{ Form::selectYear('date_of_birth[year]', date('Y') - 3, date('Y') - 16, null, ['class' => 'form-control']) }}
</div>
{{ $errors->first('date_of_birth', '<span class="help-block">:message</span>') }}
</div>
This is contained within a form that has the model bound to it.
Side Note
The alternative answer posted in here actually overrides the default way that laravel handles dates, and unless you actually return a carbon object, you'll never have the luxury of using the format method, or any of the other handy things that carbon has.
If you have other columns that contain dates, add them to the data mutators list:
public function getDates()
{
return ['created_at', 'updated_at', 'date_of_birth', 'some_date_column'];
}
This will now make it so that each of those is an instance of Carbon allowing you to format as you please, whenever you please and easily modify, duplicate and a whole host of other things. For more information regarding this, see: http://laravel.com/docs/eloquent#date-mutators
You can format all fields retrieved from the database with creating an Accessor in your model. For example if your database field is created_at use getCreatedAtAttribute.
public function getCreatedAtAttribute($date)
{
return Carbon::($date)->format('d/m/Y');
}
I want a select in my form, but the value of the select are dynamic (with ajax)
My entity:
/**
* #ORM\Column(type="string", nullable=true )
* #Assert\NotBlank()
*/
private $city;
My form:
$builder->add('city', 'choice',array('empty_value' => 'choice city', 'choices' => array() ));
But, I have:
This value is not valid..
Because the city are not in the empty array!
Since the city field is a simple string and is not related to a table, simply:
Render the field as a simple input text;
Attach to the input field the Javascript for city-autocompletion through Ajax to fill the input
Then you are ready to submit
or:
Render the field as text.
Hide it in the template
Attach a select through javascript in place of the text-input
Use that select to render the list of cities
On submit get the value from the select and put it as value of the input field.
This approach can be used also to set value for visual widgets, like a star rating.
An other solution with extend form theme:
In my form:
$builder->add('city', 'text',array('data' => 'choice city'));
And in my twig template
{% form_theme form _self %}
{% block _template_defaultbundle_membertype_city_widget %}
<select {{ block('widget_attributes') }} ><option>{{ value }}</option></select>
{% endblock %}
with "template_defaultbundle_membertype_city" is the id of the input
I have few fields on the form like name, description, timestamp.
Now in the form I am only displaying name and description but not timestamp.
public function __construct()
{
$this->setTimestamp(new \DateTime());
}
Now in my database, it is coming as null.
Either doctrine is not executing constructor or those fields are set to null when displayed in form.
Even though I am not displaying them.
You need put the timestamp field in your FormType.
If you don't need to show it, just hide the field and set default value.
Something like that:
$builder->add('timestamp', 'hidden', array(
'data' => new \DateTime(),
));
Don't forget {{form_rest(form)}} at end of the twig template to send all hidden fields.
I want to use hidden field in smarty form
tell me the how i use this
i know how to add element like this
$form -> addElement('submit', 'submit_order', Pay, 'class = "flatButton"');
but i want to used hidden fields with this
Have you tried
$form->addElement('hidden', 'fieldname', 'Text', '');
?