Skip to content

JsValidator Class

Albert Moreno edited this page Mar 21, 2015 · 17 revisions

A Proengsoft\JsValidation\JsValidator instance is returned by the Facade methods.

The mainly use of this object is to provide the converted rules and messages to the view, for propper rendering of Javascript code.

To customize the view refer to Javascript Rendering and JQuery Validation Plugin documentation.

Arrayable interface

This class implements Illuminate\Contracts\Support\Arrayable interface tho provide validation data to view. This keys are provided:

render($view)

Render the specified view with validator data

Arguments
Return
  • string. the html and Javascript code to initialize the validators.
Example

Controller

    $validator = JsValidator::make(['name' => 'required|min:5']);
    // ...
    return view('my-form-view', ['validator'=>$validator]);

View

    <form>
        <input name="name" />
    </form>
    {!! $validator->render('my-custom-javascript-view') !!}

__toString()

When this class is converted to string is called render method with default view

    public function __toString()
    {
        try {
            return $this->render();
        } catch (\Exception $e) {
            return '';
        }
    }