• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with validation

Are there known bugs with CI4 Validation using the config/validation.php file?
This doesn't work as per the documentation?

Is there a preferred method or workaround when using the validation library?

With this info we can't help you.
More info (code) is welkom.

The reason I ask, is because I have asked questions about validation before (see beow) and there seems to be no answers. I thlught I might be missing something, like there was known errors or bugs.

I have simple validation rules configured in Config/Validation.php

public $Quote_value = [
'quote_value' => [
'rules' => 'required|decimal',
'errors' => [
'required'=> 'Quote Value is required',
'decimal' => 'Quote Value must be a valid number i.e. 100.00.'

'labour_estimate' => [
'rules' => 'required|decimal',
'errors' => [
'required'=> 'Labour Estimate is required',
'decimal' => 'Labour Estimate must be a valid number i.e. 100.00.'

'costs_estimate' => [
'rules' => 'required|decimal',
'errors' => [
'required'=> 'Costs Estimate is required',
'decimal' => 'Costs Estimate must be a valid number i.e. 100.00.'

My controller

PHP Code:
if ($this->request->getPost('quote_value')) {
$data['quote_value'] = number_format($this->request->getPost('quote_value', FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND),2);
$data['quote_value'] = '';

if ($this->request->getPost('labour_estimate')) {
$data['labour_estimate'] = number_format($this->request->getPost('labour_estimate', FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND),2);
$data['labour_estimate'] = '';

if ($this->request->getPost('costs_estimate')) {
$data['costs_estimate'] = number_format($this->request->getPost('costs_estimate', FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND),2);
$data['costs_estimate'] = '';

if($this->request->getServer('REQUEST_METHOD') == 'POST'){
helper(['form', 'url']);


//Standalone Validation
'quote_value' => 'required|decimal'

//Validate using Config/Validation.php
if(!empty($data['quote_value']) || !empty($data['labour_estimate']) || !empty($data['costs_estimate'])) {
$this->validation->run($data, 'Quote_value');

$this->validation->run($data, 'Quote');


if(!empty($validation_errors)) {
return redirect()->back()->withInput()->with('validation', $this->validation->listErrors());
... do something else;

When quote_value = 100.00, labour_estimate=20.00, costs_estimate=10.00, and //Standalone Validation is used. NO ERRORS reported

Using the same values and //Validate using Config/Validation.php. ERROR "The quote_value field must contain a decimal number."
NOTE: I only use one validation method at a time when running the tests.

This is strange on 4 levels
1) I did not expect an error returned
2) Why would the same rule work using one method and not the other
3) Why would one field fail validation and the other pass, when essentiallly they are identical
4) Why is CI4 not returning the text defined in 'errors' array of the validation rule (i.e. "Quote Value must be a valid number i.e. 100.00."). Note I get the same text using getErrors() and listErrors()

I am viewing the validation reponse
PHP Code:
<?php if (session()->has('validation')) : ?>
<div class="alert alert-danger alert-dismissible">
<?= session('validation') ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
<?php endif ?>

Very, very strange??

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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