Welcome Guest, Not a member yet? Register   Sign In
Form validation in model
#1

I am following the News tutorial and now I try to move the validation from the News controller/create() to the  News model. 
Controller: 
PHP Code:
<?php namespace App\Controllers;
use 
App\Models\NewsModel;
use 
CodeIgniter\Controller;

class 
News extends Controller
{    
    
public function create()
    {
        helper('form');
        $model = new NewsModel();

        if ($model->save([
                'title' => $this->request->getVar('title'),
                'slug'  => url_title($this->request->getVar('title')),
                'body'  => $this->request->getVar('body'),
            ]) === false)
        {
            echo view('templates/header', ['title' => 'Create a news item']);
            echo view('news/create', ['errors' => $model->errors()]);
            echo view('templates/footer');
        }
        else
        {
            echo view('news/success');
        }
    }

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

use 
CodeIgniter\Model// Loads database library etc

class NewsModel extends Model
{
    protected $table 'news'
    protected $returnType 'object';
    protected $allowedFields = ['title''slug''body'];

    protected $validationRules    = [
        'title' => 'required|min_length[3]|max_length[255]',
        'body'  => 'required'
    ];

    protected $validationMessages = [
        'body'        => [
            'required' => 'Can not be empty!'
        ]
    ];
    

I expected the validation rules stated in $validaiotnRules to kick in on $model->save().
The actual result is that I get the "Whoops" error when calling News/create()
Reply
#2

Edit (or create) your .env file and set the environment to “development” - that will show you the actual error instead of the generic “Whoops”
Reply
#3

(12-20-2019, 06:57 AM)MGatner Wrote: Edit (or create) your .env file and set the environment to “development” - that will show you the actual error instead of the generic “Whoops”
I get "Argument 1 passed to url_title() must be of the type string, null given, called in C:\xampp\htdocs\ci4\app\Controllers\News.php on line 49".
I think I must look for a POST before I run $model->save(). So I update my news/create() to:
PHP Code:
<?php namespace App\Controllers;
use 
App\Models\NewsModel;
use 
CodeIgniter\Controller;

class 
News extends Controller
  

    
public function create()
    {
        helper('form');
        $model = new NewsModel();

        if (!$_POST OR $model->save([
                'title' => $this->request->getVar('title'),
                'slug'  => url_title($this->request->getVar('title')),
                'body'  => $this->request->getVar('body'),
            ]) === false)
        {
            echo view('templates/header', ['title' => 'Create a news item']);
            echo view('news/create', ['errors' => $model->errors()]);
            echo view('templates/footer');
        }
        else
        {
            echo view('news/success');
        }
    }


Now I can insert new posts but if I submit an empty form, my validation error doesn't display. My view:
PHP Code:
<h2><?= esc($title); ?></h2>

<?= \Config\Services::validation()->listErrors(); ?>

<form action="/news/create" method="post">

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

    <label for="body">Text</label>
    <textarea name="body"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form> 


d in C:\xampp\htdocs\ci4\app\Controllers\News.php on line 49ng, null given, called in C:\xampp\htdocs\ci4\app\Controllers\News.php on line 49 
Reply
#4

Hi,

Use $this->request->postVar for get your post input

(Sorry for my very bad english, i'm French)
Reply
#5

(This post was last modified: 12-20-2019, 12:17 PM by muuucho.)

I changed my view to: (edit code)
PHP Code:
<h2><?= esc($title); ?></h2>

<form action="/news/create" method="post">
    <label for="title">Title</label>
    <br>
    <?php if(isset($errors['title'])){echo $errors['title'].'<br>';}?>
    <input type="input" name="title" /><br />

    <label for="body">Text</label>
    <br>
    <?php if(isset($errors['body'])){echo $errors['body'].'<br>';}?>
    <textarea name="body"></textarea><br />
    <input type="submit" name="submit" value="Create news item" />
</form> 
...so problem solved. But, why didn't this vork in the view: <?= \Config\Services::validation()->listErrors(); ?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB