callbackAddField

callbackAddField(string $fieldName, callable $callback)

Create a custom field with a callback for add form. As this is add form, there is not any parameters added to the callback. You can return any custom HTML you wish.

Example

$crud->callbackAddField('contact_last_name', function ($fieldType, $fieldName) {
    /** $fieldType will look like this:
     GroceryCrud\Core\Model\ModelFieldType Object
    (
        [isNullable] => 
        [dataType] => varchar
        [defaultValue] => 
        [permittedValues] => 
        [options] => stdClass Object
            (
                [maxLength] => 50
            )
        [isRequired] => 
        [isReadOnly] => 
        [isSearchable] => 1
    )*/

    return '<input class="form-control" name="' . $fieldName . '" type="text" value="">';
});

Have in mind that from PHP 5.4 and later there is an extra functionality with keywork use, so that means that you can pass any extra parameters at the callback from outside the callback if you wish it. For example:


$username = 'john';
$crud->callbackAddField('contact_telephone_number', function () use ($username) {
     // You have access now at the extra custom variable $username
    return '+30 <input name="telephone_number"  /> for: ' . $username ;
});

Notice: Grocery CRUD Enterprise is all about security, so we are obliged to inform you that the callbackAddField is using the: dangerouslySetInnerHTML function of reactJS. As you can understand from the name, this may be very dangerous if it is not used properly. We are suggesting to use it mainly in admin environments and make sure that you are always filtering your data at the callback. This was the only way that we could enable the functionality of callbackAddField and you have to be aware of this warning before using it.