Laravel validation

Asked

Viewed 592 times

2

I need to do a data validation coming from an XML, have as I use a Validation Request?

Or Validation Request only for data coming from a form?

  • It would be interesting to give an example of XML and what has already mounted.

  • I believe that requests are only for form validations.

1 answer

3


You cannot directly use the Request Validation of Laravel 5, but you can use the Validator with any array. First read the XML contents.

<?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 :(';

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.