i want validate my form input field or you want to say i have an array and i want to validate that array using codeigniter
Example :
i have array like :
$array['obj_type']='sample';
$array['obj_id']='44';
$array['user_id']='34566';
and my form validation config as like :
'validatedata' => array(
array(
'field' => 'obj_type',
'label' => 'No Type Define here',
'rules' => 'required'
),
array(
'field' => 'obj_id',
'label' => 'No any item selected here',
'rules' => 'required|is_natural_no_zero'
),
array(
'field' => 'user_id',
'label' => 'No user logged in',
'rules' => 'required|is_natural_no_zero'
),
),
and when i use form validate its not validate array
if ($this->form_validation->run('validatedata')) {
} else {
echo validation_errors();
}
its print all error which define on on validatedata config array;
i just use
$this->form_validation->set_data($array);
then i validate form
if ($this->form_validation->run('validatedata')) {
echo "sucess";
} else {
echo validation_errors();
}
now its works fine and good.
You have to load form validation library in your controller..
$this->load->library(array('form_validation'));
You have to provide the data to the form_validation library:
$this->form_validation->set_data($array);
and then you can use
$this->form_validation->run('validatedata')
as intended.
If you want to validate multiple arrays, you'll have to call reset_validation() after validating each array.
Check system/libraries/Form_validation.php (around line 255, depending on your version of CI) for more information.
Related
I'm using CI 3.1.7 and want to stop validating form if there is an error. For example:
$config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required',
'errors' => array(
'required' => '%s is required',
),
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required',
'errors' => array(
'required' => '%s is required',
),
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required',
'errors' => array(
'required' => '%s is required',
),
)
);
When user leaves username and email blank, the form will show only username is required. Any help is appreciated, thank you!
You cannot stop validation->run() but you can control which error message displays. The limitation is you cannot show the error next to the related field. Or, I should say instead, I cannot think of an easy way to show the error next to the input it belongs to.
Anyway, here's how to extract the first error message.
if($this->form_validation->run() == FALSE)
{
$errors = $this->form_validation->error_array();
// There could be many but grab only the first
$fields = array_keys($errors);
$err_msg = $errors[$fields[0]];
}
If you want the name of the field you can use this.
$err_field = $fields[0];
If i got your question right .. you want to stop on the first encountered form validation error then you have to edit the core form validation library which is extremely bad practice but luckily you can extend its functionality and either edit the run() method itself or create your own method and just copy the run() code and edit this block of code:
// Execute validation rules
foreach ($this->_field_data as $field => &$row)
{
// Don't try to validate if we have no rules set
if (empty($row['rules']))
{
continue;
}
$this->_execute($row, $row['rules'], $row['postdata']);
// here is the modification
if(count($this->_error_array) > 0) return true; // error found
}
now it will stop execution when finding its first error
Pleas check
public function __construct()
{
parent::__construct();
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
Or check https://code.tutsplus.com/tutorials/codeigniter-form-validation-from-start-to-finish--cms-28768
I want to have one validation for two input
ex I have input agendaCode and agendaNumber
I want codeigniter check the concation value of both input at the same time so I will have code like
$this->form_validation->set_rules('agendaCode/agendaNumber','my_callback_function);
but its return error
i know the answer by using
$this->form_validation->set_rules('agendaCode','my_callback_function[agendaNumber]');
You can only pass one field name to the set_rules() method when doing it that way:
https://www.codeigniter.com/user_guide/libraries/form_validation.html#setting-validation-rules
However, you can pass an array:
https://www.codeigniter.com/user_guide/libraries/form_validation.html#setting-rules-using-an-array
So:
$config = array(
array(
'field' => 'agendaCode',
'label' => 'Agenda Code',
'rules' => 'callback_my_function'
),
array(
'field' => 'agendaNumber',
'label' => 'Agenda Number',
'rules' => 'callback_my_function'
)
);
$this->form_validation->set_rules($config);
I mnot sure if you can validate two input in the same statement but i can see why you get an error
you need to change
$this->form_validation->set_rules('agendaCode/agendaNumber','my_callback_function);
to
$this->form_validation->set_rules('agendaCode','callback_function);
$this->form_validation->set_rules('agendaNumber','callback_function);
the corrent statement is callback_functionname it has to be callback not my_callback or anotherthing else
reference For
I'm using this datamapper http://datamapper.wanwizard.eu
problem is datamapper have validation methods similar with codeigniter form validation.but not as same.
An example, a model admins model validation array:
public $validation = array(
'username' => array(
'rules' => array('unique', 'required', 'trim', 'max_length' => 60, 'min_length' => 3),
'label' => 'User'
),
'password' => array(
'rules' => array('required', 'trim', 'encrypt', 'min_length' => 6),
'label' => 'Password'
)
);
but form validation array must be like that:
public $form_validation = array(
array(
'field' => 'username',
'label' => 'User',
'rules' => 'unique|required|trim|max_length[60]|min_length[3]'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'required|trim|encrypt|min_length[6]'
)
);
I don't want to make two manual validation for new admin adding (first form validation, after datamapper validation). I think there is a way to make this with just one manual validation.
sorry my bad English, I hope you understand. Thanks in advance.
Using the Datamapper's validation alone should be enough, without the CI's form library.
When you try to save the model, the save() method will return a true or false depending on if the save was successful. If it isn't the model's error property should be filled with the error messages generated for the validation that failed. The messages can be loaded from language files with keys named appropriately, also the codeigniter's form validation library's form_validaton_lang.php is loaded too.
In your controller you could make use of them like this:
Class TheController extends CI_Controller {
function save() {
// get the model object somehow
// ...
// update attributes
$model->prop0 = $this->input->post('prop0');
$model->prop1 = $this->input->post('prop1');
// try to save it
if ($model->save()) {
// save successful
redirect(...);
} else {
// save failed load form again, with the model
$this->load->view('path/to/the/form', array('model' => $model));
}
}
}
The view could work like this:
<form method="post" action="...">
<label>prop0</label>
<input type="text" name="prop0" value="<?php print $model->prop0?> ">
<?php if (!empty($model->error->prop0)):?>
<div class="error"><?php print $model->error->prop1; ?></div>
<?php endif; ?>
<label>prop1</label>
<input type="text" name="prop1" value="<?php print $model->prop1?> ">
<?php if (!empty($model->error->prop0)):?>
<div class="error"><?php print $model->error->prop1; ?></div>
<?php endif; ?>
<buton type="submit">go</button>
</form>
The same form can be used when no previous model exists in the database, just create an empty instance of the model you need, and pass it to the form.
So I have a code here to validate the usernname exist or not, I'm using tank_auth library
if(! $this->tank_auth->is_username_available($username))
{
$this->form_validation->set_message('username_check');
}
In the form validation file ( validation.php ) how can I call this message
'register_view' => array(
array(
'field' => 'username',
'label' => 'أسم المستخدم',
'rules' => 'required|trim|max_length[20]|username_check'
),
I added username_check at the end isn't this right?
well it's easier to use
'rules' => 'required|trim|max_length[20]|is_unique[users.username]'
Form validation
and add your custom message here
$this->form_validation->set_message('is_unique', 'Error Message');
I can see you are using Arabic it's better to check how to integrate language with default form validation messages as well
I'm just starting to use Zend Framework and was following the quick start documentation for the latest version (1.11.10). Everything was going just fine, but when I placed the form code and ran the application, the form did not render. My code is exactly like http://framework.zend.com/manual/en/learning.quickstart.create-form.html
On the view, I can dump the form just fine with var_dump($this->form);
I've tried echo $this->form(), echo $this->form->render(), but nothing appeared... What could it be?
This problem can occur when Zend can't find the template file for an element. Look at following code:
$element->setDecorators(array(
array('ViewScript',
array(
'viewScript' => 'directory/_input.phtml'
)
)
));
The file _input.phtml must be in the right folder for this Controller. Otherwise Zend can't find the template for input and can't successfully render your element and will not show anything.
Make sure you pass the form to the view from the controller.
In your action handler:
$this->view->form = $my_form;
In your view:
echo $this->form;
I suspected that this was the cause of your problem because Zend Framework doesn't complain if you try to echo a parameter that doesn't exist. (i.e. echo $this->some_fake_parameter won't do anything)
Ok so i tried your code, and it worked for me no problem.
Here is everything:
Controller
<?php
class IndexController extends Zend_Controller_Action
{
public function myTestAction()
{
$form = new Form_Guestbook();
// ... processing logics
if($this->getRequest()->isPost())
{
if($form->isValid($this->getRequest()->getPost()))
{
var_dump($form->getValues());
}
}
$this->view->assign('form', $form);
}
}
Form
<?php
class Form_Guestbook extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// Add an email element
$this->createElement('text', 'email', array(
'label' => 'Your email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
// Add the comment element
$this->addElement('textarea', 'comment', array(
'label' => 'Please Comment:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Sign Guestbook',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
}
}
?>
View
<?php echo $this->form->render(); ?>
can be seen on: http://yaconiello.com/index/my-test
If this isnt working for you, you may be having a configuration error.
I had a problem like that (exact same form, since it is eclipse example)
My problem was due to misunderstanding. Since I thought that I have to directly access to the view script. I entered in the browser something like: hostname/application/view/script/something.php
But in zend all accesses should be through public folder. You have to access to the view like this: hostname/app_name/public/guestbook
hope that would help you