Zend form change matching error - php

I add validator to email element:
$usr_email->addValidator('Db_NoRecordExists', true, array('users', 'usr_email'));
And now have error message: A record matching 'admin#example' was found
I want change this error, but I want that will display inserted email
A email 'admin#example' already exist in database
I try , but email not disply ant html tags
addErrorMessage("**This email **strong text** address already exist.**");

Use %value% placeholder in the error message, it will be replaced with the actual value.
Example:
$usr_email->addValidator('Db_NoRecordExists', true, array(
'users',
'usr_email',
'messages' => array(
'recordFound' => 'A email %value% already exist in database'
)));

Related

How to check if a value already exists in the database with codeigniter

I found this line : $this->form_validation->set_rules();
But I have no idea where to use it. it seems complicated and everywhere its mentioned its about 'username' and 'password' fields.
I have a variable called 'utr' which I am POSTing to the same page with ajax submit, now I want to to check if it is a unique value that is submitted on form submit and not already in my database.
The table name is 'orders' and the column name is 'utr'.
Thank you.
You can check any value which is already exist in database try this code :
utr is the name which is send by form and is_unique validate table.coulmn in database.
$this->form_validation->set_rules(
'utr', 'UTR Title',
'required|is_unique[orders.utr]',
array(
'required' => 'You have not provided %s.',
'is_unique' => 'This %s already exists.'
)
);

Laravel custom validation rule on valid email

So basically I have a simple form and one of the fields is an email.
My controller responsible for this form is the following(showing only the essentials)
$messages = array(
'rsvp_email.required' => 'A valid email is required.')
);
$rules = array(
'rsvp_email' => 'required|max:150|email',
);
$validator = Validator::make($request->all(), $rules,$messages);
Now there are 2 scenarios:
a) The email is not inserted and the above validation works with the custom message(This works OK)
b) The email is not in a valid format (myemail#email) and the resulted error message is The rsvp email must be a valid email address. which is not what I want to be displayed.
What additional rule should I include for a valid email?
Thank
IF you want to change this message go to the following path:
resources/lang/en/validation.php
and change value of email index.

Symfony2 add extra selectbox

I added extra select box (choice type) and mapped=>false see the code below.
However when I submit the form. It returned the error msg "This value is not valid."
$form->add('extraField' ,'choice', array(
'required' => false,
'choices' => $arrayChoices,
'mapped'=>false,
'data' =>$id
));
What did I do wrong here?
Ok, after this doc : http://symfony.com/doc/current/reference/forms/types/choice.html#choices
I don't see 'data' option. Try to remove it please. (and , what is the use of this field [data]? )
Are you sure that $id is valid array key for $arrayChoices?
Also, when you submit the form, the request must contain a valid key inside of $arrayChoices

Messages not working on Zend_Form_Element_Text

In a form, I have the following element:
$email = new Zend_Form_Element_Text('username');
$email
->setLabel($this->getView()->l('E-mail'))
->setRequired(TRUE)
->addValidator('EmailAddress')
->addValidator('Db_NoRecordExists', true,
array(
'table' => 'pf_user',
'field' => 'email',
'messages' => array(
'recordFound' => 'This username is already registered',
)
))
->setErrorMessages(array(
'emailAddressInvalidFormat' => 'You must enter a valid e-mail',
'isEmpty' => 'You must enter an e-mail',
'recordFound' => 'This e-mail has already registered in out database'
));
$form->addElement($email)
the problem is that I always I get the same message "You must enter a valid e-mail" (the first one). Does anybody knows what is the mistake??
Actually, what you're doing is the following :
You set the errors on the element
Zend now thinks that the element did not validate correctly and that the first error is
"You must enter a valid e-mail"
When you display the form, since you set errors, Zend will find them and display the first one it finds. If you switch the order then you'll find that whichever error you put up top will be the error you get.
The more correct way is to set the custom messages in the validator. When the validators are called to validate the element, if the validation fails, the validator will call the setErrorMessages on the element to set the custom errors you specify. Use this type of code below to set your custom messages.
$element->addValidator( array( 'Db_NoRecordExists', true, array(
'messages' = array(
Zend_Validate_Db_Abstract::ERROR_NO_RECORD_FOUND => 'Myy custom no error record',
Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND => 'My custom record error'
)
) ) );
You'll find that usually there are consts in each validator class that specify one type of error. In this case, the consts are in the parent class of the DB_NoRecordExists class but usually you'll find them directly in the class near the top.
Basically by passing 'true' as second parameter to addValidator() you are saying the validator to break the chain whenever validator fails . Since "" is not an valid email address hence the first email validator fails and breaks the chain
From Zend Doc http://framework.zend.com/manual/en/zend.validate.validator_chains.html
In some cases it makes sense to have a validator break the chain if
its validation process fails. Zend_Validate supports such use cases
with the second parameter to the addValidator() method. By setting
$breakChainOnFailure to TRUE, the added validator will break the chain
execution upon failure, which avoids running any other validations
that are determined to be unnecessary or inappropriate for the
situation. If the above example were written as follows, then the
alphanumeric validation would not occur if the string length
validation fails:
$validatorChain->addValidator(
new Zend_Validate_StringLength(array('min' => 6,
'max' => 12)),
true)
->addValidator(new Zend_Validate_Alnum());

Zend Translate Zend Form!

Currently, isEmpty errors throw:
Value is required and can't be empty
I'm loading my translator up like this:
[translation]
adapter = array
content.english["emailNotUnique"] = "Your user already exists"
content.english["Value is required and can't be empty"] = "You must specify your ID"
locale = en
The config above produces a valid array according to zend translate spec, so:
$this -> form -> setTranslator(new Zend_Translate($this -> getConfig() -> translation));
expected result is that isEmpty errors should now show up as
You must specify your ID
However I'm getting no love. No errors and no translation. I'm on Zend 1.11.1 and PHP5.3.5.
I think that the problem is with english key in your ini file. Specifically it should not be there, because what you are actually passing to Zend_Translate as a content is:
'content' => array(
'english' => array(
"emailNotUnique" => 'Your user already exists' ,
"Value is required and can't be empty" => 'You must specify your ID'
)
);
And it should be:
'content' => array(
"emailNotUnique" => 'Your user already exists' ,
"Value is required and can't be empty" => 'You must specify your ID'
);
Hope this will help.
Try to change
content.english["isEmpty"] = "You must specify your ID"
you can user language file(MO) its simple translate in multiple language
when initialize bootstrap at a time initialize selected language

Categories