A helper to easy validate laravel forms.
A helper to easy validate laravel forms., (*1)
Add artisaninweb/laravel-formvalidation-helper
as requirement to composer.json, (*2)
{ "require": { "artisaninweb/laravel-formvalidation-helper": "0.1.*" } }
Replace 'Illuminate\Html\HtmlServiceProvider'
with 'Artisaninweb\FormValidationHelper\ServiceProvider'
, (*3)
Replace in aliases 'Form' => 'Illuminate\Support\Facades\Form'
with 'Form' => 'Artisaninweb\FormValidationHelper\Facades\Form'
, (*4)
The parameters to use:, (*5)
required
(bool): Make the field required.
rules
(string): Specify the rules of the validation.
error-class
(string): Add a custom class to the form field on error (optional, default: 'error')., (*6)
Form::text('field-name','field-value',[required' => true, 'rules' => 'required|email', 'error-class' => 'form-error']);
To output the error you can place this in your view.
Default the error wil come from the validator, you can edit this in /app/lang/{lang}/validation.php
., (*7)
// Replace the error with you own. Form::error('field-name','You can put a custom html error here., (*8)
'); // Only replace the html tags Form::error('field-name',':message, (*9)
');
Example:, (*10)
echo Form::open(['url' => '/login']); echo Form::text('email', '', ['required' => true, 'rules' => 'required|email', 'error-class' => 'form-error']); echo Form::error('email', '<p>This field is required.</p>'); echo Form::password('password', '', ['required' => true, 'rules' => 'required|min:8', 'error-class' => 'form-error']); echo Form::error('password', '<p>This field is required.</p>'); echo Form::submit('Login'); echo Form::close();
After a form submit you can validate the last submitted form., (*11)
Form::validate(function($postData,$passes,$messages) { var_dump($postData); var_dump($passes); var_dump($messages); });
If you are using https://github.com/rcrowe/TwigBridge
as TwigBirdge in Laravel (like i do).
You can replace 'TwigBridge\Extension\Laravel\Form'
with 'Artisaninweb\FormValidationHelper\Extension\TwigBridgeForm'
.
You will find this in app/config/packages/rcrowe/twigbridge/extensions
., (*12)