Implement new validation
Created 6 months ago by cyno

Hi, I'm trying to implement a VAT Number validator, but I cannot make it work. The laravel package I chose for this is this: https://github.com/mpociot/vat-calculator . The package says it can be used as a new rule: 'vat_number'. I've tried two different methods, none of them seems to work. Any chance you could help me? First one is simply use it in the form builder as: '' 'vat_number' => [ 'required' => false, 'label' => 'VAT Number', 'type' => 'anomaly.field_type.text', 'rules' => [ 'vat_number' ], '' But it ends in an error: BadMethodCallException Method [validateVatNumber] does not exist.

Second thing I tried is to make a validator. Problem is I know how to do it with laravel and request, but i cannot make it work with formbuilder. Code:

FormBuilder:

'vat_number' => [ 'required' => false, 'label' => 'VAT Number', 'type' => 'anomaly.field_type.text', 'rules' => [ 'vat_number' ], 'validators' => [ 'vat_number' => [ 'message' => false, 'handler' => ValidateVAT::class, ], ], ],

Validator: <?php namespace Hustle\OrdersModule\Order\Validation;

use Anomaly\Streams\Platform\Ui\Form\FormBuilder; use Mpociot\VatCalculator\Validators\VatCalculatorValidatorExtension;

class ValidateVAT { public function handle(FormBuilder $builder, $attribute, $value) { $validator = Validator::make($builder, ['vat_number' => 'vat_number']);

    foreach ($validator->errors()->all() as $error) {
        $builder->addFormError($attribute, $error);
    }

    return $validator->passes();
}

}

Also I don't know how to format the code here my apologies.

william  —  6 months ago

At the end of this page , you might find some info :

https://github.com/pixney/pyrocms-cheatsheet