PHP code example of andersondanilo / modelform

1. Go to this page and download the library: Download andersondanilo/modelform 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/ */

    

andersondanilo / modelform example snippets


// SimpleForm.php
use ModelForm\Form;
use ModelForm\Fields\CharField;
use ModelForm\Fields\IntegerField;

class SimpleForm extends Form
{
    public function makeFields()
    {
        $this->name = new CharField(['label' => 'Name']);
        $this->age = new IntegerField(['label' => 'Age']);
    }
}

$simpleForm = new SimpleForm(['data' => Input::old() ?: Input::all()]);

    {{ $simpleForm->name->label() }}:
    {{ $simpleForm->name->text(['class' => 'form-control']) }}

    $name = $simpleForm->name->value;

use ModelForm\Form;
use ModelForm\Fields\CharField;
use ModelForm\Fields\IntegerField;

class SimpleForm extends Form
{
    public function makeFields()
    {
        $this->name = new CharField(['label' => 'Name']);
        $this->age = new IntegerField(['label' => 'Age']);
    }
    
    public function makeModel()
    {
        return new MyModel();
    }
    
    public function makeValidator($data)
    {
        return Validator::make($data, [
            'name' => '

$model10 = MyModel::find(10);
$form = new SimpleForm(['model' => $model10, 'data' => Input::old() ?: Input::all()]);

if(!$simpleForm->isValid()) {
    return Redirect::back()->withErrors($simpleForm->errors())->withInput();
}

$simpleForm->save();

use ModelForm\FormSet;

class SimpleFormSet extends FormSet
{
    public function makeForm($model=null)
    {
        return new SimpleForm(['model'=>$model]);
    }
}

$simpleFormSet = new SimpleFormSet(['data' => Input::old() ?: Input::all());

$addressFormSet = new AddressFormSet(['relation'=>$customer->addresses(), 'data' => Input::old() ?: Input::all());

if(!$addressFormSet->isValid()) {
    return Redirect::back()->withErrors($addressFormSet->errors())->withInput();
}
$addressFormSet->save();