如何在Laravel中使用XML构建表单

mic*_*chi 5 php laravel-4

更新:
我知道如何解析XML,但不知道在架构中的确切位置,请参阅下面定义的问题.
欢迎所有建议!


为了进入Laravel,我尝试从XML文件构建表单.

问题:

  1. 如何将XML中的数据导入视图?
  2. 在哪里构建该表单 - 在重复方面,我更喜欢创建一次表单,并用它来创建,编辑和查看
  3. 验证 - 我想尽可能多地重用它

XML: foods_form.xml(简化):

<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>
Run Code Online (Sandbox Code Playgroud)

视图: 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
Run Code Online (Sandbox Code Playgroud)

控制器: 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
    }
}
Run Code Online (Sandbox Code Playgroud)

net*_*n73 5

Laravel不会帮助您从某些XML创建表单.

您需要使用以下库来解析XML SimpleXML:您可以在php.net上找到一些文档

首先创建一个SimpleXMLElement:

$xml = new SimpleXMLElement('../path/to/your/file/foods_form.xml', 0, true);
Run Code Online (Sandbox Code Playgroud)

您现在可以使用您的$xml对象生成表单,尊重XML的格式(转储$xml对象以了解结构)

只需将对象放在视图中即可直接使用.

要验证表单,您可以使用ValidationLaravel:链接到文档


Jus*_*tin 0

下面是您可以构建用于处理 XML 到 HTML 表单转换的类类型的开始。

<?php

class XmlToHtmlFormConverter {


    public function buildFormContent($filename)
    {
        $xml_fields = new SimpleXmlElement($filename, 0, true);
        $html = '';

        foreach ($xml_fields as $field) {
            $attributes = $field->attributes();
            $html .= '<label for="'.$attributes['name'].'">'.$attributes['label'].'</label>'.PHP_EOL;

            if ('text' == $attributes['type']) {
                $html .= '<input type="text" name="'.$attributes['name'].'" />'.PHP_EOL;
            } else {
                $html .= $this->buildOptionInputs($field);
            }
        }

        return $html;
    }


    protected function buildOptionInputs($field)
    {
        $html = '';
        $attributes = $field->attributes();
        foreach ($field->option as $option) {
            $html .= '<input type="radio" name="'.$attributes['name'].'" value="'.$option.'" />'.PHP_EOL;
        }
        return $html;
    }
}

// Uncomment below to actually see the output, this works with your xml file.
// $converter = new XmlToHtmlFormConverter;
// echo $converter->buildFormContent('form.xml');
Run Code Online (Sandbox Code Playgroud)

正如前面的答案所述,您可以将此类注入控制器构造函数中。如果你想变得更奇特,你可以创建一个接口并注入它,然后使用 App::bind('SomeInterface', 'SomeImplementation'); 将你的实现绑定到该接口。但为了简单起见,您可以直接注入类。

控制器:

class TestsController extends BaseController {

    protected $xml_to_html_form_converter;


    public function __construct(XmlToHtmlFormConverter $xml_to_html_form_converter)
    {
        $this->xml_to_html_form_converter = $xml_to_html_form_converter;
    }


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


    public function create() {
        $xml_file_path = 'some/path/xmlfile.xml';
        return View::make('create')->with(array(
            'form_content' => $this->xml_to_html_form_converter->buildFormContent($xml_file_path);
        ));
    }


    public function handleCreate() {
        // do your validations like you would with any html form
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你的观点就会是这样的......

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

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

    {{ $form_content }}

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