Skip to content

JsValidator Reference

albertmoreno edited this page Mar 4, 2016 · 8 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.


selector($selector)

Specifies the jQuery selector to define the form to validate

Arguments
  • $selector (string|null) JQuery Selector used to select the form to be validated. Optional, selector defined in Configuration will be used by default.
Return
  • JsValidator. instance to be rendered.
Example

Controller

    $validator1 = JsValidator::make(['search' => 'min:3']);
    $validator2 = JsValidator::make(['name' => 'required']);
    // ...
    return view('my-form-view', compact('validator1', 'validator2'));

View

    <form id="form-search">
        <input name="search" />
    </form>
    <form id="form-post">
        <input name="name" />
    </form>
    {!! $validator1->selector('#form-search') !!}
    {!! $validator2->selector('#form-post') !!}

view($view)

Specifies the view used to render Javascript Validations

Arguments
Return
  • JsValidator. instance to be rendered.
Example

Controller

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

View

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

render($view, $selector)

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', '#my-morm-id') !!}

ignore($selector)

By default hidden fields are not validated. You can override this behaviour with this method specifyings the jQuery selector from elements to be to ignored when validating, simply filtering them out. See jQuery Validation Reference to get more information.

Arguments
  • $selector (string) JQuery Selector used to select the elements to ignore.
Return
  • JsValidator. instance to be rendered.
Example

Controller

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

View

    <form>
        <!-- This field will be ignored in validation -->
        <input name="name" class="no-validate" />
    </form>
    {!! $validator->selector('#my-morm-id') !!}


Arrayable interface

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


__toString()

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

    public function __toString()
    {
         return $this->render();
    }
Clone this wiki locally