Laravel-admin: How to show readonly form on edit state?

Created on 10 Jun 2018  ·  5Comments  ·  Source: z-song/laravel-admin

  • Laravel Version: 5.5.*
  • PHP Version: 7.1
  • Laravel-admin: 1.5.*

Description:

How to show readonly form value on edit state

Most helpful comment

Ok, so I spoke too soon. Romerski method did not work for me, it was just my cached code that was running. After clearing code cache and debug with var_dump, I find mode is always 'create' in the output. I follow through the code and found why isMode('edit') does not work.

Let say your controller render the code like so: https://github.com/z-song/laravel-admin.org/blob/master/app/Admin/Controllers/CategoryController.php#L42

$content->body($this->form()->edit($id));

So why doesn't it work when ->edit($id) set builder mode to 'edit'? Well, before calling the ->edit($id) method, it calls the $this->form() method to generate the form. Well, at this point, the code get executed right away instead of waiting until toString() call; so of course the mode will still be 'create'.

After researching, I stumble upon issue #1631 and #1348. Solution for issue #1631 of checking model()->id did not work since model is also empty at the current state (as it is still before edit call). My original hack was based of issue #1348 which works, but I didn't like it. Decided to go with this:

$isEdit = (substr(trim(request()->path(), '/'), -5) === '/edit') || !request()->isMethod('get');

if ($isEdit) {
   // do stuff here
}

All 5 comments

Try:

if ($form->builder()->isMode('edit'))
{
    $form->yourInput()->attribute(['readOnly'=>'true'])
}

@Romerski Not Work,
Here is my code

if ($form->builder()->isMode('edit')) {       
    // $form->text('username', trans('admin.username'))->attribute(['readOnly'=>'true']);    
    $form->username()->attribute(['readOnly'=>'true']);    
} else {       
     $form->text('username', trans('admin.username'))->rules('required');      
}

For me works good
->attribute(['readonly' => 'true'])
or
->attribute(['disabled' => 'true'])

I'm using 1.5.x-dev

Also using 1.5.x and @Romerski method works for me to. Learned something today. Previously I hack my way around by passing in a parameter when calling form method. Thanks.

Ok, so I spoke too soon. Romerski method did not work for me, it was just my cached code that was running. After clearing code cache and debug with var_dump, I find mode is always 'create' in the output. I follow through the code and found why isMode('edit') does not work.

Let say your controller render the code like so: https://github.com/z-song/laravel-admin.org/blob/master/app/Admin/Controllers/CategoryController.php#L42

$content->body($this->form()->edit($id));

So why doesn't it work when ->edit($id) set builder mode to 'edit'? Well, before calling the ->edit($id) method, it calls the $this->form() method to generate the form. Well, at this point, the code get executed right away instead of waiting until toString() call; so of course the mode will still be 'create'.

After researching, I stumble upon issue #1631 and #1348. Solution for issue #1631 of checking model()->id did not work since model is also empty at the current state (as it is still before edit call). My original hack was based of issue #1348 which works, but I didn't like it. Decided to go with this:

$isEdit = (substr(trim(request()->path(), '/'), -5) === '/edit') || !request()->isMethod('get');

if ($isEdit) {
   // do stuff here
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

cdhraesaemer picture cdhraesaemer  ·  3Comments

donglianyou picture donglianyou  ·  3Comments

chenyongmin picture chenyongmin  ·  3Comments

piian picture piian  ·  3Comments

MarKco picture MarKco  ·  3Comments