Reason: There is Codeigniter 4 with a perfect validation library :)
This is a extension library of Form_validation
- Avoid resend POST vars on refresh form page (Don't worry about F5)
- Get Flashmessage for Errors
- Get Flashmessage for Success
- Ajax Validation Response
- Customize Ajax success response
- Customize Success Message
- Extends Validators (Easy and fast)
- Download the latest release
- Copy the folder FormValidation/ and the file MY_Form_validation.php to your Codeigniter folder application/libraries
application/
-- libraries/
---- FormValidation/
---- MY_Form_validation.php
Create a view file (using the functions to repopulate) The Form view
Create the controller method: The Form controller method
Create a controller method for validate your data (and save in database) The post controller method
Requirements: The JQuery plugin formHelper
Create a view file: The Form view
Create the controller method: The form controller
Open the file FormValidation/validators.php and create your function.
validators.php
/*This example is for Brasil Zip code*/
function zipcode($value)
{
return ((preg_match('/^[0-9]{5,5}([-]?[0-9]{3,3})?$/', $value))===1);
}
Done, now you can use the function in your validation rules.
$this->form_validation->set_rules('zipcode', 'Zip code', 'required|zipcode', array('zipcode'=>'Escreva um cep valido'));
Setting the success message
Parameters:
- string $text - Success message
Sets the default prefix and suffix for success message
Parameters:
- string $open - Open delimiter
- string $close - Close delimiter
Redirect after execute validation
Parameters:
- string $redirect - URI/URL to redirect
Exclude Fields to re-populate
Parameters:
- array $array Associative array with the fields to exclude
Run the validation
Parameters:
- callable $success_cb Successfull callback
- callable $error_cb Error callback
Get the validation message
Return: string The message (Html code)
Get Form Values (JSON string) from flashdata
Return: string - Json string
Setting the message template
Parameters:
- string $template - Template string
- array $classType - (Optional) CSS Class to replace in template. Examples: ['class-error'=>'alert-danger', 'class-success'=>'alert-success']. Defaults: ['class-error'=>'error', 'class-success'=>'success']
Add variable on success json response (Only for ajax request)
Parameters:
- string $key - Key name
- string $value - Value
Display the value for input field. Use in a view file.
Parameters:
- string $field - Input name
- string $default - (Optional) Default value
Display 'checked' for input type checkbox and radio button. When the value is present in form values, display "checked". Use in a view file.
Parameters:
- string $field - Input name
- string $value - This value is setting? .. So, Checked
Display 'selected' for input select(comobobox). When the value is present in form values, display "selected". Use in a view file.
Parameters:
- string $field - Input name
- string $value - This value is setting? .. So, Selected