I am having an issue trying to display the form validation error on the view using a callback validation function. If the user selects 'Yes' for the vehicle option, the form should check for other mandatory fields related to that vehicle option field.
In my controller I have a simple function that outputs the form and gets data back from the form:
function new_customer_record()
{
if ($this->form_validation->run() == FALSE)
{
// data array has values to be passed to the customer_form_view
$this->load->view('customer_form_view',$data);
}else{
//get data from the post method and save it to the database
}
}
In the config folder, I have a file called from_validation.php that has the array filled with validation rules and the callback function as shown below:
$config = array (
'employee/new_record' => array(
array (
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'required',
),
array (
'field' => 'vehicleOwn',
'label' => 'Own a vehicle',
'rules' => 'required|callback_checkVehicleInfo',
),
),
);
function checkVehicleInfo($str){
if ($str == "Yes"){
$config = array ( 'employee/new_record' => array(
array (
'field' => 'vehicle_model',
'label' => 'Vehicle Model',
'rules' => 'required'
),
array (
'field' => 'vehicle_rego',
'label' => 'Vehicle Rego',
'rules' => 'required'
),
array (
'field' => 'vehicle_type',
'label' => 'Vehicle Type',
'rules' => 'required'
),
),
);
$this->form_validation->set_message('checkVehicleInfo','Please enter your vehicle information');
return FALSE;
}else{
return TRUE;
}
}
In the view, i have something similar to this for each field that has 'required' validation rule - other fields work fine except the vehicle mandatory fields (in the callback function):
<?php $vehicle_model = #field($this->validation->vehicle_model, $customer- >vehicle_model); ?>
<tr>
<td><label>Vehicle Model</label></td>
<td><input name="vehicle_model" type="text" value="<?php echo ($edit=== true)? $vehicle_model :set_value('vehicle_model'); ?>" size="20" maxlength="20">
<?php echo form_error('vehicle_model'); ?></td>
</tr>
But I am not getting any error messages displayed, the validations is not working either.
Right after new_customer_record() function, you should add the callback function (not in config, but put checkVehicleInfo function in your controller).
Related
I'm new to CakePHP. I'm validating my form, but the problem is that no validation is working except the not empty validation.
My model file is:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'rule_name' => array(
'alphaNumeric' => array(
'rule' => 'Numeric',
'required' => true,
'message' => 'Letters and numbers only'
)
)
);
}
My View file is:
echo $this->Form->create('User');
echo $this->Form->input('rule_name',array('class'=>'form-control','autocomplete'=>'off'));
Please suggest how I can fix this.
General usage pattern adding a rule for a single field:
public $validate = array(
'fieldName1' => array(
'rule' => 'ruleName',
'required' => true,
'allowEmpty' => false,
'message' => 'Your Error Message'
)
);
Must be look at Data Validation in cakePHP
I have a Job model where I want to submit a form to my other action (not to save the data directly into the db just to pass it to my other action) and I should check whether the user has selected at least 1 checkbox.
Here is my view's snippet:
echo '<tr class="v-top">
<td colspan=2>'.$this->Form->input('system_codes_filter', array(
'type' => 'select',
'multiple' => 'checkbox',
'label' => '',
'name' => 'system.codes.filter',
'class' => 'floatLeft',
'options' => array('System codes' => $this->Submit->getSystemCodes($systemcodes)),
'selected' => array('*'))).
'</td></tr>';
Here is my model's snippet:
public $validate = array(
'system_codes_filter' => array(
'rule' => array('multiple', array(
'min' => 1,
'message' => 'Please select at least on system code!'
)
)
)
);
As far as I know the validation is called when I want to use $this->Job->save(), but I don't use this in my action.
Unfortunately this does not work. When I submit my form without selecting any checkboxes, it let's me do that and if I look at the $this->validationErrors array, it is empty. What am I doing wrong? Thanks in advance.
I have a form with multi select box:
echo $this->Form->input('emp_id', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'style' => 'width:210px; height:125px;'
));
I selected multiple values from this list box and click the SAVE button.
But it displays the validation message.
How can i solve this?
class TravancoDSRGroup extends AppModel {
var $name = 'TravancoDSRGroup';
var $useTable = 'dsr_group'; // This model uses a database table 'exmp'
var $validate = array(
'emp_id' => array(
'rule' => 'notEmpty',
'message' => 'The employee field is required'
)
);
}
This is the model code....
If it is possible...?
You don't have to explicitly specify
'type' => 'select'
if you set 'emps' from the controller, it will allow cake's automagic to work. just add
$this->set(compact('emps'));
Post output of debug($this->request->data) for better understanding of problem.
Have you defined hasMany or HABTM relationships to the emp ? To have multiple values saved you will have to define hasMany or HABTM relations, if you wish to save it as CSV then you will have to do the processing yourself in 'beforeSave'.
I've been struggling with this for the last hour or so, wondering if some fresh eyes can help.
Model
class User extends AppModel {
public $name = 'User';
public $validate = array(
'email' => array(
'valid' => array(
'rule' => 'email',
'message' => 'The email is not valid'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter an email'
)
)
);
}
Controller
class UserController extends AppController {
var $uses = array('User');
function index(){
$users = $this->User->find('all');
$this->set(compact('users'));
}
public function add() {
$this->set('title_for_layout', 'Add new user');
if(isset($this->data) && !empty($this->data)) {
$this->User->set($this->data);
$this->log($this->User->invalidFields(), "debug");
if($this->User->validates()){
if ($this->User->save($this->data)) {
$this->Session->setFlash("Added " . $this->data['User']['name']);
$this->redirect('index');
}
} else {
$this->Session->setFlash('There are errors with your form submit, please see below.');
}
}
}
}
View
<?php
echo $this->Form->create('User');
echo $this->Form->input('name', array('label' => 'Name'));
echo "<div class='clear'></div>";
echo $this->Form->input('email', array('label' => 'Email'));
echo "<div class='clear'></div>";
echo $this->Form->button('Reset', array('type' => 'reset'));
echo $this->Form->button('Add Useer', array('type' => 'submit'));
echo $this->Form->end();
?>
But I never get invalid fields for email? Have I missed something glaring?
If it makes any difference, this is a plugin Im developing so it doesnt sit directly in app/ but in app/Plugins
Thanks
EDIT: So I've been struggling with this for a while now, and still no joy. One thing I have noticed though, when I print out the model details (using var_dump($this->User) ), the [validate] array is empty. For example:
[validate] => Array
(
)
[validationErrors] => Array
(
)
Im presuming this is what the issue is, even though I have declared my $validate array, its somehow being overwritten? Anyone come across this before? Any solutions?
public $validate = array(
'email' => array(
'valid' => array(
'rule' => array('email'),
'message' => 'The email is not valid'
),
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter an email',
'allowEmpty' => false
)
)
);
Try adding rules as array and adding 'allowEmpty' key set to false on the required validation.
Damn! So simple. If I read the cookbook properly at http://book.cakephp.org/1.3/en/view/1114/Plugin-Models it would have told me that
If you need to reference a model within your plugin, you need to include the plugin name with the model name, separated with a dot.
Thus..
var $uses = array('Plugin.User');
works.. Hope this helps someone else!
If the first field called sendEmail has value ="Yes" the rest of the fields are to be validated...
I have this set_rules defined in the form_validation.php file in the config folder..
$config = array(
array(
'field' => 'sendEmail',
'label' => 'Send Email',
'rules' => 'required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required'
),
array(
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'required'
),
array(
'field' => 'last_name',
'label' => 'Last Name',
'rules' => 'required'
)
);
but email, first_name and last_name fields are to be validated only if sendEmail has value="Yes".
Not sure how to do this, can someone please help me with this?
thanks
Do the conditions like this in your controller:
if ($this->input->post('sendEmail') == 'Yes'){
$this->form_validation->set_rules('email', 'Email', 'required');
... [etc]
}
The validation only runs when you call $this->form_validation->run(), so just write an if statement before that. Something like this:
if ($this->input->post("sendEmail") == 'Yes') {
$this->form_validation->set_rules($config);
if ($this->form_validation->run() == FALSE)
{
// failed validated
}
else
{
// passed validation
}
} else {
// never attempted validation
}