Helpers

In this section you will find general helpers available in the Utils package.

Translations

Some of the most common translations are available in the package. You can use them in your application by calling __ function

        
__('pjutils::validation.required')

There is prepared class to differentiate grammatical genders in translations

        
use Patrikjak\Utils\Common\Helpers\GrammaticalGender;
 
trans_choice('pjutils::validation.required', GrammaticalGender::MASCULINE) // :attribute je povinný
trans_choice('pjutils::validation.required', GrammaticalGender::FEMININE) // :attribute je povinná
trans_choice('pjutils::validation.required', GrammaticalGender::NEUTER) // :attribute je povinné

Rules

Password

There is a password rule available in the package. You can use it in your validation rules.

        
use Patrikjak\Utils\Common\Rules\Password;
 
public function rules(): array
{
return [
'password' => ['required', new Password()],
];
}

What is this rule doing?

Telephone

There is a telephone rule available in the package. You can use it in your validation rules.

        
use Patrikjak\Utils\Common\Rules\TelephoneNumber;
use Patrikjak\Utils\Common\Services\TelephonePattern;
 
public function rules(): array
{
return [
'telephone' => ['required', new TelephoneNumber(TelephonePattern::SK)],
];
}

What is this rule doing?

Supported formats:

TelephonePattern service can show example with getExample

        
use Patrikjak\Utils\Common\Services\TelephonePattern;
 
TelephonePattern::SK->getExample(); // +421123456789

Middlewares

VerifyRecaptcha - check recaptcha token

There is a middleware available in the package. You can use it in your routes.

You need to set up recaptcha secret key in your .env file.

        
RECAPTCHA_SECRET_KEY=your_secret_key

Or you can publish config and set it up how you want.

        
php artisan vendor:publish --tag=config

You need to set up recaptcha site key in your js files as global variable. This is recognized by connector and it will send recaptcha token in the request.

        
window['RECAPTCHA_SITE_KEY'] = 'token';

Then you can use the middleware in your routes.

        
use Patrikjak\Utils\Common\Http\Middlewares\VerifyRecaptcha;
 
Route::post('/register', [RegisterController::class, 'store'])
->middleware(VerifyRecaptcha::class);

You will need to pass recaptchaToken in your request.

If recaptcha token is not valid or missing, by default it will return 422 status code with InvalidRecaptchaTokenException

If you are using Form from Utils package, you will activate recaptcha by setting recaptcha method in the form component.

        
new Form().setRecaptchaAction('register').bindSubmit();

In this case with Form.defaultErrorCallback - default error callback for the form component, it will show error message in the form - email input. This is the default behaviour of InvalidRecaptchaTokenException exception.

In case you want to change the default behaviour, you can create your own exception handler and pass it to the middleware.

        
use Patrikjak\Utils\Common\Http\Middlewares\VerifyRecaptcha;
 
Route::post('/register', [RegisterController::class, 'store'])
->middleware(VerifyRecaptcha::withExceptionClass(YourException::class));

Requests

ValidationException trait

There is a trait available in the package. You can use it in your requests.

It will throw Illuminate\Validation\ValidationException with errors and 422 status code if the request is not valid.

        
use Patrikjak\Utils\Common\Http\Requests\Traits\ValidationException;
 
class StoreRequest extends FormRequest
{
use ValidationException;
 
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8'],
];
}
}