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?
Related
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 can successfully put new messages in view files only if they are not present in model. I mean validations have to be in model, but messages attached to those validations should not be there.
Example model
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'required' => true, 'allowEmpty' => false,
'message' => 'Please enter a username.'),
'alpha' => array(
'rule' => array('alphaNumeric'),
'message' => 'The username must be alphanumeric.'),
'unique_username' => array(
'rule'=>array('isUnique', 'username'),
'message' => 'This username is already in use.'),
'username_min' => array(
'rule' => array('minLength', '3'),
'message' => 'The username must have at least 3 characters.')),
'email' => array(
...................
...................
and here's an example of view
echo $this->Form->input('username',array(
'label' => __d('app_users','Username'),
'error' => array(
'required' => __d('app_users', 'Please enter a username'),
.....................
.....................
));
So, my question is how can one make it into a work without removing 'message' => 'Please enter a username.' in a model but to TRULY OVERRIDE IT.
I mean validations have to be in model, but messages attached to those validations should not be there.
And how do you then have messages for a JSON API service? The text contained in validation has nothing to do with how your site looks so should not be in a view.
You can change the validation message from the model or from custom validation methods without much hassle and its all in the book
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?
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.
How validate file upload in CakePHP?
When i wrote some like this:var $validate = array(
'file' => array(
'select' => array(
'rule' => array('selectFile'),
'message' => 'There is no file!'),
'type' => array(
'rule' => array('typeFile'),
'message' => 'Bad type!'),
'size' => array(
'rule' => array('sizeFile'),
'message' => 'Bad with size!')));
Works only with the last validate, here 'size'.
Maybe You know solution, for validation for files with many messages?
try adding required=true to each of the 3 rules