How to make Laravel (Blade) text field readonly - php

I have a text box that needs to be made readonly; I don't want to use array('disabled' => 'true') because I need PHP to process the field:
{{ Form::text('login_token', Worker::generateLoginToken()) }}
How do you add this attribute?

Just add it as the 3rd argument:
{{ Form::text('login_token', Worker::generateLoginToken(), ['readonly']) }}

That's how I did it in Laravel 5:
{!! Form::text('id', null, ['class' => 'form-control', 'readonly' => 'true']) !!}
Cheers.

Write the following line
{!! Form::text('field_name','field_value',array('class'=>'form-control','readonly')) !!}

For Laravel 5 and above
{!! Form::text('name', 'default-value', ['class'=>'class-name','readonly']) !!}
In third argument you can pass all your extra arguments in form of an array. This line will result into something like this in html.
<input class="class-name" readonly="readonly" name="name" type="text" value="default-value">
For Laravel < 5 , this should work
{{ Form::text('name', 'default-value', ['class'=>'class-name','readonly']) }}

Try this...
{{ Form::text('login_token', Worker::generateLoginToken(),array('readonly')) }}

I am using Laravel 5.4 along with BootForm, and the only way that it has worked was by doing:
{!! BootForm::text('Name', 'name', $name)->disable() !!}
Based on the docs of adamwathan/form.
Hope it helps!

Related

How to set autofocus on Laravel input field using Form::text whilst still setting the class

I can't seem to set autofocus on a input field in Laravel 5.4, whilst also setting the class of the element.
What I've tried:
{{ Form::text('email', Input::old('email'), ['class'=>'field-Login'], array('autofocus'=>'autofocus'))}}
{{ Form::text('email', Input::old('email'), array('autofocus'=>'autofocus'), ['class'=>'field-Login'])}}
{{ Form::text('email', Input::old('email'), array('autofocus'=>'autofocus',['class'=>'field-Login']))}}
You need to combine your attributes. For example:
echo Form::text('email', 'default#value.com', ['class' => 'class123', 'autofocus']);

Check if Form method is post or patch in Laravel

I have an edit view and i am using a partial _form view.
Is there a way to check if the form is a patch or post?
What i plan to do is to change the hidden field in edit form
#if (form is post)
{!! Form::hidden('signature') !!}
#else
<div class="form-group">
{!! Form::label('signature', 'Signature: ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('signature', null, ['class' => 'col-md-2 form-control', 'required']) !!}
</div>
</div>
#endif
because this variable is already saved to DB and i want to load it for edit.
Or to check if form is post, that would work also!
I usually pass the variable to a view where I set action, like:
$action = 'store';
Then I use this variable to build route name:
{!! Form::open(['route' => 'post'.$action, ....
And detect what type of action is needed:
#if ($action == 'store')
I guess it's the most readable and simple way to achieve what you're trying to achieve. You can do something similar.
Try this:
$isPut= Request::isMethod('put');
if($isPut) {
//
}

Laravel deleting a file NotFoundHttpException

this is my form in my view
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
controller in which i delete file from database and the original file
public function destroyFile($file_name, $id)
{
File::findOrFail($id)->delete();
$file_path = storage_path('documents').'/'.$file_name;
$destinationPath = $file_path; File::delete($file_path);
return redirect('/documents');
}
This is the route
Route::delete('documents/{file}/{id}','FilesController#destroyFile');
And when i press submit button I get NotFoundHttpException
Try to use this
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name, $file->id] ]) !!}
Actually, their answers are correct. You need the _method to be DELETE. When I am using this. Laravel do it for me.
Or you can put this on your form
<input type="hidden" name="_method" value="DELETE">
or
{!! Form::hidden('_method', 'DELETE') !!}
It is not possible to use this method with html forms in most browsers, most only support GET and POST.
So the reason for this request not working is because the browser sends this as a GET request, wich is the default.
GET, POST, PUT and DELETE are however supported in most major browsers when using XMLHttpRequests (ajax).
add {{ method_field('DELETE') }} to your form .
{!! Form::open(['url' => ['documents/{file}/{id}', $file->name, $file->id],'method' => 'delete']) !!}
{{ method_field('DELETE') }}
{!! Form::token() !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
The reason is that HTML forms does not support PUT, PATCH, DELETE actions. Basically you need to spoof them as described here. https://laravel.com/docs/5.2/routing#form-method-spoofing

Conversion of html textarea to laravel 4.2 textarea

Hi im using ckeditor so i currently have the html version which is
<textarea class="ckeditor" name="editor" id = "stuff">
{{ $opendoc }}
</textarea>
the $opendoc came from my controller. it contains txt in my controller it looks like this
$fname = $file->filename;
$opendoc = file_get_contents(public_path('uploads/docs/' . $fname));
return View::make('dmy.open_doc' , compact('title', 'smpl' , 'opendoc'));
im trying to display the data using laravel 4.2 textarea
currently im using something like this
{{ Form::textarea('open_file', $opendoc , array('class' => 'ckeditor')) }}
but the value of $opendoccould not be viewed. any ideas what im doing wrong? thanks in advance
It just shows the textarea but without any contents - check whether that variable is set or not.
{!! Form::textarea('open_file', isset($opendoc)? $opendoc:null, array('class' => 'ckeditor','size' => '10x3')) !!}
Use the Form::textarea() method.
The simplest usage is to only pass a single argument, the name.
{{ Form::textarea('notes') }}
This produces the following HTML.
<textarea name="notes" cols="50" rows="10"></textarea>
Notice the default cols and rows.
You can pass the value as the second argument.
{{ Form::textarea('notes', '3 < 4') }}
The value will be escaped.
<textarea name="notes" cols="50" rows="10">3 < 4</textarea>
Additional options can be passed as a third argument. This must be an array.
{{ Form::textarea('notes', null, ['class' => 'field']) }}
This will add the class "field" to the text area.
Your solution
{!! Form::textarea('open_file', isset($opendoc)? $opendoc:null, array('class' => 'ckeditor','size' => '10x3')) !!}

Laravel 5 - inserting old data into form via variable

I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project, $document->id]
]) !!}
{!! $documentData->documentData !!}
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
$documentData->documentData contains the html code for this particular form.
Now my problem is, $documentData->form_data contains the old inputs for this form.
Is there any way to get this old input into the form, the way I am currently handling things?
Thanks
in controller you can access old input by $request->flash(); while in frontend you can access old by input type="text" name="name" value="{{ $name }}"

Categories