CakePhp: Form validation on several fields? - php

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.'
)
);

Related

CakePHP Number Validation

Is there any cakephp validation for numbers only. That input field should only accept Numbers whether its float OR integer.
I have this field validation in model, But I don't know what rule to use.
'payment_commission' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
Any help is much appreciated..
You want to use numeric to test for valid numbers:-
'payment_commission' => array(
'required' => array(
'rule' => 'notEmpty', // use notBlank as of CakePHP 2.7
'message' => 'Required.'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Numbers only'
)
)
You can alternatively use decimal to check for floats or naturalNumber for natural numbers. Full details can be found in the offical docs.
You can also make sure that the input field only allows for numeric values:-
<?= $this->Form->input('payment_commission', ['type' => 'number']) ?>
This has the added advantage that on some mobile devices the keyboard will switch to a number pad for easier input. It's important to ensure that the server-side validation (the Cake part) is in place though as there are ways for people to get round client-side validation.
You can try below cakePHP code for number validation.
'payment_commission' => array(
'numeric' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Required.'
)
)
)
You can use below html pattern code in for your textbox.
<input type="text" name="payment_commission" pattern="[0-9]{10}" title="Three letter country code">
Here you can manage your pattern by changing [0-9]{10}. ([0-9] = expected integers from User, {10} = length).
You can use following rule to validate numbers(float and integer):
'payment_commission' => array(
'numeric' => array(
'rule' => array('decimal', 2),
'message' => 'Please enter only numbers',
'allowEmpty' => false,
'required' => true,
)
),
Let me know if this works for you.

CakePHP show custom validation message on empty fields

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.

Changing validation on the fly in CakePHP

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'
));

cakephp plugin data validation

I have a form on my homepage from an element in my Leads plugin. I have this in my Lead model:
public $validate = array(
'last_name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
),
),
'phone' => array(
'rocket' => array(
'rule' => array('phone', null, 'us')
),
),
);
My app is validating correctly because it's not posting to my database unless I fill in required fields. The problem is that the because my url isn't /leads/add it's not showing my custom error messages on my fields when I submit the form.
I think it has something to do with the fact that I'm using an element. Anyone have any ideas or had trouble with this before?
The rule should be notEmpty instead of notempty. What is working right now is the allowEmpty set to false which makes sure the field is not empty using !empty($field) || is_numeric($field).

cakephp show validation errors with jquery

I'm using cakephp 2.0 and I'm having problem using Cake's validation thing, which works wonderful when used for it alone.
var $validate = array(
'loginname' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 3),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
),
'alphaNumericRule' => array(
'rule' => 'alphaNumeric',
),
'uniqueRule' => array(
'rule' => 'isUnique',
),
),
'password' => array(
'minCharactersRule' => array(
'rule' => array('minLength', 5),
'required' => true,
'allowEmpty' => false,
),
// on so on ...
Due to Internalization I put the error messages into the view like this:
echo $this->Form->input('display_name', array(
// some other options ...
'error' => array(
'betweenRule' => __('Your Dispay Name must contain at least 3 characters and should be maximal 20 characters long.'),
'uniqueRule' => __('This Display Name is already in use!'),
),
);
Now I want jQuery to validate the form on client-side as well with some little - sexy message box to return validation errors if something goes wrong.
The Ajax Request works fine but the problem here is: I only get returned the name of the validation rule, not the message.
sure: Because I didn't specified any messages in the model.
But how can I get those messages I declared in the view?
Is there any solution without having to type everything twice (PHP and JS)?

Categories