Barebone\Form

<?php

$form = new Barebone\Form();
	    
$form->is_ajax();
if($form->is_send() && !$form->is_valid()){
    $this->ShowZebraDialog('Error', 'Your form has an invalid seckey', 'error');
}

if($form->is_send() && $form->is_valid()){
    $this->ShowModal('You send this data:', '<pre>'.print_r($_POST, true).'</pre>');
}
$form->secure_form();

$form->addInput('text', 'ID', 'id', ['disabled' => 'disabled', 'class' => 'form-control']);
$form->addInput('text', 'Name', 'name', ['class' => 'form-control']);
$form->addInput('text', 'State', 'state', ['class' => 'form-control']);

$options = [];
$options[0] = 'No choice made';
$options[1] = '1 choice made';
$options[2] = '2 choice made';
$form->addSelect(
    label: 'Choices', 
    name:'choices', 
    attributes: ['class' => 'form-control'], 
    options: $options
);
$form->addButton('submit', 'Save', ['class'=>'btn btn-success', 'style'=>'margin-top: 20px;margin-left: 15px;']);
$this->view->form = $form;
	
<form role="form" method="POST" action="" onsubmit="$.php( $(this).attr('action'), $(this).serialize() ); return false;">
<input type="hidden" name="CSFR" value="6aa1176aee0ac" />
<div class="form-group">
<label class="col-sm-4" for="id">
ID</label>
<div class="col-sm-8">
<input type="text" name="id" id="id" disabled="disabled" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4" for="name">
Name</label>
<div class="col-sm-8">
<input type="text" name="name" id="name" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4" for="state">
State</label>
<div class="col-sm-8">
<input type="text" name="state" id="state" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4">
Choices</label>
<div class="col-sm-8">
<select name="choices" class="form-control">
<option value="No choice made">
No choice made</option>
<option value="1 choice made">
1 choice made</option>
<option value="2 choice made">
2 choice made</option>
</select>
</div>
</div>
<input type="submit" value="Save" class="btn btn-success" style="margin-top: 20px;margin-left: 15px;" />
</form>

1. Form Creation and Configuration:

  • $form = new Barebone\Form();: Creates a new form object using a framework or library called "Barebone."
  • $form->is_ajax();: Sets a flag indicating that the form is designed for AJAX submission (asynchronous JavaScript submission).

2. Form Validation and Security:

  • if($form->is_send() && !$form->is_valid()){ ... }: Checks if the form has been submitted and if it's valid. If not valid, displays an error dialog using a custom function ShowZebraDialog.
  • if($form->is_send() && $form->is_valid()){ ... }: If the form is submitted and valid, displays a modal dialog using a custom function ShowModal, showing the submitted data.
  • $form->secure_form();: Activates a security mechanism within the Barebone framework, likely a hidden field or token for cross-site request forgery (CSRF) protection.

3. Form Input Fields:

  • $form->addInput('text', 'ID', 'id', ['disabled' => 'disabled']);: Adds a text input field with the label "ID", name "id", and disabled state (uneditable).
  • $form->addInput('text', 'Name', 'name', []);: Adds a text input field labeled "Name" with the name "name."
  • $form->addInput('text', 'State', 'state', []);: Adds a text input field labeled "State" with the name "state."
  • $form->addSelect(...);: Adds a select dropdown with:
    • Label: "Choices"
    • Name: "choices"
    • Options: "No choice made", "1 choice made", "2 choice made"

4. Submit Button:

  • $form->addButton('submit', 'Save');: Adds a submit button labeled "Save."

5. Pass Form to View:

  • $this->view->form = $form;: Assigns the created form object to a variable named "form" in a view object, likely for rendering the form in a template.

Key Points:

  • The code uses a framework for form handling, likely providing additional features and security measures.
  • It handles AJAX submissions and form validation.
  • It includes basic CSRF protection.
  • It creates a visually simple form with a few text fields, a dropdown, and a submit button.
  • Barebone\Form

    Barebone\Form extends Barebone\Component
    • [P] $form : Element
    • [M] __construct( $attributes ) : void
    • [M] secure_form() : void
    • [M] panelize( Barebone\Form $form, string $title, string $footer ) : Barebone\Form\Element
    • [M] create_csfr_token() : void
    • [M] is_valid_csfr() : bool
    • [M] is_send() : bool
    • [M] is_valid() : bool
    • [M] is_ajax() : void
    • [M] is_multipart() : void
    • [M] flash( string $message, string $type ) : void
    • [M] add( Barebone\Form\Element $element ) : void
    • [M] addButton( $type, $value, $attributes ) : void
    • [M] addInput( $type, $label, $name, $attributes ) : void
    • [M] addRadioGroup( $label, $name, $options, $attributes ) : void
    • [M] addTextarea( $label, $name, $attributes ) : void
    • [M] addCheckbox( $label, $name, $value, $attributes ) : void
    • [M] addHidden( $name, $value, $attributes ) : void
    • [M] addSelect( string $name, string $label, array $attributes, array $options ) : void
    • [M] get_input_type_from_db_type( $propertie ) : void
    • [M] get_json() : void
    • [M] get_html() : void
    • [M] render() : void
    • [M] get_csrf() : void
    • Barebone\Component

      Barebone\Component
      • [P] $properties : array
      • [M] __construct( mixed $data ) : void
      • [M] setData( mixed $data ) : void
      • [M] toArray() : array
      • [M] debug( mixed $data, bool $vardump, $cls_pre ) : void
      • [M] __set( string $name, mixed $value ) : void
      • [M] __get( string $name ) : void
      • [M] __isset( string $name ) : void
      • [M] sanitize_array( array $data ) : void