Welcome Guest, Not a member yet? Register   Sign In
Strange Validation Behaviour
#1

(This post was last modified: 08-22-2020, 01:56 AM by 68thorby68.)

Hi,
I have a 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_FLOATFILTER_FLAG_ALLOW_THOUSAND),2);
}else{            
    $data['quote_value'] = '';
}

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

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

if(
$this->request->getServer('REQUEST_METHOD') == 'POST'){
    
helper(['form''url']);
    
    
$this->validation->reset();    
            
    
//Standalone Validation
    
$this->validation->setRules([
        
'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');
            
    
$validation_errors=$this->validation->listErrors();
    
    if(!empty(
$validation_errors)) {
        return 
redirect()->back()->withInput()->with('validation'$this->validation->listErrors());
    }else{
        ... 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>
    </div>
<?php endif ?>

Very, very strange??
Reply


Messages In This Thread
Strange Validation Behaviour - by 68thorby68 - 08-22-2020, 01:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB