I know to use set_value()
for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isnt submitted or its before a form submission?
我知道使用set_value()来设置CodeIgniter表单中的默认值,但是如果值未提交或在表单提交之前,如何设置验证的默认值?
public function index($uri = NULL)
{
$this->load->helper('form');
$this->load->library('form_validation');
//pull from DB or Session
$data = array(
'Status' => 'users_default_status',
'Order' => 'users_default_order',
'Asc' => 'users_default_asc'
);
$this->form_validation->set_data($data);
$this->form_validation->set_rules('Status', 'Status', 'numeric|trim|required|strtolower');
$this->form_validation->set_rules('Order', 'Order', 'trim|required');
$this->form_validation->set_rules('Asc', 'Asc', 'trim|required|strtolower');
if ($this->form_validation->run() === FALSE) // validation failed
{
// validation failed but maintain submitted values in form/feedback
}else{
// validation successful, do whatever
}
}
So that if there was a form submission it uses the POST values, and if not it uses defaults (but still validated). I would like the defaults to work on a variable by variable basis, so some can be defaults some can be user submitted.
因此,如果有表单提交它使用POST值,如果没有它使用默认值(但仍然有效)。我希望默认值在变量的基础上处理变量,因此有些可以是默认值,有些可以是用户提交的。
2 个解决方案
#1
I know to use
set_value()
for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isn't submitted or it's before a form submission?我知道使用set_value()来设置CodeIgniter表单中的默认值,但是如果值未提交或者在表单提交之前,我如何设置验证的默认值?
Simply check if the value exists and use it as the default, otherwise it's blank (or a different default).
只需检查值是否存在并将其用作默认值,否则为空(或默认值不同)。
In the Controller, decide if you're going to load a blank form, if not, send the data for the fields....
在Controller中,决定是否要加载空白表单,如果没有,则发送字段的数据....
$data['fieldname'] = "whatever"; // from the database
$this->load->view('yourpage', $data);
Then in your View, check for the existence of this data for each field. If the data was sent, use it. Otherwise, set a blank value.
然后在您的视图中,检查每个字段是否存在此数据。如果数据已发送,请使用它。否则,请设置空值。
<?php $value = isset($fieldname) ? $fieldname : ''; ?>
<input name="fieldname" value="<?php echo set_value('fieldname', $value); ?>" type="text" />
-
If you do not send the data from the Controller, the field will be blank (you could also set a default)
如果您不从Controller发送数据,该字段将为空(您也可以设置默认值)
-
If you send the data from the Controller, the field will be filled out with this data from your Controller (database).
如果从Controller发送数据,则将使用Controller(数据库)中的数据填写该字段。
-
If you submit the form and validation fails,
set_value()
function will reload the field with the data from the most recent post array.如果您提交表单并且验证失败,则set_value()函数将使用最新的post数组中的数据重新加载字段。
#2
These are just some thoughts...
这些只是一些想法......
The validation rules act upon the posted data. So if you are using set_value('order',$default_order)
, when the form is submitted it will either take the new user entered value or the one you provided.
验证规则对发布的数据起作用。因此,如果您使用set_value('order',$ default_order),则在提交表单时,它将获取新用户输入的值或您提供的值。
If the user empties a prefilled or default input, you can't have it set as "required" in the rules. What you would do is use a callback function to handle that case to check if it's empty and provide a default value and return TRUE.
如果用户清空预填充或默认输入,则不能将其设置为规则中的“必需”。您要做的是使用回调函数来处理该情况以检查它是否为空并提供默认值并返回TRUE。
#1
I know to use
set_value()
for setting default values in a CodeIgniter form, but how can I set the default values for the validation if either the value isn't submitted or it's before a form submission?我知道使用set_value()来设置CodeIgniter表单中的默认值,但是如果值未提交或者在表单提交之前,我如何设置验证的默认值?
Simply check if the value exists and use it as the default, otherwise it's blank (or a different default).
只需检查值是否存在并将其用作默认值,否则为空(或默认值不同)。
In the Controller, decide if you're going to load a blank form, if not, send the data for the fields....
在Controller中,决定是否要加载空白表单,如果没有,则发送字段的数据....
$data['fieldname'] = "whatever"; // from the database
$this->load->view('yourpage', $data);
Then in your View, check for the existence of this data for each field. If the data was sent, use it. Otherwise, set a blank value.
然后在您的视图中,检查每个字段是否存在此数据。如果数据已发送,请使用它。否则,请设置空值。
<?php $value = isset($fieldname) ? $fieldname : ''; ?>
<input name="fieldname" value="<?php echo set_value('fieldname', $value); ?>" type="text" />
-
If you do not send the data from the Controller, the field will be blank (you could also set a default)
如果您不从Controller发送数据,该字段将为空(您也可以设置默认值)
-
If you send the data from the Controller, the field will be filled out with this data from your Controller (database).
如果从Controller发送数据,则将使用Controller(数据库)中的数据填写该字段。
-
If you submit the form and validation fails,
set_value()
function will reload the field with the data from the most recent post array.如果您提交表单并且验证失败,则set_value()函数将使用最新的post数组中的数据重新加载字段。
#2
These are just some thoughts...
这些只是一些想法......
The validation rules act upon the posted data. So if you are using set_value('order',$default_order)
, when the form is submitted it will either take the new user entered value or the one you provided.
验证规则对发布的数据起作用。因此,如果您使用set_value('order',$ default_order),则在提交表单时,它将获取新用户输入的值或您提供的值。
If the user empties a prefilled or default input, you can't have it set as "required" in the rules. What you would do is use a callback function to handle that case to check if it's empty and provide a default value and return TRUE.
如果用户清空预填充或默认输入,则不能将其设置为规则中的“必需”。您要做的是使用回调函数来处理该情况以检查它是否为空并提供默认值并返回TRUE。