Forms
Never been easier to build forms then this really well builed framework. Example bellow is how to use it in Control Tower and not outside.
Class meta
- Name: fields
- Namespace: form\builder\fields
- Construct:
- (String) fieldTemplate: class string to method input fields (required)
- (String) formTemplate: class string to method complete form/fields template
- (Array) passToInst: Pass classes/objects/array to nested methods above
- Dependencies: form\builder\arguments
Usage
Quick
echo $this->modal()->fields()->text()->name("email")->attr(["type" => "email"])->get();
Advanced: Create the form
First create the form with a structured array
$this->modal()->fields()->add("userForm", [ // Set form name "firstname" => [ "type" => "text", // Set form type (input text or textarea and so on.) "label" => "First name", "validate" => [ "length" => [1, 80] ] ], "lastname" => [ "type" => "text", "label" => "Last name", "validate" => [ "length" => [1, 120] ] ], "email" => [ "type" => "text", "label" => "Email", "attr" => [ "type" => "email", "placeholder" => "john.doe@hotmail.com" ], "validate" => [ "length" => [1, 120] "!email" => NULL ] ], "upload,portait" => [ "type" => "file", "label" => "Portrait", "description" => "Open media library and chooes a portait image or upload a new one.", "config" => [ "type" => 1, // 1: Keep aspect ratio, 2: Auto crop image, 3: Trim image "webp" => 1, // Auto convert to modern file format "accept" => "image", // Can be comma seperated: image,svg,video,file //"hex" => "#FFFFFF", // Set BG if transparent, #FFFFFF for white or "auto" to get prominent color from image and set as BG "images" => [ [ "width" => 640, "height" => 640, "srcset" => "1x" ], [ "width" => 1280, "height" => 1280, "srcset" => "2x" // 1x, 2x or max-width: 640px ] ] ] ], ]); $this->modal()->fields()->add("advanced", [ // Set form name "status" => [ "type" => "radio", "label" => "Synlig", "validate" => [ "length" => [1, 1], "number" => NULL, "!min" => 0, "!max" => 1 ], "items" => [ 1 => "Ja", 0 => "Nej" ] ] ]);
Build the form
Build the all the form. This means that all the forms values will be inserted and also validated as one unit, so use uniqe input/field name accross all forms within the instance.
// Set form values $this->modal()->fields()->setValues([ "firstname" => "Daniel", "lastname" => "Doe", "email" => "daniel@creativearmy.se", "upload" => [ "portrait" => "[JSON_STRING_FROM_WE_MEDIA]" ], "status" => 1, ]); // Build form $this->modal()->fields()->build();
Read the form
Read/return the form (html-code) in the body
<div id="the-form"> <?php echo $this->modal()->fields()->form("userForm"); ?> </div>
Validate and save
Validate the form data then save it in the database. If success then return all and "only" form fields that you have in "userForm/advanced". If fields has been maliciously injected then these fields will be filtered out. This makes your life much easier when working with dynamic and nested form fields/arrays.
$validate = new \form\validate($this->modal()->fields(), $_POST); if($arr = $validate->respect()) { // Validate failed $this->modal()->json()->add("status", 3); $this->modal()->json()->add("form_error", $arr); } else { // Validate success // This method will filter in only used fields $post = $this->modal()->request()->array($validate->request())->val(); $insert = DB::_insert($post, "DB_TABLE"); $insert->execute(); $this->modal()->json()->add("status", 1); $this->modal()->json()->add("message", "Uppdated"); }
Field types
Input text
@return string/html
public text()
Input hidden
@return string/html
public hidden()
Input date
@return string/html
public date()
Input date time
@return string/html
public datetime()
Input text in list form
@return string/html
public list()
Textarea
@return string/html
public textarea()
Textarea with counter
@return string/html
public maxLength()
File/image field with library
@return string/html
public file()
Image tooltip field
@return string/html
public tooltip()
Select list / drop list
@return string/html
public select(?callable $callback = null)
Position
@return string/html
public position()
Select list multiple
@return string/html
public multiple($addHeader = false)
Radio button
@return string/html
public radio()
Checkbox
@return string/html
public checkbox()
Group fields
@return string/html
public group()