Feedback Laraform is now in maintenance mode. Read more on our GitHub.

Extending Core

#Extending Backend

Laraform does provide certain features described in the documentation, but it's pretty easy to implement custom features or overwrite existing ones.

If you want to extend functionality on the backend the best way to do that is to create an app\Forms\Laraform.php base form instance for your project and extend everything from that later.

Let's see an example where we need dynamic $languages options for example. The $languages property is a property and therefore cannot be set dyniamically. However you can overwrite getLanguages method and you can implement a custom solution:

<?php

// app\Forms\Laraform.php

namespace App\Forms;

use DB;

class Laraform extends \Laraform
{
  public function getLanguages() {
    return DB::table('languages')->get()->mapWithKeys(function($language){
      return [
        $language->code => [
          'label' => $language->label,
          'code' => $language->code,
        ];
      ]
    })
  }
}

Now if you extend this Laraform class instead of \Laraform languages will come from database dynamically. Eg. let's create an app/Forms/PostForm.php:

<?php

// app/Forms/PostForm.php

namespace App\Forms;

// Extending `Laraform` from `App\Forms` namespace
class PostForm extends Laraform
{
  public function schema() {
    // ...
  }
}

You can check out Laraform Backend for available properties and method or go directly to vendor/laraform/laraform-laravel/src/Laraform.php.

#Extending Frontend

The same is true for extending Laraform on the frontend. You might create a base resources/js/components/form/Laraform.vue component which you can later use as a mixin in your forms.

Let's say we want to handle fail event every time the same way within each of our form. Then we can create this base Laraform component, where we subscribe for fail event and we'll have a default solution:

// resources/js/components/forms/Laraform.vue

<script>
  export default {
    mixins: [Laraform],
    methods: {
      handleFail(response) {
        alert(response.messages[0])
      }
    },
    mounted() {
      this.on('fail', this.handleFail)
    }
  }
</script>

Now let's just simply use this component as a default mixin for each of our forms to have this handleFail method that is being called on fail event:

// eg. resources/js/components/forms/PostForm.vue

<script>
  import Laraform from './Laraform'

  export default {
    mixins: [Laraform],
    // ...
  }
</script>

If you need custom handling of fail event you can simply overwrite handleFail method at any form instance.