• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with validatiomessage

#1
Hello,

I have a problem with the validationmessage in the model.
The validation works perfect but why can't i see the validationmessage from the model?
Through this way i can my own message in Dutch for each error.
As this works then i can have the field names in englisch and not in Dutch.

Thanx for the help


Controller:
PHP Code:
<?php namespace App\Controllers;
use 
App\Models\DepartmentModel;
use 
App\Entities\Departments;

public function 
create()
    {
        $model = new DepartmentModel();

        // Validate here first, since some things
        $rules array_merge($model->getValidationRules(['only' => ['naam']]));

        if (! $this->validate($rules))
        {
            return redirect()->back()->withInput()->with('errors'$model->errors());
        }

        // Save the department
        $department = new Departments($this->request->getPost());

        if (! $model->save($department))
        {
            return redirect()->back()->withInput()->with('errors'$model->errors());
        }

        // Success!
        return redirect()->route('department')->with('message''Afdeling is toegevoegd.');

    


Model:
PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;
use 
App\Entities\Departments;

class 
DepartmentModel extends Model
{
    protected $table      'department';
    protected $primaryKey 'id';

    protected $returnType 'App\Entities\Departments';
    protected $useSoftDeletes false;

    protected $allowedFields = ['naam''omschrijving''actief'];

    protected $useTimestamps true;
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';
    protected $deletedField  'deleted_at';

    protected $validationRules    = [
        'naam'          => 'required|min_length[2]|max_length[150]|is_unique[department.naam,id,{id}]',
        'omschrijving'  => 'max_length[150]',
    ];
    protected $validationMessages = [
        'naam'=> [
            'min_length' => 'Sorry. Deze naam is reeds al aanwezig. Kies een andere naam.'
        ]
    ];
    protected $skipValidation     false;




View:
PHP Code:
<?= $this->extend('base'?>

<?= $this->section('breadcrumb'?>

<div class="row">
    <div class="col-12">
        <div class="page-title-box">
            <div class="page-title-right">
                <ol class="breadcrumb m-0">
                    <li class="breadcrumb-item"><a href="<?= route_to('/'?>">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?= route_to('department'?>"><?= $title ?></a></li>
                    <li class="breadcrumb-item active">Nieuw</li>
                </ol>
            </div>
            <h4 class="page-title"><?= $title ?></h4>
        </div>
    </div>
</div>

<?= $this->endSection() ?>

<?= $this->section('content'?>

<div class="row">
    <div class="col-lg-6 offset-lg-3 col-md-12">
        <div class="card">
            <div class="card-body">
                <h4 class="header-title mb-3">Afdeling nieuw</h4>

                <form class="needs-validation" action="<?= route_to('department'?>" method="post" novalidate>
                    <?= csrf_field() ?>
                    <div class="form-group mb-3">
                        <label for="naam">Naam<span class="text-danger">*</span></label>
                        <input type="text" class="form-control <?php if(session('errors.naam')) : ?>is-invalid<?php endif ?>" id="naam" name="naam" placeholder="Naam" value="<?= old('naam'?>" required autofocus>
                        <div class="invalid-feedback">
                            <?= session('errors.naam'?>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                        <label for="omschrijving">Omschrijving</label>
                        <input type="text" class="form-control <?php if(session('errors.omschrijving')) : ?>is-invalid<?php endif ?>" id="omschrijving" name="omschrijving" placeholder="Omschrijving" value="<?= old('omschrijving'?>">
                        <div class="invalid-feedback">
                            <?= session('errors.omschrijving'?>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                        <div class="custom-control custom-checkbox form-check">
                            <input type="checkbox" class="custom-control-input" id="actief" name="actief" value="1" checked>
                            <label class="custom-control-label" for="actief">Actief</label>
                        </div>
                    </div>
                    <a class="btn btn-info mr-2" href="<?= route_to('department'?>">Terug</a>
                    <button class="btn btn-primary" type="submit">Verstuur</button>
                </form>

            </div> <!-- end card-body-->
        </div> <!-- end card-->
    </div> <!-- end col-->
</div> <!-- end row -->

<?= $this->endSection() ?>

Entities:
PHP Code:
<?php namespace App\Entities;

use 
CodeIgniter\Entity;

class 
Departments extends Entity
{
    /**
     * Define properties that are automatically converted to Time instances.
     */
    protected $dates = ['created_at''updated_at''deleted_at'];

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.