Codeigntier validation issue while using tank_auth - php

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

Related

Stop form validating on first error codeigniter

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

codeigniter form validation on array not form submit

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.

using one validation for two input codeigniter

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

CodeIgniter Form Validation Matches Label

Is it possible to change the output of the matches call when running form validation in CodeIgniter?
I'm validating a password against a confirm password box.
array(
'field' => 'userPassword',
'label' => 'Password',
'rules' => 'trim|required|matches[userConfPassword]'
)
But when the error is triggered it prints out:
The Password field does not match the userConfPassword field.
The user has no idea was userConfPassword means and I'd like to change that to something more friendly. Reading the docs I can't find a way to do it.
Well, you can override the default message:
$this->form_validation->set_message('matches', 'the two passwords do not match|');
or some other message like that :).
I usually use that message for the "confirm password" only, though, so I set the normal password field as required and that's all, while for the confirm I have it to match the password entered, not the otherway around (or both). But that's just matter of taste I think.
For the record, it's written in this paragraph of the manual.
This code works for me:
array(
'field' => 'userPassword',
'label' => 'Password',
'rules' => 'trim|required|matches[userConfPassword]'
),
array(
'field' => 'userConfPassword',
'label' => 'Confirm Password',
'rules' => 'trim|required'
)
When the error is triggered it prints out:
The Password field does not match the Confirm Password field.

How can I customise Zend_Form regex error messages?

I have the following code:
$postcode = $form->createElement('text', 'postcode');
$postcode->setLabel('Post code:');
$postcode->addValidator('regex', false,
array('/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i'));
$postcode->addFilters(array('StringToUpper'));
$postcode->setRequired(true);
It creates an input field in a form and sets a regex validation rule and works just fine.
The problem is that the error message it displays when a user enters an invalid postcode is this:
'POSTCODE' does not match against pattern
'/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i'
(where input was POSTCODE)
How can I change this message to be a little more friendly?
I think to remember, you can set the error message in the Validator:
$postcode = $form->createElement('text', 'postcode');
$postcode->setLabel('Post code:');
$postcode->addValidator('regex', false, array(
'pattern' => '/^[a-z]{1,3}[0-9]{1,3} ?[0-9]{1,3}[a-z]{1,3}$/i')
'messages' => array(
'regexInvalid' => "Invalid type given, value should be string, integer or float",
'regexNotMatch' => "'%value%' does not match against pattern '%pattern%'",
'regexErrorous' => "There was an internal error while using the pattern '%pattern%'"
)
);
$postcode->addFilters(array('StringToUpper'));
$postcode->setRequired(true);
If that doesn't work, try
setErrorMessages(array $messages): add multiple error messages to display on form validation errors, overwriting all previously set error messages.
If you define your validator as external variable use setMessage():
$validator = new Zend_Validate_Alnum();
$validator->setMessage('My custom error message for given validation rule',
Zend_Validate_Alnum::INVALID);
$formElement->addValidator($validator);
As you see in example above validator for form doesn't differ from any other kind of Zend_Validate_* instances.
Setting up validation messages involves looking into API Docs and finding out message constant for a given validation error (as I did in case of Zend_Validate_Alnum::INVALID). Of course if your IDE provides good context auto-completion just typing the validator class can be enough - as message constants are really self-explanatory in most cases.
Another way would be to use Zend_Form's magic methods, and simply passing 'messages' key, as a parameter to your validator:
$formElement->addValidator(array(
'alnum', false, array('messages' => array(
Zend_Validate_Alnum::INVALID => 'my message'
))
));
This would internally trigger the setMessages() method defined in Zend_Validate_Abstract, and in essence just a short-cut/time-saver defined for Zend_Form's.
NB: There's a dedicated section in ZF Manual regarding validation messages.
You could use the original Zend postcode validator
$user->addElement('text', 'postcode', array('label' => 'Postcode *',
'required' => true,
'class' => 'postcode_anywhere',
"validators" => array(
array("NotEmpty", false, array("messages" => array("isEmpty" => "Required *"),)),
array('PostCode', false, array('locale' => 'en_GB')
)
),
'filters' => array(array('StringToUpper')),
'class' => 'text'
)
);

Categories