Home > other >  Problems with Laravel and validation
Problems with Laravel and validation

Time:08-26

Im building CRUD for my application in Laravel 9. I defined some fields in my database as NOT NULL. Those values will be completed by code at the time a new user is created.

The first field Im dealing with is 'creado' (same as 'created at' in english) field (and I dont want to use built in timestamp)

User model:

public $timestamps = false;
protected $fillable = [
        'username',
        'password',
        'apellidos',
        'nombres',
        'tipoDocumento',
        'nroDocumento',
        'cuit',
        'cuil',
        'fechaNacimiento',
        'sexo',
        'domicilio',
        'domicilioNro',
        'domicilioPiso',
        'domicilioDepto',
        'localidad',
        'codigoPostal',
        'telefonoFijo',
        'telefonoCelular',
        'telefonoAlt',
        'email',
        'creado', //This is the value
    ];

protected $hidden = [
    'password',
    'password_repeat', //This value is still being displayed in error msg...
    'remember_token',
];

protected $casts = [
    'creado' => 'datetime', //Dont think its necessary
];

public function setPasswordAttribute($password)
{
    $this->attributes['password'] = bcrypt($password);
}

public function setCreadoAttribute()
{
    //This method never get called...
    $this->attributes['creado'] = date("Y-m-d H:i:s");
}

RegisterRequest:

public function rules()
{
    return [
        'username'          => 'required|unique:clientes,username',
        'password'          => 'required|min:8',
        'password_repeat'   => 'required|same:password',
        'apellidos'         => 'required',
        'nombres'           => 'required',
        'fechaNacimiento'   => 'required',
        'sexo'              => 'required',
        'tipoDocumento'     => 'required',
        'nroDocumento'      => 'required|unique:clientes,nroDocumento',
        'cuil'              => 'nullable|min:11|max:11',
        'cuit'              => 'nullable|min:11|max:11',
        'domicilio'         => 'required',
        'domicilioNro'      => 'required',
        'localidad'         => 'required',
        'codigoPostal'      => 'required',
        'telefonoCelular'   => 'required',
        'email'             => 'required|unique:clientes,email',
        'creado'            => 'nullable|date',
    ];
}

Controller:

public function register(RegisterRequest $request)
{
    $cliente = Clientes::create($request->validated());
    return redirect("login")->with("success","Bla Bla Bla");
}

In view, all fields are present, except 'creado'. If I add input type text named creado, it works. Else the field its not included in the query. I dont know why its not working when I marked it as nullable.

CodePudding user response:

$casts array and setCreado can be removed. Then, you can use an accessor, in your model:

protected function creado(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => date("Y-m-d H:i:s"),
        );
    }

Mas info ;) Accessors

Importante this: This method name should correspond to the "camel case" representation of the true underlying model attribute / database column when applicable.

CodePudding user response:

From the docs

If you need to customize the names of the columns used to store the timestamps, you may define CREATED_AT and UPDATED_AT constants on your model:

<?php
 
class Flight extends Model
{
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = 'updated_date';
}
  • Related