📜 ⬆️ ⬇️

Extension of the Form_validation class with support for default field values

Once asked in the comments such a question . I recently solved this problem by expanding the Form_validation class in CodeIgniter (vaguely resembling the alteration suggested to me in response to my question).
In general, it is strange that there is no such functionality in the validation class initially - the form helper function for displaying the field value, setting the checkbox, etc (set_value, set_checkbox) supports the default field value, but setting default values ​​in mappings everywhere in set_value is not very convenient.

Therefore, such an extension of the validation class was written, which allows you to set an array of default values. The methods used by the form helper are also redefined.


<?php
class MY_Form_validation extends CI_Form_validation
{
var
$_default_fields = array();

function
set_default_values ( $default )
{
if (
is_array ( $default )) {
$this -> _default_fields = $default ;
}
}

function
_get_default_field_value ( $field , & $default )
{
if (
!$default ) {
if (isset(
$this -> _default_fields [ $field ])) {
$default = $this -> _default_fields [ $field ];
}
}
}

function
set_value ( $field = '' , $default = '' )
{
$this -> _get_default_field_value ( $field , $default );
return
parent :: set_value ( $field , $default );
}

function
set_checkbox ( $field = '' , $value = '' , $default = FALSE )
{
$this -> _get_default_field_value ( $field , $default );
return
parent :: set_checkbox ( $field , $value , $default );
}

function
set_radio ( $field = '' , $value = '' , $default = FALSE )
{
$this -> _get_default_field_value ( $field , $default );
return
parent :: set_radio ( $field , $value , $default );
}

function
set_select ( $field = '' , $value = '' , $default = FALSE )
{
$this -> _get_default_field_value ( $field , $default );
return
parent :: set_select ( $field , $value , $default );
}
}



You can see that all changes to the set_ * methods have been reduced to a preliminary call to the _get_default_field_value method that checks the $ default argument passed, and if it is empty (if the string is empty, if the number is 0, or just FALSE), checks whether for the specified field, the default value in the array, and if it is, assigns this value to $ default, and then the parent method set_ * is called. Thus, if you specify any default value in set_ *, then it will be used first. That is, this extension does not affect the code already written and works based on the Form_validation / Form helper.
')
Thus, in the controller when the form is first (first) sent to Form_validation, an array of fields obtained, for example, from the database, and when the form is output after submitting it (in case of validation error), does not send an array of default values, everything values ​​from $ _POST will be used

I suspect that experienced developers writing under CI have done such a thing for a long time, but it was interesting for me to use the functionality already available in the framework for more convenient work with the validation class.

Source: https://habr.com/ru/post/52412/


All Articles