CodeIgniter Form Validation Matches Label - php

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.

Related

Use of repeated type with Symfony

for one property of my object (newPSW), I use a repeated type in the form builder.
$builder->add('NewPSW', 'repeated', array(
'type' => 'password',
'invalid_message' => 'blablabla',
'first_options' => array('label' => 'New password'),
'second_options' => array('label' => 'Confirm password'),
))
));
if I look at the code source the name of the two fields are newPSW[first] and newPSW[second]
Validating my form, I would like to add a custom error to NewPSW property.
For any other "normal" field, I would do this (and it works good) :
$error = new FormError("What I want to say");
$form->get('object Property name')->addError($error);
I tried to do the same thing with this field but the error message does not display.
Q1 What do I have to write in the 'get' method of the $form to add the error?
I tried this already :
$form->get('newPSW')->addError($error); (no error but nothing is displayed)
$form->get('newPSW[first]')->addError($error); (error : Child "newPSW[first]" does not exist)
You can provide custom error message for the second of the repeated field like this:
$form->get('NewPSW')->get('second')->addError(new FormError('Oops! This is error message for confirm field'));

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

Codeigntier validation issue while using tank_auth

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

AllowEmpty vs NotEmpty

New to CakePHP here - I'm going through the documentation on the site, trying to muster up some basic data validation for a model I'm creating. This will likely be the first of many questions I have about CakePHP.
In the CakePHP Book, the validation rules seem to specify two different methods for making sure that a field isn't empty - AllowEmpty, and NotEmpty.
Question - is there a tangible difference between these two? CakePHP states that validation rules should occur in your model or controller - is one better suited for a model, and the other for a controller? The Book doesn't say anything about this. I'm guessing that one is an older method that's simply still around?
What gives? Should I use a specific one, or both, or does it not matter?
Edit: I decided to check the CakePHP 1.3 class documentation for it (to check the default value of the allowEmpty attribute), but it doesn't even show up. It's not in the source code either...is there something I'm missing?
Welcome to Cake. I hope you enjoy it.
This is definitely one of the stranger aspects of Cake.
notEmpty is a rule in and of itself. You can define it in your $validation attribute. You can assign a message for when this validation fails. You can treat this as if it is any other validation rule.
allowEmpty is an option of another validation rule, normally not notEmpty. It is not a validation rule in-and-of-itself. This would allow, for example, you to define that a varchar field allows an empty string, '', or a string with no more than 20 characters.
Edit:
Here's some code
// model validation using 'notEmpty'
$validation = array(
'fieldName' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'This value may not be left empty!'
),
... // other rules can go here
),
... // other fieldName can go here
);
// model validation using 'allowEmpty' to create an optional field
$validation = array(
'fieldName' => array(
'maxLength' => array(
'rule' => array('maxLength', 20),
'message' => 'This field may only contain 20 characters!',
'allowEmpty' => true // we'll also accept an empty string
),
... // other rules can go here
)
... // other fieldName can go here
);
I found a case where I had to use 'allowEmpty' => false instead of rule => 'notEmpty'. I had a form with an upload input (type='file') that had a validation rule of notEmpty, and it kept failing validation, even though the debugger showed the file[] array loaded. When I removed the 'notEmpty' rule and set allowEmpty => false, it worked, throwing an error when no file was chosen and accepting it when one was selected.
It must have something to do with the value being an array rather than a text value.
Its very simply to make server side validation in cakephp
Here is code for both validation (noEmpty, maxlength) for the same field.
'fieldName' => array(
'rule' => array('maxLength', 20),
'message' => 'fieldName should be less than 20 characters',
'allowEmpty' => true
),
'fieldName' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter field name',
),
),

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