Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Creating Fields

Felice Ostuni edited this page May 15, 2015 · 13 revisions

Creating Fields

To use your custom field on DataForm/DataEdit you need to specify the class namespace, i.e.:

$form->add('hexcolor','Color','App\Yourname\Fields\Colorpicker');

This class:

  • must extend \Zofe\Rapyd\DataForm\Field\Field
  • this class must have at least a property "type" for example $type = 'colorpicker';
  • in this class you should override build() method to fill $this->output based on field 'status' (field status is a property that dataform share with fields, it can be: "show,create,modify, hidden")
  • you can override (if needed): getValue() and getNewValue() to format stored value or process user input.

A bit of words for getValue() getNewValue():

  • the first is used to fill $this->value and the second to fill $this->new_value
  • by default $this->value is filled by the value stored in the db (if any)
  • by default $this->new_value will be the new value to save/process (from user input)

So, wen you need to "reformat" user input you can overwrite getNewValue(), instead if you need to format db value to show a human readable value you can overwrite getValue().

Most of time if field is "simple" (doesn't require transformations) you need only to override build(). Like this one: https://github.com/zofe/rapyd-laravel/blob/master/src/DataForm/Field/Redactor.php

If you don't know how to setup a custom Namespace in Laravel this is a mini how-to:

Custom Namespace in Laravel for managing fields

  • make a \app\Yourname folder
  • make a \app\Yourname\Field folder

in this last folder put your fields:

<?php namespace App\Yourname\Field;

use Illuminate\Support\Facades\Form;
use Zofe\Rapyd\Rapyd;
use Zofe\Rapyd\DataForm\Field\Field;

class Myfield extends Field {
....
  • then you must regenerate autoload by
composer dump-autoload
php artisan dump-autoload
  • then you should be able to use your own fields:
$form->add('customfield','Asdasd','App\Yourname\Field\Myfield');

obviously if you need to include js/css assets you can put it on a /public/yourname/ folder then referencing in your field using:

Rapyd::js('yourname/myfield.min.js');
Rapyd::css('yourname/myfield.css');

NOTE for L4:

if you're on Laravel 4 use a different namespace: namespace Yourname\Field and add a psr-0 declaration in your composer.json :

"autoload": {
    "psr-0": {
           "Yourname": "app/"
    }
},

then a use the correct namespace for L4

$form->add('customfield','Asdasd','Yourname\Field\Myfield');
Clone this wiki locally