I'm using CodeIgniter 2 with DataMapper ORM.
For Users, I have confirm_password and confirm_email fields (plus others) which both aren't fields in the database (table users does not have these fields), but it's just there to show on the sign-up form:
I also have back-end where these 2 fields (confirm_password and confirm_email) do not exist in the form.
public $validation = array(
'first_name' => array(
'label' => 'lang:common_first_name',
'rules' => array('required', 'trim')
),
'last_name' => array(
'label' => 'lang:common_last_name',
'rules' => array('trim')
),
'email' => array(
'label' => 'lang:common_email',
'rules' => array('required', 'trim', 'unique', 'valid_email')
),
'confirm_email' => array(
'label' => 'lang:common_confirm_email',
'rules' => array('matches' => 'email')
),
'password' => array(
'label' => 'lang:common_password',
'rules' => array('required', 'min_length' => 6, 'encrypt')
),
'confirm_password' => array(
'label' => 'lang:common_confirm_password',
'rules' => array('matches' => 'password')
)
);
If I don't make the confirm_email or confirm_email fields required, the validator won't trigger the matches rule.
If I make them required, then the back-end that does not have these fields, triggers the confirm_email and confirm_password, but it shouldn't.
Is it best to include ALL possible validation rules (in the model of course) that we may have in the application?
Is it a good idea to alter these rules in the controller (say remove
confirm_email index from $validation array) when adding user on
back-end?
I appreciate any thoughts.
Thanks
Do you have the latest version of ORM Mapper, according to documentation you can add non database fields.
http://stensi.com/datamapper/pages/validation.html
Also, you can now add validation rules for non-Database Table fields,
such as 'Confirm Email Address' or 'Confirm Password'. For example:
and you don't need required rule for the confirmation fields - as they indicate that filed is required in DB hence the DB error) , - the matches property will do the required validation against the matched field (e.g. if it dosen't match it will throw the error.) In other words you only need required on 'email' field, confirmation_email will throw the error if filed don't match. On empty field you really need to show that email is required.
Finally - you can remove the index, but generally thats not good idea. I would , instead, if above fails - add the form validation rule in controller.
Related
I have form consists of 28 fields (client requirement).
I am using Codeigniter framework. I have to type 28 lines of code for form validation.
Is there anything else I can do like creating helper for form validation.
Any help would be appreciated.
Thanks
There is no standard here or a right way to do it and your best option is to create a validation rule for every single input of them in your validation array and maybe you can make a check to categorize some of them like "fname, lname, ..." those have the same validations, but that won't save you anything just added overhead.
But maybe your best shot is to create a function in your base_controller like this:
function set_validation_rules()
{
$this->load->library('form_validation');
$config = array(
array(
'field' => 'firstname',
'label' => $this->lang->line($line.'firstname'),
'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
),
array(
'field' => 'lastname',
'label' => $this->lang->line($line.'lastname'),
'rules' => 'trim|required|alpha|min_length[3]|max_length[15]'
),
array(
'field' => 'password',
'label' => $this->lang->line($line.'password'),
'rules' => 'trim|required|min_length['.$min.']|max_length['.$max.']'
),
array(
'field' => 'passconf',
'label' => $this->lang->line($line.'password_confirm'),
'rules' => 'trim|matches[password]'
)
);
$this->form_validation->set_rules($config);
}
or create a helper as you suggested, but the most important step is to make a convention for yourself for var/inputs naming and make it a habit so inputs from different view have the same names and pass all inputs to this function for validation after modifying by adding a switch cases to it so every firstname has its own rules and every password has its own and so on, hope you got the idea.
I'm searching a way to show custom message declared on validation array inside a notEmpty rule.
So if i have this validation:
'username' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'username empty',
'required' => true
),
//other validation rules
if i leave e filed empty into form, cake php show his own default message for empty field, and not my custom message. How can i show it and catch this event into my Controller class?
The key to the array should be the fieldname, unless I'm misunderstanding your context:
'myFieldName' => array(
'rule' => 'notEmpty',
'message' => 'custom message for empty field',
'required' => true
)
If I've misunderstood the context, please edit your question giving a little bigger picture of the validation code in your model.
I'm using CakePHP 2.3.8 and I'm trying to figure out if there's a way to set certain validation rules to required on the fly.
For example, my User model has phone_number, username, email, and password validation. If a user wants to change their username, their phone number isn't required to do so. That means I can't set it to required, because then when changing a username, the phone_number will be expected to be present in the data.
public $validate = array(
'username' => array(
'minLength' => array(
'rule' => array('minLength', '3'),
'message' => 'A username with a minimum length of 3 characters is required'
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This username has already been taken.'
)
),
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Please enter a valid email address.',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This email address is already in use'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
),
'phone_number' => array(
'rule' => array('valid_phone'),
'message' => 'Invalid phone number',
)
);
To get around this problem, in my controller for the corresponding action what I've been doing is checking to make sure the expected inputs have been posted. If not, set that index to null so that it is validated...such as
public function change_username(){
if(!isset($this->request->data['username'])){
$this->request->data['username'] = null;
}
$this->ExampleModel->set($this->request->data);
//if it wasn't posted, the username index will be created but set to null. This is my workaround for setting something to "required"
if($this->ExampleModel->validates() == true){
//do something
}
else{
//do something
}
}
While this works, I feel like it makes for a lot of extra coding, especially if I have a form that has a lot of inputs.
I've also tried to validate only the inputs that I need, but unless the data was posted, it ignores them. For example
if($this->ExampleModel->validates(array('fieldList' => array('phone')) == true){
.....
}
If "phone" wasn't posted, it doesn't validate it.
Is there any way to set required for a given input's validation to true on the fly? I found this article on using multiple validation rulesets and while it would accomplish what I want, there would be a lot of re-coding involved.
Before validation, can I set an input to required?
Firstly, in your Model validation rules you have phone_number but yet trying to validate phone, there aren't validation rules for phone.
It would be ideal request->data[] to match model fields, you can rebuild an array etc.
From book.cakephp:
This will add a single rule to the password field in the model. You can chain multiple calls to add to create as many rules as you like:
$this->validator()
->add('password', 'required', array(
'rule' => 'notEmpty',
'required' => 'create'
))
->add('password', 'size', array(
'rule' => array('between', 8, 20),
'message' => 'Password should be at least 8 chars long'
));
I'm trying to implement change password functionality in Symfony 2 project.
I have entity User with validation rules in validation.yml file. In User entity I have field "password" with its validation constraints in validation.yml.
I created form with 2 field 'password' and 'confirmPasswod'. I want to use my entity validation constraints for 'password' field and check equality between 'passwod' and 'confirmPassword' fields. In my contronller I write
$form = $this->createForm(new SymfonyForm\ChangePasswordType(), new Entity\User());
if ($form->isValid())
{..............}
In 'User' entity I don't have 'confirmPasswod' field. So I get error:
Neither property "confirmPassword" nor method "getConfirmPassword()" nor method "isConfirmPassword()" exists in class
Is there any way to use entity-based form validation for some form fields and not entity-based validation for other?
Thanks in advance.
In SymfonyForm\ChangePasswordType you can use something like this:
$builder->add('password', 'repeated', array(
'type' => 'password',
'first_name' => 'Password',
'second_name' => 'Password confirmation',
'invalid_message' => 'Passwords are not the same',
));
Since Symfony 2.1 you can configure options to avoid broken element name (as mentioned in comment)
$builder->add('password', 'repeated', array(
// … the same as before
'first_name' => 'passwd',
'second_name' => 'passwd_confirm',
// new since 2.1
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Password confirmation'),
));
I've some conditions to a form to be valid that has to be on several fields instead one, how to do this.
Some example for a registration:
enterprise or firstName+lastName filled
mobile phone number OR static phone number filled
How to do this? Is there an implemented way or I've to do it myself every time?
Thank you
Write your own validation rules. Cake Book: Custom validation rules
Attach a rule to the enterprise field what checks if it is filled or the first, last names are filled. Attach another rule to the name fields to check if the names or the enterprise fields are filled. Similar to the phone fields. You are in the model so you can reach all passed fields in $this->data
I am not sure if I understand the question correctly but you can create your own validation rule and then apply it for the desired fields (not really the other way around). See here
Otherwise Cakephp has a lot of pre-built validation rules here is an example:
var $validate = array(
'title' => array(
'titleRule1' => array (
'rule' => array('minLength', 1),
'required' => true,
'allowEmpty' => false,
'last' => true,
'message' => 'Please enter a title.'
),
'titleRule2' => array(
'rule' => array('between', 1, 100),
'message' => 'Your title must be between 1 and 100 characters long.'
)
),
'description' => array(
'rule' => array('minLength', 1),
'required' => true,
'allowEmpty' => false,
'last' => true,
'message' => 'Please write a description.'
)
);