Laravel validation

1

I need to do a validation of data coming from an XML, how can I use a Validation Request ?

Or Validation Request only serves data coming from a form?

    
asked by anonymous 26.05.2015 / 02:46

1 answer

3

You can not directly use the Request Validation of Laravel 5, but you can use Validator with any array . You must first read the XML content.

<?php

public function validate(){
    $xml = '<book>
      <author>Gambardella, Matthew</author>
      <title>XML Developer\'s Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications with XML.</description>
    </book>';

    // Rotina para extrair os dados do XML
    $xmlData = (array) simplexml_load_string($xml);

    $rules = [
        'author' => 'string|required',
        'title' => 'string|required',
        'genre' => 'string',
        'price' => 'numeric',
        'publish_date' => 'date',
        'description' => 'string'
    ];

    if (Validator::make($xmlData, $rules)){
        return $xmlData;
    }

    return 'XML Invalido :(';

}
    
27.05.2015 / 16:17