Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a form from XML in Laravel

Tags:

php

laravel-4

Update:
I know how to parse XML, but not exactly where in the architecture, see problems defined below.
All suggestions are welcome!


Fighting my way into Laravel, I try to build a form from a XML file.

Problems:

  1. how to get the data from the XML into the view?
  2. where to build that form - in terms of repetition, I'd prefer to create the form once, and use it for create, edit and view
  3. validation - I'd like to reuse it as much as possible

the XML: foods_form.xml (simplified):

<form>
    <field id="1" name="cheese" label="favorite cheese?" type="radio" req="1" filter="int">
        <option id="1" value="1">Camembert</option>
        <option id="2" value="3">Gouda</option>
    </field>
    <field id="2" name="beer" label="favorite beer?" type="text" req="1" filter="str" />
</form>

the view: app/views/create.blade.php:

@extends('layout')
@section('content')

<form action="{{ action('FormsController@handleCreate') }}" method="post" role="form">

    @foreach ($fields as $field)
        <label for="{{ $field->name }}">{{ $field->label }}</label>

        @if ($field->type == 'text')
            <input type="text" name="{{ $field->name }}" />
        @else
            @foreach ($field->option as $option)
                <input type="radio" name="{{ $field->name }}" value="{{ $option }}" />
            @endforeach
        @endif
    @endforeach    

    <input type="submit" value="Create" />
    <a href="{{ action('FormsController@index') }}">Cancel</a>
</form>
@stop

the controller: app/controllers/FormsController.php:

class TestsController extends BaseController {

    public function index() {
        // return some view
    }

    public function create() {
        return View::make('create');
    }

    public function handleCreate() {
        // validation according to XML
        // save to database if valid || return to form if not valid
    }
}
like image 237
michi Avatar asked Jan 21 '14 23:01

michi


2 Answers

Laravel won't be helping you creating forms from some XML.

You'll need to parse your XML with a library like SimpleXML : you'll find some documentation here, on php.net

Begin with creating a SimpleXMLElement :

$xml = new SimpleXMLElement('../path/to/your/file/foods_form.xml', 0, true);

You can now use your $xml object to generate your form, respecting the format of your XML (dump your $xml object to have an idea of the structure)

Simply put your object in your view to use it directly.

To validate your form, you can use Validation from Laravel : link to the doc

like image 144
netvision73 Avatar answered Nov 13 '22 09:11

netvision73


I have yet to find an XSL friendly MVC.

The way I do it (Huge fan of XSL in views)

Final Page will have 3 modules lets say:

My view will only contain

// I don't know how you simply echo a variable from "yet another framework"
// but this is the only thing you need to do from the view
echo $someModule;
echo $otherModule;
echo $lastModule;

My controller will have 3 extra dependencies injected to contain whatever logic I got to execute. And use the simplest class to apply my xsl

<?php
class SomeController extends SomeMvcController {

    private $someModuleLogic;
    private $otherModuleLogic;
    private $lastModuleLogic;
    private $xslTransformer;


    public function __construct( XslTransformer $xslTransformer, $someModuleLogic, $otherModuleLogic, $lastModuleLogic ) {
        $this->someModuleLogic  = $someModuleLogic;
        $this->otherModuleLogic = $otherModuleLogic;
        $this->lastModuleLogic  = $lastModuleLogic;

        parent::__construct();
        $this->xslTransformer = $xslTransformer;
    }


    public function someAction() {

        /**
         * doStuff functions will take your parameters like get, post etc and return a DomDocument object
         * which can be programmatically calculated via PHP or generated by reading an XML file (or xml from any buffer)
         */
        $someModule  = $this->xslTransformer->transform(
            'myViews/modules/someModule.xsl',
            $this->someModeuleLogic->doStuff()
    );
        $otherModule = $this->xslTransformer->transform(
            'myViews/modules/otherModule.xsl',
            $this->otherModeuleLogic->doStuff()
    );
        $lastModule  = $this->xslTransformer->transform(
            'myViews/modules/lastModule.xsl',
            $this->lastModeuleLogic->doStuff()
    );        }
}



class XslTransformer {

    public function transform( $xslLocation, DOMDocument $domDocument ) {
        $xslDocument = new DOMDocument();
        $xslDocument->load( $xslLocation );

        $xsltProcessor = new XSLTProcessor();
        $xsltProcessor->importStylesheet( $xslDocument );

        $document                     = $xsltProcessor->transformToDoc( $domDocument );
        $document->encoding           = 'UTF-8';
        $document->formatOutput       = true;
        $document->preserveWhiteSpace = false;

        return $document;
    }
}

This keeps my views/controllers very small and simple without any logic. Everything is done in the injected class and I can chop that one into tiny simple pieces.

like image 29
kali Avatar answered Nov 13 '22 11:11

kali