This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

butching
butching

how can i add custom validation rule for example

class MyClass extends Model
{
       $rules = [
               'firstname' => 'required|mycustomrule';
               'lastname' => 'required|mycustomrule';
      ]

      public function mycustomrule($attribute, $value, $parameters) {
           return  Db::table('users')->where('firstname', $this->firstname)->where('lastname',$this->lastname)->count();
      }
}

what im trying to do is check the uniqueness of firstname and lastname together from db.

adc_91
adc_91

In the documentation explain in detail: https://octobercms.com/docs/services/validation#custom-validation-rules

In the same way I leave an example of what I always use when I need to create custom validation rules.

First I create a classes folder inside the plugin and extend "\Illuminate\Validation\Validator"

<?php namespace Acme\Demo\Classes;

class CustomValidator extends \Illuminate\Validation\Validator { public function validatePriceformat($attribute, $value, $parameters) { // Your code validation here return false; } }

Then in the model register the new validation rule.

use \Acme\Demo\Classes\CustomValidator;

Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes) { return new CustomValidator($translator, $data, $rules, $messages, $customAttributes); });

Then it's a matter of applying the priceformat rule to the field you want to validate and that's it.

pain-spark50564
pain-spark50564

adc_91 said:

In the documentation explain in detail: https://octobercms.com/docs/services/validation#custom-validation-rules

In the same way I leave an example of what I always use when I need to create custom validation rules.

First I create a classes folder inside the plugin and extend "\Illuminate\Validation\Validator"

<?php namespace Acme\Demo\Classes;

class CustomValidator extends \Illuminate\Validation\Validator { public function validatePriceformat($attribute, $value, $parameters) { // Your code validation here return false; } }

Then in the model register the new validation rule.

use \Acme\Demo\Classes\CustomValidator;

Validator::resolver(function($translator, $data, $rules, $messages, $customAttributes) { return new CustomValidator($translator, $data, $rules, $messages, $customAttributes); });

Then it's a matter of applying the priceformat rule to the field you want to validate and that's it.

I dont got it, can you send the code of all files ? Exemple: Where you put the validator code ? Where $rules enter.

I dont know from where start ...

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.