I've just started a little page using CodeIgniter and wanted to run CodeIgniter's form validation magic tricks. For this, I've set some rules via config/form_validation.php:
$config = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required|max_length[64]'
)
);
But in addition to that, I wanted to set some specific rules inside the controller itself.
$this->form_validation->set_rules('name', 'Name', ' is_unique[table.name]');
My problem - the specific set_rules() seems to have reset all previously defined rules.
Is there a way to merge both set of rules? Or did I miss a method for that?
I've had this exact issue before - where I wanted to use one set of rules, but add one extra rule for a specific controller.
Unfortunately you are correct - and the form_validation will overwrite the old rules. You cant even call the variable containing the old rules from the config - because its not stored in an accessible format.
The way I did the workout was to define the rules in a generic config file as arrays - and load the arrays inside the controller, then append a new rule, then set the whole array as the ruleset.
The other option is to just define two different rulesets inside the config file (even though they might be almost identical) - and just call the different rulesets as required.
It is better to defined named array in the config file for each controller and use it as mentioned in the Codeginiter user guide.
$config = array(
'signup' => array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required'
),
array(
'field' => 'passconf',
'label' => 'PasswordConfirmation',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
)
),
'email' => array(
array(
'field' => 'emailaddress',
'label' => 'EmailAddress',
'rules' => 'required|valid_email'
),
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'required|alpha'
),
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required'
),
array(
'field' => 'message',
'label' => 'MessageBody',
'rules' => 'required'
)
)
);
Call it like $this->form_validation->run('signup') with the name of the array.
I can't say I have a lot of experience with CI however, as far as I know you should be able to append additional rules. Failing that consider trying:
$config[] = array('name', 'Name', ' is_unique[erfolge.name]');
$this->form_validation->set_rules($config);
or use array_merge if you don't want to modify your standard configuration.
Related
In Backpack CMS framework I have created a table using migration fields like : 'parent_id' and 'is_active'.
I have added these fields in the crud controller using the following command:
$this->crud->addFields(array(
['name' => 'title',
'label' => 'Title',
'type'=>'text'],
['name' => 'parent_id',
'label' => 'Parent ID',
'type'=> 'number'],
['name' => 'is_active',
'label' => 'Is Active',
'type'=>'number']
), 'update/create/both');
It should work in edit mode and create mode like my other tables. It shows the defined fields in the create and update form but unfortunately they doesn't work and always return the default value or previous value in the record.
I've tried adding the field in single format but there was no use.
The problem is here: 'update/create/both' you should pick only one of the three options.
What you want is to use only both. But since it is the default value, you don't really need to add it to the end of the addFields function.
This will work:
$this->crud->addFields([[
'name' => 'title',
'label' => 'Title',
'type' => 'text'
], [
'name' => 'parent_id',
'label' => 'Parent ID',
'type' => 'number'
], [
'name' => 'is_active',
'label' => 'Is Active',
'type' => 'number'
]
]);
The problem was with the fillable variable..... Only the 'title' field was in the fillable variable of the related model.
I'm using Codeigniter 3 validations for creating new users. Some of these validations consist of:
...
array(
'field' => 'cpf_cnpj',
'label' => 'CPF',
'rules' => 'required|_validate_cpf|is_unique[usuario.cpf_cnpj]'
),
array(
'field' => 'email',
'label' => 'E-MAIL',
'rules' => 'required|valid_email|is_unique[usuario.email]'
),
...
The issue is that when editing an existing User, if the e-mails provided during the creation is kept the same, the validation will fail. How to workaround this?
I have a form in magento admin panel. In the form i have checkboxes which i can select multiple options or one. The issue is i am unable to put validations for that. Because without selecting any option i can save records. My code is as in below:
$fieldset-> addField('time_ranges', 'checkboxes', array(
'label' => Mage::helper('CheckoutTime')->__('Time Ranges'),
'required' => true,
'class' => 'required-entry',
'name' => 'time_ranges[]',
'values' => array(
array(
'label' => Mage::helper('CheckoutTime')->__('Education'),
'value' => 'education',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Business'),
'value' => 'business',
),
array(
'label' => Mage::helper('CheckoutTime')->__('Marketing'),
'value' => 'marketing',
),
array(
'value' => 'investment',
'label' => Mage::helper('CheckoutTime')->__('Investment'),
)
),
));
Can anyone please tell me how to add validations into this form.
Thank You
Try by changing
'name' => 'time_ranges[]'
to
'name' => 'time_ranges'
This way is correct. There were some issues in other places in my coding. That's why it didn't work early. Or else this is the correct way to do that.
I have defined a validation rule like
protected $add_rules = array(
'add_question' => array(
'field' => 'add_question',
'label' => 'lang:topic.add_question_error',
'rules' => 'trim|required'
)
);
I have to add extra rules in this class if user is not logged on like
if(!isset($this->current_user->id)){
'username' => array(
'field' => 'username',
'label' => 'lang:topic.username',
'rules' => 'trim|required'
)
}
How can i add this second rule within the first protected class?
If you want to add it to the $add_rules array you need to do
if(!isset($this->current_user->id)) {
$this->add_rules['username'] = array(
'field' => 'username',
'label' => 'lang:topic.username',
'rules' => 'trim|required'
);
}
Because it is a protected property you will only be able to manipulate it in the same class or any class that extends it
I have a model ex. posts that acts as translate and I want to validate the title which is a translated field.
How is this possible. The below does not work.
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
),
));
Change rule name to camelCase: notEmpty and add message
you translate your message in /Locale default.po
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'your translated message',
),
));
whats the big deal?