Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unique validation error #22

Closed
4 tasks done
robsonrk opened this issue Dec 4, 2019 · 3 comments
Closed
4 tasks done

[BUG] Unique validation error #22

robsonrk opened this issue Dec 4, 2019 · 3 comments
Labels
bug Something isn't working

Comments

@robsonrk
Copy link

robsonrk commented Dec 4, 2019

Prerequisites

  • Able to reproduce the behaviour outside of your code, the problem is isolated to Laraform.
  • Provided all the required dependencies to reproduce the bug (eg. form, component, model, migration).
  • Your issue isn't already filed.
  • You filled in the entire issue template.

Versions

  • PHP version: 7.3.11
  • Laravel version: 6.6.0
  • Laraform Vue package version: 1.1.8
  • Laraform Laravel package version: 1.1.8

Description

Can't validate trough Laraform default unique validation.

Steps to Reproduce

// MyForm.php
public $schema = [
    'id' => [
        'type' => 'key'
    ],
    'name' => [
        'type' => 'text',
        'label' => 'Name',
        'rules' => 'required'
    ],
    'email' => [
        'type' => 'text',
        'label' => 'Email',
        'rules' => 'required|email|unique:users,email',
    ],
    'password' => [
        'type' => 'password',
        'label' => 'Password',
        'rules' => 'required'
    ]
];

Expected behavior:

Should return error message from unique validation

The Email has already been taken.

Or nothing if email address isn't registered yet.

Actual behavior:

Console returns an error from backend:

exception: "ErrorException"
file: "/var/www/vendor/laraform/laraform-laravel/src/Controllers/ValidatorController.php"
line: 21
message: "Undefined index: attributes"

Additional Information

// src/Controllers/ValidatorContoller.php
// line: 21
dd( $request->all() );

// returns
array (
  'params' => 
  array (
    0 => 'users',
    1 => 'user@example.com',
  ),
  'value' => 'user@example.com',
)
@robsonrk robsonrk added the bug Something isn't working label Dec 4, 2019
@laraform
Copy link
Owner

laraform commented Dec 7, 2019

Hi @robsonrk! Unique is one of the validators which work differently on the frontend than the backend: https://laraform.io/docs/1.x/basics/validation#rule-unique.

The reason for this is security, so the only way to use it is to separate the frontend and backend rules. Here's an example:

  1. Create rules
'rules' => [
  'frontend' => ['required', 'email',  'unique:user_email'],
  'backend' => ['required', 'email', 'unique:users,email'],
],
  1. Set endpoint for unique validator in config
    https://laraform.io/docs/1.x/basics/configuration
Laraform.config({
  endpoints: {
    validators: {
      unique: // your endpoint, .eg 'validate/unique'
    }
  }
})
  1. Create route
<?php

// web.php

Route::post('validate/unique', 'ValidatorController@unique')->middleware('throttle:60,1')->name('validate.unique');

  1. Create validation logic
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;

class ValidatorController extends Controller
{
  public function unique(Request $request) {
    switch ($request->params[0]) {
      case 'user_email':
        $validator = Validator::make($request->all(), [
          'value' => 'unique:users,email',
        ]);

        return response()->json(!!$validator->fails());
        break;
    }
  }
}

Hope this helps!

@robsonrk
Copy link
Author

Thank you for reply.

I understood your explanation and I've read the documentation also.

But shouldn't this work out of box as Laraform have it's own validator?

<?php

namespace Laraform\Controllers;

use Validator;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ValidatorController extends Controller
{
    public function activeUrl(Request $request) {
        $validator = Validator::make($request->all(), [
            'url' => 'active_url',
        ]);

        return response()->json($validator->fails() ? false : true);
    }

    public function unique(Request $request)
    {
        $attributes = $request->all()['attributes'];

        $validator = Validator::make($request->all(), [
            'value' => 'unique:' . implode(',', $attributes),
        ]);

        return response()->json($validator->fails() ? false : true);
    }

    public function exists(Request $request)
    {
        $attributes = $request->all()['attributes'];

        $validator = Validator::make($request->all(), [
            $request->laraformFieldName => 'exists:' . implode(',', $attributes),
        ]);

        return response()->json($validator->fails() ? false : true);
    }
}

Rather, it throws an undefined index('attributes') exception at following line:
$attributes = $request->all()['attributes'];

@laraform
Copy link
Owner

Yes, it did have this feature out of the box, but it was removed for security reasons. The controller you've found is just a leftover which should be removed with the next release.

I'd recommend going with a similar solution I've described above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants