Welcome Guest, Not a member yet? Register   Sign In
{field} placeholder does not get replaced with field name in a custom error message
#8

Hi there...

After hours of try and error I found a solution for this problem. In my solution I use my code to show how it works fine:

The "MyRules" file (part):
PHP Code:
public function myinvaliddate($str)
    {
        switch(session()->get('locale')){
            case "en":
                $format "Y-m-d H:i";
                break;
            case "de":
                $format "d.m.Y H:i";
                break;
        }
        $d = \DateTime::createFromFormat($format$str);
        return $d && $d->format($format) == $str;
    


The Model (part with rules & messages):
PHP Code:
protected $validationRules = [
        'title'             => 'trim|required|min_length[5]|max_length[190]',
        'category'          => 'required',
        'description'       => 'trim|required|min_length[10]',
        'starts_at'         => [
            'label' => 'Homegames.form.event_start',
            'rules' =>'required|format_date|myinvaliddate|dateinpast',
            'errors' => [
                'required' => 'Homegames.validation.description_required',
                'format_date' => 'Homegames.validation.format_date',
                'dateinpast' => 'Homegames.validation.date_in_past',
            ],
        ],
        'ends_at'           => [
            'label' => 'Homegames.form.event_end',
            'rules' => 'required|format_date|myinvaliddate|dateinpast',
            'errors' => [
                'required' => 'Homegames.validation.description_required',
                'format_date' => 'Homegames.validation.format_date',
                'dateinpast' => 'Homegames.validation.date_in_past',
                'myinvaliddate' => 'Homegames.validation.myinvaliddate',
            ],
        ],
    ]; 


The Language-File (part):
PHP Code:
'validation' => [
        'description_required' => 'Das {field} - Formularfeld ist erforderlich',
        'date_in_past' => 'Das {field} - Formularfeld darf nicht in der Vergangenheit sein',
        'end_before_start' => 'Das Enddatum darf nicht früher als das Startdatum sein',
        'wrong_format' => 'Das {field} - Datum muss von folgendem Format sein: dd.mm.JJJJ ST:MI',
        'myinvaliddate' => 'Das {field} enthält ein ungültiges Datum'
    ], 


I hope my approach will help you with your project.

Greetings,

Kighlander
Reply


Messages In This Thread
RE: {field} placeholder does not get replaced with field name in a custom error message - by Codinglander - 01-23-2021, 05:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB