I have languages file form_validation_lang.php with a standard errors messages in array.
I added some own messages here:
$lang['my'] = "Enter vacancy name";
I have form validation:
$this->form_validation->set_rules('vacancy', 'here load text from field my', 'required');
I want to display only text from $lang['my'] when my field is not filled.
You can set messages for certain rule, make form_validation.php inside application folder as #Craig mentioned above:
$lang['form_validation_required'] = 'Yep, that {field} is required and has custom message.';
You can place rules message inside controller:
$this->form_validation->set_message('integer', 'There must be an integer.');
And you can also set message for one of the rules among others:
$this->form_validation->set_rules('email', 'Your email', 'required|valid_email',
array('valid_email' => 'Watch out, it should be email')
);
Related
we are using magento, we need to get the value of a text field in register page to be displayed in the email template sent as a notification. the field is added by marketplace extension. it is text field.
Thanks
Find the below controller sample code for send parameter to email.
if(isset($_POST))
{
$email_template_variables = array(
'customer_name' => $_POST['name']
);
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);
Display varible / parameter in email like below.
{{var customer_name}}
I tried to create a custom data annotation validation attribute (NameValidationAttribute) in MVC 5 project using VS2013. I was able to successfully add the client side validation and the error message for custom validation is getting displayed as soon as the focus leaves the textbox. However, the standard attributes like [Required] and [Range] validators are now not showing proper error messages, says 'Warning: No message defined for 'field' ' (See below screenshot).
Question:
- Why the standard validation error messages are showing as "Warning: No message defined for UnitsInStock"? What am I missing?
Below is my custom client validation script:
I included following scripts in EditProducts page.
Please note that the error messages for UnitPrice, UnitsInStock and ReorderLevel fields are defined with Range validation attribute (see below).
FYI, I tried to change the order of the scripts in ProductEdit page but still its showing the same message.
Please advise!
I ran into this issue. I had created an MVC attribute called PasswordAttribute, with a client side validator called 'password'.
$.validator.addMethod('password', function (value, element, params) {
...[validation logic]
});
$.validator.unobtrusive.adapters.addBool('password');
As soon as I added this logic to the solution I got the error you saw on my Password and ConfirmPassword fields. Note that this occurred before the attribute was even used. Simply renaming the client side validator to 'passwordcheck' fixed the issue. I initially thought that the name 'password' possibly clashed with one of the pre-defined validators (cf. jQuery Validation Documentation) but this doesn't appear to be the case. I suspect now that it is a clash with the name or value for some input field attribute. Anyway, the solution was simply to rename the validator.
jQuery unobtrusive need data-msg for default validate message.
This is how to apply dynamic error message from your model to Html
$.validator.unobtrusive.adapters.add("requireif", function (options) {
$('#' + options.element.id).attr("data-msg", options.message);
// Add rule ...
});
You can change default warning message.
$.validator.addMethod("requireif", function (value, element, pair) {
// validate logic
return true/false;
}, "YOUR DEFAULT MESSAGE HERE");
Or
#Html.TextBoxFor(m => m.Property1, new { Class = "form-control", data_msg = "YOUR DEFAULT MESSAGE HERE" })
Or if you can put it directly to your Html like this.
<input class="form-control" data-msg="YOUR DEFAULT MESSAGE HERE"/>
I'm in the process of setting up a basic site for cell phone reviews and information. I keep getting these fake accounts registering and posting content on my site that is not appropriate.
I have just installed the CAPTCHA and image CAPTCHA module, but this doesn't seem to be stopping them.
What is the best way to avoid these fake accounts?
Thank you.
Another strategy is to add another field in the user registration form. Most bots wouldn't know which fields are required, so they fill in everything. If the user enters a value into the new field then don't create an account. You can hide the field from the UI with CSS so that real people won't be able to see the field and enter anything into it. Read Easy spam prevention using hidden forms for a detailed explanation.
To implement this feature into your Drupal site, you need to create a module to alter the user registration form and create a validation for it.
Add another field to the user registration form:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'user_register_form') {
$form['field_fname'] = array(
'#title' => 'Answer if you are a bot',
'#type' => 'textfield',
);
$form['#validate'][] = 'mymodule_user_bot_validate';
}
}
Add the validation:
function mymodule_user_bot_validate($form, &$form_state) {
if($form['field_fname']['#value'] != '') {
form_set_error('bot_prevention', t('Could not create your account.'));
drupal_goto('user/register');
}
}
Then hide the field with CSS.
I'm setting up a form with CI and I'm using the form validation to help me with this.
At the moment I have different rules setup, like
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
But in my form I have a checkbox, enabling the checkbox will show some new inputs that are required when the checkbox is checked.
When the checkbox is unchecked these fields are not required.
What would be the best method to do this in CI?
// Set validation rules
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
// Some more rules here
if($this->form_validation->run() == true) {
// Form was validated
}
I faced something just like this a couple months ago. I just added a short if-statement.
It looked something like this (using address for example):
$this->form_validation->set_rules('home_address', '"Home Address"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('unit', '"Unit"', 'trim|xss_clean|strip_tags');
$this->form_validation->set_rules('city', '"City"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('state', '"State"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('zip', '"Zip"', 'required|trim|xss_clean|strip_tags');
//checkbox formatted like this in the view: form_checkbox('has_second_address', 'accept');
if ($this->input->post('has_second_address') == 'accept')
{
$this->form_validation->set_rules('street_address_2', '"Street Address 2"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('state_2', '"State 2"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('city_2', '"City 2"', 'required|trim|xss_clean|strip_tags');
$this->form_validation->set_rules('zip_2', '"Zip 2"', 'required|trim|xss_clean|strip_tags');
}
if($this->form_validation->run() == true) {
//example
//will return FALSE if empty
$street_address_2 = $this->input->post('street_address_2');
//and so on...
}
I'm not sure if this is the Codeigniter way or not, but last time I checked I couldn't find a "best method" way. This definitely gets the job done, and it at minimum allows you to take control over a users $_POST variables.
Anyways, I hope this helps
if($this->input->post('checkbox_name')){
// add more validation rules here
}
you could use javascript / jquery to set the value of a hidden field to true or false depending of whether the checkbox has been checked.
Then in your controller check with a conditional
if ($hidden_field) {
//run more validation.
}
The fields I am making required to fill out, should repopulate the correctly filled out fields, while NOT submitting the form and posing errors for the incorrectly filled out fields. What is the best way to do that?
Please note that with this code, I am repopulating the fields as they should be upon submitting the form correctly and they are all displaying when page is reloaded.
<div class="dropdown_structure">
<?php
if($user['location'] == "empty")
{
echo country_dropdown('location');
}
else
{
echo country_dropdown('location',$user['location']);
}
?>
</div>
Also please note that I've tried inserting the value in the input fields.
$data = array( 'name' => 'location', 'value' => $this->input->post('location'));
echo relation_dropdown('location');
Thanks in advance
Hi if you are using the following country dropdown helper you can set value validation in following way
Country dropdown helper
country_dropdown('location',array(),set_value('location'))
even in your second dropdown use set_value('field_name') will work. if validation is failed your selected value will be remain.