简体   繁体   中英

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://tutsnare.com/how-to-use-captcha-in-laravel-5/

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 this index() 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>{{ $error }}</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?

Yes you can validate by using normal html.

"laravelcollective/html": "~5.0" 

Here is installation process: https://laravelcollective.com/docs/5.0/html

And Here is validation explanation: https://laravel.com/docs/5.0/validation#basic-usage

Thanks

Yes, also it's possible do more validation logic if you need it.

You can take approach for Form Request https://laravel.com/docs/5.4/validation#form-request-validation and some other validations.

Also, the after hook allow to evaluate before the rules define take place.

/**
 * Configure the validator instance.
 *
 * @param  \Illuminate\Validation\Validator  $validator
 * @return void
 */
public function withValidator($validator)
{
    $validator->after(function ($validator) {
        if ($this->somethingElseIsInvalid()) {
            $validator->errors()->add('field', 'Something is wrong with this field!');
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM