I have a little checkbox on a signup form im creating which 'must' be checked before the user is allowed to continue ... Im finding it difficult to figure out how to do this with the form_validation functions as well, basically the 'agree to terms and conditions' checkbox MUST be checked in order for the user to continue, if not, an error message displayed, the code i have at the moment is below, if someone could give me a bit of a helping hand that would be great.
Ok, in my view i have the following
$agreeCheck = array( 'name' => 'agreeCheck', 'id' => 'agreeCheck', 'value' => 'agree', 'checked' => set_checkbox('agreeCheck', 'agree', FALSE));
<?php echo form_checkbox($agreeCheck); ?>
and then in my controller i have the following
$this->form_validation->set_rules('agreeCheck', 'Agree to the Terms and Conditions', 'required');
At the moment, it only remembers the value that was clicked if there is a submission, except if its not checked, it doesnt return anything.
Try this out:
$this->form_validation->set_rules('agreeCheck', 'Agree to the Terms and Conditions', 'required|isset');
Try this out:
$this->form_validation->set_rules('agreeCheck', '...', 'callback_terms_check');
And then set up this method in the controller:
function terms_check() {
if (isset($_POST['agreeCheck'])) return true;
$this->form_validation->set_message('terms_check', 'THIS IS SOOOOO REQUIRED, DUDE!');
return false;
}
Related
this is my form_validation.php
$config = array(
'buy' =>array(
array(
'field' => 'id',
'label' => 'Item',
'rules' => 'trim|required'
),
array(
'field' => 'qty',
'label' => 'Quantity',
'rules' => 'trim|greater_than[0]|callback_validate_qty'
)
)
);
My Controller to load form validation :
$this->load->library('form_validation');
Function validate_qty($qty)
$item_id = $this->input->post('id');
$total_item= $this->item_model->getTotalItem($item_id);
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return FALSE;
}
this validation is work perfectly, but someone has got throught it, he buy more than maximum stock..
How to prevent this? I don't know how he does that, but it looks like the validation didn't work for him. When I tested, it works perfectly, I don't know what's wrong with it, maybe he uses software or anything? is there any solution? Thanks
i think your code is correct.
i have one doubt on that code.
check the variable you used in if condition is correct or not ($qty). becuase you not show the full code.i don't known how that variable comes.
Also change the code like if and else condition.
if ($qty >= $total_item) {
$this->form_validation->set_message('validate_qty', 'Cant Buy More than maximum stock!');
return false;
} else {
return true;
}
Assume if you have 30 stock in your hand and you want to get all 30 stocks out. But according to your if condition (if ($qty >= $total_item) {) it throws error.
Fix 1
if should be > (if ($qty > $total_item) {).
Fix 2
Typo - in your callback function you've used two underscores _. Get rid of one
callback__validate_qty
^^
Read more about codeigniter.com - Callbacks
I have modified my model so that the data displayed in cgridview is unique per user, depending on the account type...
However I need to create a form from another model where I could get the data from the cgridview via dropdown...
I used this code at first...
<?php
$this->widget('ext.select2.ESelect2',array(
'model'=>$model,
'attribute'=>'pr_id',
'data'=>$model->searchPatient(),//function to provide data
// or
//'data'=>CHtml::listData(PatientRecord::model()->findAll(), 'id', 'first_name')
);
?>
but it returns all of the contents of the PatientRecord model, I tried using a condition before planning to retrieve the contents from the cgridview...
$doctor= Yii::app()->user->id;
CHtml::listData(PatientRecord::model()->findAll( array(
'condition'=>'doctor_id=:doctor_id',
'params' => array(':doctor_id' => $doctor)
)
);), 'id', 'first_name')
it didn't have an error but it didn't display anything on the dropdown either...
any suggestions?
I think the problem is with a ; and ) in your model code, try this:
$doctor= Yii::app()->user->id;
CHtml::listData(PatientRecord::model()->findAll( array(
'condition'=>'doctor_id=:doctor_id',
'params' => array(':doctor_id' => $doctor)
)
), 'id', 'first_name');
You should always enable error logging in local environment, this will help you find any errors in your code. Here is a link on how to enable error logging.
Hope that helps :)
I know this is a stupid question but after messing around, I am confused what is the right way of doing.
Below are my code :
View registration_form.php :
$input_data = array(
'name' => 'user_name',
'id' => 'user_name',
'value' => set_value('user_name'),
'maxlength' => MAX_CHARS_4_USERNAME
);
Controller register.php:
$this->load->model('user_model');
$this->user_model->create_user( 'customer', array() );
Form Validation Config :
$config['customer_creation_rules'] = array(
array(
'field' => 'user_name',
'label' => 'USERNAME',
'rules' => 'trim|required|alpha|strtolower'
)
);
Model user_model.php :
public function create_user( $role, $insert_array = array() )
{
// The form validation class doesn't allow for multiple config files, so we do it the old fashion way
$this->config->load( 'form_validation/administration/create_user/create_' . $role, TRUE );
$this->validation_rules = config_item( $role . '_creation_rules' );
$form_username = $this->config->item($role . '_creation_rules', 'field');
echo $form_username;
}
What I want to do right now is to check the username that the user input and auto add a number to the input username before inserting into database.
Initially I thought of getting the inputted username from the Form Validation, after messing around, I can't get the value no matter what I have tried.
Am I doing it wrongly? Do I just get from $_POST instead?
Hope you guys can help me out on this. Thanks in advance!
This shows the full list (waaaay at the bottom): http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
I don't see that one there, but it says you can use any PHP function that requires only one parameter so that should work, but you'll have to look deeper into the docs to figure out why it isn't.
I don't think you can set rules for a form, you have to set them per variable, as the docs show:
$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.
So the framework is CodeIgniter 2.0.2. I have a form that has groups of fields that correspond to rows in a database. The names of the fields are in the format:
opt[0][foo]
opt[0][bar]
opt[1][foo]
opt[1][bar]
etc...
The index (1,2,etc...) does not correspond to row IDs in the database, it is simply a way to split up the groups of fields. There may be gaps in the index as users are able to add and remove an arbitrary number of the field groups. All groups are identical, that is, they contain exactly the same set of fields with the same second level names.
I want to be able to use CodeIgniter's validation library to validate the form and (p)re-populate as necessary. I've found plenty of posts (in addition to the excellent CI user guide) on the pre-populating and I know how to get the working with the re-populating in general. However, this is the first time I've had to try it with the indexed field names as above. I've tried the below and it doesn't work:
array(
'field' => 'opt[][foo]',
'label' => 'Foo',
'rules' => 'required'
)
I'm guessing I was just hoping for too much and CodeIgniter doesn't support what I need it to do. Extending the existing form validation library is an option so if anyone has been in the same situation and can provide some tips that would be very welcome.
UPDATE:
Just a little extra info, I've also tried validating a specifically indexed field (see below) and that also didn't work... As I understand it multidimensional validation should work in the specific case:
array(
'field' => 'opt[0][foo]',
'label' => 'Foo',
'rules' => 'required'
)
The following controller code works for me on CI 2.0.2
public function test() {
$this->load->library('form_validation');
$this->load->helper('form');
$this->form_validation->set_rules('test[test1][test2]', 'Test', 'required|valid_email');
$this->form_validation->run();
echo validation_errors();
echo form_open($this->uri->uri_string());
echo form_input('test[test1][test2]', set_value('test[test1][test2]'));
echo form_submit();
echo form_close();
}
You can use this to loop through the opt variable and set validation rules for each input.
if(!empty($opt))
{
foreach($opt as $id => $value)
{
$this->form_validation->set_rules('opt[' . $id . '][foo]', 'Foo', 'required');
$this->form_validation->set_rules('opt[' . $id . '][bar]', 'Bar', 'required');
}
}
You should take a look at the callback functions for the validating class - this should help you accomplish what you need for validation.