Hide forms in Laravel 5.5 only visually

1

I am doing a project in Laravel 5.5. I have the following form to edit a post:

The idea is that the only forms that the user can see are the notes and the description. The rest of the other fields will already have a value in the database so that the user, as he can not see them, can not modify them. I read that there is a way to do it with css, since I only want to hide it visually.

Here I put the code of the form:

{{ Form::hidden('user_id', auth()->user()->id) }}

<div class="form-group">
{{ Form::label('category_id', 'Categorias') }}
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
</div>

<div class="form-group">
{{ Form::label('name', 'Nombre de la etiqueta') }}
{{ Form::text('name', null, ['class' => 'form-control', 'id' => 'name']) }}
</div>
<div class="form-group">
{{ Form::label('slug', 'URL amigable') }}
{{ Form::text('slug', null, ['class' => 'form-control', 'id' => 'slug']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Imagen') }}
{{ Form::file('file') }}
<div class="form-group">
{{ Form::label('file2', 'Notas') }}
{{ Form::file('file2') }}   
</div>
<div class="form-group">
{{ Form::label('status', 'Estado') }}
<label >
    {{ Form::radio('status', 'PUBLISHED') }} Publicado
</label>
<label >
    {{ Form::radio('status', 'DRAFT') }} Borrador
</label>
</div>
<div class="form-group">
{{ Form::label('tags', 'Etiquetas') }}
<div>
    @foreach($tags as $tag)
    <label>
        {{ Form::checkbox('tags[]', $tag->id) }} {{ $tag->name }}
    </label>
    @endforeach
</div>
</div>
<div class="form-group">
{{ Form::label('excerpt', 'Extracto') }}
{{ Form::textarea('excerpt', null, ['class' => 'form-control', 'rows' => '2']) }}
</div>
<div class="form-group">
{{ Form::label('body', 'Descripcion') }}
{{ Form::textarea('body', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">{{ Form::submit('Guardar', ['class' => 'btn btn-sm btn-primary']) }}</div>

@section('scripts')
<script src="{{ asset('vendor/stringToSlug/jquery.stringToSlug.min.js') }}"></script>
<script src="{{ asset('vendor/stringToSlug/jquery.stringToSlug.js') }}"></script>
<script src="{{ asset('vendor/ckeditor/ckeditor.js') }}"></script>
<script>
$(document).ready(function(){
$("#name, #slug").stringToSlug({
        callback: function(text){
            $('#slug').val(text);
        }
    });
});

CKEDITOR.config.height = 400;
CKEDITOR.config.width  = 'auto';

CKEDITOR.replace('body');
</script>
@endsection

EDIT1: Users are registered in the application.

When an administrator creates or edits a post, he can use all the fields. There is only one administrator, I differentiate it since it has a value in its id of 1, the rest users with id values other than 1 are simple users.

When a user creates a post, he can use all the fields. When a user edits a post, he can only edit the notes field and the description field, so that the other fields retain the values that were placed when the post was created.

    
asked by Kinafune 16.04.2018 в 05:06
source

1 answer

1

First way:

The simplest way to do this with CSS is to include the following line in each of the parts of your form that do not want to be shown to users:

<div class="form-group {{ Auth::user()->id != 1 ? 'hidden' : '' }} ">
    // Code
</div>

With hidden the following CSS tag:

<style>
    .hidden { display: none; }
</style>

Second form:

Another way to do it without using CSS would be to use in each part that you do not want your users to see and only the tag for laravel blade @if is accessible to the administrator, the next way:

@if(Auth::user()->id == 1)
    <div class="form-group">
        // Code
    </div>
@endif

Third form:

It would be the one I would use, so you do not have to add so much code. I suppose that in your main view you have a @yield('styles') , which allows you to add specific styles in each of your views. In this way, in the view that concerns us we would add:

@section('styles')
    @if(Auth::user()->id != 1)
        <style>
            .hidden { display: none; }
        </style>
    @endif
@endsection

// ...

<div class="form-group hidden">
    // Code
</div>

// ...

This form mixes the previous two, but using less code. We would have to add the hidden tag to the fields that you do not want the users to see. This way, if someone other than the administrator loads the view, the hidden tag is activated and hidden.

    
answered by 17.04.2018 / 10:00
source