mardi 21 février 2017

Can I validate form input also without using the laravelcollective/html namespace in a Laravel application?

I am pretty new in PHP and moreover in Laravel framework and I have the following doubt.

I am following this tutorial to insert a reCAPTCHA into a form (but my doubt is more related to form validation than to reCAPTCHA): http://ift.tt/2ly3WLk

So to declare a form it use this syntax into the view:

{!! Form::open(array('url'=>'contact','method'=>'POST', 'id'=>'myform')) !!}

I think that this syntax is related to the laravelcollective/html namespace, is it?

So I have installed it performing the statment:

composer require "laravelcollective/html":"^5.4"

In the controller method is defined thisindex() method that handle the form sumbit operation:

<?php namespace App\Http\Controllers;
use Input;
use Validator;
use Redirect;
use Session;
class EnquiryController extends Controller {
    public function index()
    {
        $data = Input::all();
        $rules = array(
            'name' => 'required',
            'email' => 'required|email',
            'subject' => 'required',
            'g-recaptcha-response' => 'required|captcha',
            'msg' => 'required',
        );
        $validator = Validator::make($data, $rules);
        if ($validator->fails()){
            return Redirect::to('/contact')->withInput()->withErrors($validator);
        }
        else{
            // Do your stuff.
        }
    }
}

So, as you can see in the previous code snippet, this method provide input validation using the $rules array and if validation fails there is a redirect to the same page showing including the validation errors:

return Redirect::to('/contact')->withInput()->withErrors($validator);

that will be print in the view by this section of code:

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <strong>Whoops!</strong> There were some problems with your input.<br /><br />
        <ul>
            @foreach ($errors->all() as $error)
               <li></li>
            @endforeach
        </ul>
    </div>
@endif

My doubt is: can I validate the form input and returns the potential errors in the same way also using the pure HTML form instead the one provided by the laravelcollective/html namespace?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lHsku8
via IFTTT

Aucun commentaire:

Enregistrer un commentaire