Wednesday 20 September 2023

Laravel Rule Validations

Laravel validations

$errorMessage = "NFL Competition already exist for year $this->year and with the selected competition type.";

return [

    'name' => 'string',
    'abbreviation' => 'required|max:10',
    'year' => 'required|digits:4|integer|min:1900|max:' . (date('Y') + 1),
    'competition_type_id' => ['required', 'string', new IsCompositeUnique('basketball_competitions', ['year' => $this->year, 'competition_type_id' => $this->competition_type_id], $this->competition_id, $errorMessage)],

];





<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\DB;

class IsCompositeUnique implements Rule
{
/**
* @var string
*/
private $tableName;
/**
* @var array
*/
private $compositeColsKeyValue = [];
/**
* @var mixed|null
*/
private $rowId;

private $errorMessage;

/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(string $tableName, array $compositeColsKeyValue, $rowId = null, $errorMessage = null)
{
$this->tableName = $tableName;
$this->compositeColsKeyValue = $compositeColsKeyValue;
$this->rowId = $rowId;
$this->errorMessage = $errorMessage;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value): bool
{
if ($this->rowId) {
$record = DB::table($this->tableName)->where($this->compositeColsKeyValue)->first();
$passess = !$record || ($record && $record->id == $this->rowId);
} else {
$passess = !DB::table($this->tableName)->where($this->compositeColsKeyValue)->exists();
}

return $passess;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message(): string
{
if ($this->errorMessage) {
return $this->errorMessage;
}
$colNames = '';
foreach ($this->compositeColsKeyValue as $col => $value) {
$colNames .= $col . ', ';
}
$colNames = rtrim($colNames, ', ');

return "The combination of $colNames must be unique.";
}
}
Share:

Monday 26 June 2023

Laravel Nova ReadOnly Problems

 On Laravel Nova,   when creating record field with readonly and default values are not submitted.  This is the correct way to work around it.


Text::make("Field Name", 'field_name')
->withMeta(
[
'extraAttributes' => ['readonly' => true],
'value' => $this->field_name ?? 'Default Value'
]), 
Share:

Thursday 19 May 2022

PHP Regex Sample Code


A regular expression (shortened as regex or regexp or rational expression) is a sequence of characters that specifies a search pattern in the text.

A good testing site is 

https://infoheap.com/php-preg_match-online/ 

Examples of regex for password

PHP Version

/^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z])(?=\S*[\W]).{8,}$/


Must have special characters

(?=\S*[\W])


Must have a number

(?=.*\\d)


Must have uppercase and lowercase

(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z])


Some reference

https://stackoverflow.com/questions/8141125/regex-for-password-php

https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a

Share:

Friday 22 April 2022

Tuesday 8 March 2022

Wednesday 9 February 2022

Changing .env Programatically

With great powers comes great responsibility, changing .env values in the server were great but if you don't have access to it this code might come in handy.

In your route, where key is the key, and value will be the new env value


Route::get('changeenv/{key?}/{value?}',"controller@methodname"); 

In your controller


  public function changeEnv($key, $value)
    {
        $path = app()->environmentFilePath();

        $escaped = preg_quote('=' . env($key), '/');

        file_put_contents($path, preg_replace(
            "/^{$key}{$escaped}/m",
            "{$key}={$value}",
            file_get_contents($path)
        ));

        return "env change successfully " .  env($key);
    }

In your browser, you could type this to change the .env's CAPTCHA value

https://mysite.com/changeenv/CAPTCHA/new_value
Share:

Tuesday 8 February 2022

Laravel SMTP Settings for Different Mail Service Provider

Laravel SMTP Settings for Different Mail Service Provider MAILHOG

brew update && brew install mailhog
For Laravel .env

MAIL_DRIVER=smtp
MAIL_HOST=0.0.0.0
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
Run in Terminal

mailhog
Testing Email Open this link Mailhog UI

Just continue sending email
Share:

Popular Posts

Recent Posts

Pages

Powered by Blogger.

About Me

My photo
For the past 10 years, I've been playing with codes using PHP, Java, Rails. I do this for a living and love new things to learn and the challenges that comes with it. Besides programming I love spending time with friends and family and can often be found together catching the latest movie or planning a trip to someplace I've never been before.