• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation Always Print Error Message

#4
My approach would be:

controller:
PHP Code:
public function pagecreate()
{
    if ($this->request->getMethod() === 'post')
    {
        if ($this->validate(['title' => 'required''body' => 'required']))
        {
            if ($this->model->save(['title' => $this->request->getPost('title'), 'content' => $this->request->getPost('body')]))
            {
                return redirect()->to('');
            }
            else
            
{
                return redirect()->back()->withInput()->with('message''Unable to Save.');
                //Change the code above for the following if the validation is done in the model and remove 'if validate section'
                //return redirect()->back()->withInput()->with('errors', $this->model->errors());
            }
        }
        else
        
{
            return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
        }
    }
    return view('Backend/Pages/Create');


views:
PHP Code:
//Backend/Themes/Base
<html>...
<
body>
...
<
div class="container">
    <?
php if (session('message')) : ?>
        <div class="alert alert-info alert-dismissible">
            <?= session('message'?>
            <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
        </div>
    <?php endif ?>
    <?= $this->renderSection('content'?>        
</div>
</body>
</html> 
PHP Code:
// Backend/Pages/Create
<?= $this->extend('Backend/Themes/Base'?>
<?= $this
->section('content'?>
<h4>Create New Page</h4>
<form method="post">
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control <?= session('errors.title') ? 'is-invalid' '' ?>" id="title" name="title" value="<?= old('title'?>">
        <div class="invalid-feedback"><?= session('errors.title'?></div>
    </div>
    ...
</form>
<?= $this->endSection() ?>
Reply


Messages In This Thread
RE: Validation Always Print Error Message - by elephpantech - 04-03-2019, 06:31 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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