1. Go to this page and download the library: Download mvanvu/php-form library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mvanvu / php-form example snippets
use MaiVu\Php\Form\Form;
$fieldsData = [/*See tests/index.php to know how to create the fields data*/];
$form = new Form($fieldsData);
// Default template is Bootstrap (the both v3 and v4 are working)
echo $form->renderFields();
// Render Uikit 3 template
echo $form->renderTemplate('uikit-3');
// Or set default template
Form::setTemplate('uikit-3');
echo $form->renderFields();
// Render horizontal
echo $form->renderHorizontal();
// Validate form
if ($form->isValidRequest()) // The same $forms->isValid($_REQUEST)
{
echo 'Cool! insert the valid data to the database';
$data = $form->getData(); // Instance of Registry
$validData = $data->toArray();
var_dump($validData);
}
else
{
echo 'Oops. The form is invalid:<br/>' . implode('<br/>', $form->getMessages());
}
use MaiVu\Php\Form\Form;
use MaiVu\Php\Form\FormsManager;
$fieldsData1 = [/*See tests/index.php to know how to create the fields data*/];
$fieldsData2 = [/*See tests/index.php to know how to create the fields data*/];
$form1 = new Form($fieldsData1);
$form2 = new Form($fieldsData2);
$forms = new FormsManager([$form1, $form2]);
// OR
// $forms = new FormsManager;
// $forms->add($form1)->add($form2);
echo $forms->renderFormFields(0);
echo $forms->renderFormFields(1);
// echo $forms->renderHorizontal(0);
// echo $forms->renderHorizontal(1);
// OR set name for the form
// $forms->set('form1', $form1);
// $forms->set('form2', $form2);
// echo $forms->renderFormFields('form1');
// Validate form
if ($forms->isValidRequest()) // The same $forms->isValid($_REQUEST)
{
echo 'Cool! insert the valid data to the database';
$validData = $forms->getData(true); // Get data as an array instead Registry
var_dump($validData);
}
else
{
echo 'Oops. The form is invalid:<br/>' . implode('<br/>', $forms->getMessages());
}
use MaiVu\Php\Form\Form;
$fieldsData = [
[
'name' => 'text',
'type' => 'text',
'value' => null,
],
];
$form = new Form('myForm', $fieldsData);
$form->bind(
[
'myForm' => [
'text' => 'The text value',
],
]
);
echo $form->getField('text');
// Will render with the form name myForm[text]: <input name="myForm[text]" type="text" value="The text value"/>
// Form deep name
$form = new Form('myForm.params', $fieldsData);
$form->bind(
[
'myForm' => [
'params' => [
'text' => 'The text value',
],
],
]
);
echo $form->getField('text');
// <input name="myForm[params][text]" type="text" value="The text value"/>
$password1 = [/** Password1 config data */];
$password2 = [
'name' => 'pass2',
'type' => 'Password',
'label' => 'Confirm password',
'class' => 'form-control',
' 'Confirm:pass1|4567[when:1234]' => 'Please, when this is 1234 then the Password must be: 4567',
],
];
// Just use the Email type
$email = [
'name' => 'Email',
'type' => 'Email',
'label' => 'My Email',
'messages' => [
'Email' => 'Invalid email.'
],
];
// OR set its rules contain Email: 'rules' => ['Email']
// Invalid if the value is not in the options attributes
$select = [
'name' => 'MyField',
'type' => 'Select',
'label' => 'My Field',
'options' => [
[
'value' => '1',
'text' => 'Yes',
],
[
'value' => '0',
'text' => 'No',
],
],
'rules' => ['Options'],
'messages' => [
'Options' => 'The value not found.', // Invalid if the value is not (1 or 0)
],
];
$regex = [
'name' => 'MyField',
'type' => 'TextArea',
'label' => 'My Field',
'rules' => [
'Regex:^[0-9]+$' => 'The value must be an unsigned number',
],
];
use MaiVu\Php\Form\Form;
Form::addFieldNamespaces('Your\\Custom\\MyNS');
Form::addRuleNamespaces('Your\\Custom\\MyNS');
namespace Your\Custom\MyNS;
use MaiVu\Php\Form\Field;
class MyCustomField extends Field
{
public function toString()
{
return '<p>Hello World!</p>'; // Return input field
}
}
// Usage: type => 'MyCustomField'
namespace Your\Custom\MyNS;
use MaiVu\Php\Form\Rule;
use MaiVu\Php\Form\Field;
class MyCustomRule extends Rule
{
// Php validator
public function validate(Field $field) : bool
{
return $field->getValue() === '1'; // Value = 1 is valid or not
}
}
// Usage: rules => ['MyCustomRule']
json
{
"vu/php-form": "~1.0"
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.