Magento display message on page - validation

I start to give my luck on magento for weeks now. Challenges since then. I am trying to display message above my input box but to no avail. Below are my phtml and controller.
password.phtml
<div id="account-profile">
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<form action="<?php echo Mage::getUrl('customer/account/emailopt'); ?>" method="post">
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
<div class="fieldset">
<ul class="form-list">
<li class="fields">
<div class="field">
<div class="input-box">
<label for="email"><?php echo $this->__('Email') ?><span class="required">*</span></label>
<input name="email" id="email" title="Email" value="" class="required-entry input-text validate-email" type="text" />
</div>
</div>
</li>
</ul>
</div>
<button type="submit" class="btn-dbbdr" title="<?php echo $this->__('Edit Options') ?>"><span><span><?php echo $this->__('Edit Options') ?></span></span></button>
</form>
accountController.php
public function emailoptAction()
{
if (!$this->_validateFormKey()) {
return $this->_redirect('*/emailoptions');
}
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$_email =$this->getRequest()->getParam('email');
if ($email != $_email) {
echo 'This is not your valid email';
} else {
echo 'proceed here';
}
$this->_redirect('*/emailoptions');
}
what happens here is everytime i click on submit, it just echo the message and then redirect to that page again.

You are redirecting after echoing that might not work.
Use bellow code for message display.
$message = $this->__('Error: This is not your valid email');
Mage::getSingleton('core/session')->addError($message);
More Information here
UPDATE
public function emailoptAction()
{
if (!$this->_validateFormKey()) {
return $this->_redirect('*/emailoptions');
}
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
$_email =$this->getRequest()->getParam('email');
if ($email != $_email) {
//echo 'This is not your valid email';
$message = $this->__('This is not your valid email');
Mage::getSingleton('core/session')->addError($message);
} else {
//echo 'proceed here';
$message = $this->__('proceed here');
Mage::getSingleton('core/session')->addSuccess($message);
}
$this->_redirect('*/emailoptions');
}

If you are working in admin panel then use this
Mage::getSingleton('adminhtml/session')->addError("Error here");
otherwise use this for frontend
Mage::getSingleton('core/session')->addError("Error here");
For checkout
Mage::getSingleton('checkout/session')->addError("Error here");
Use these codes in your controller.
Easy i think
if ($email != $_email) {
Mage::getSingleton('adminhtml/session')->addError("Not valid"); //change adminhtml as per need
$this->_redirect('your page');
} else {
Mage::getSingleton('adminhtml/session')->addSuccess("Valid");
$this->_redirect('your page');
}

Related

delete condition if multiple checkbox unchecked in codeigniter

please help me i would like to delete some check box if checkbox unchecked.
My checkbox is automatic show from database my sql. and i would like to delete some checkbox when checkbox in not checked.
PLease Help
its my controller
public function edit_overview($id_product)
{
if ($this->input->post('submit')) {
foreach ($id_overview = $this->input->post('id_overview') as $rm) {
$check_idoverview = $this->Biostar->check_idoverview($id_product, $rm);
if ($check_idoverview==unchecked){
$data['file'] = $check_idoverview;
$this->Biostar->delete_overview($check_idoverview,$id_product);
}else{
if ($check_idoverview > 0) {
} else {
$datafield = array(
'id_product' => $id_product,
'id_overview' => $rm
);
$this->Biostar->saveoverviewproduct($datafield);
$message_success = "Data Has Been Update";
}
}
}
}
$data['message_success'] = #$message_success;
$field = $this->Biostar->get_overview($id_product);
$fieldid_product = $this->Biostar->get_id_product($id_product);
$data['field'] = $field;
$data['id_product'] = $fieldid_product;
$data['content'] = "biostar/edit_overview_product";
$this->load->view('dashboard/index', $data);
}
My Model
function delete_overview($check_overview,$id_product)
{
$sql = "delete from overview_briostar where id_overview='$check_overview' AND id_product='$id_product'";
return $sql;
}
My View
<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>">
<div class="box-body">
<?php foreach ($speed as $row){ ?>
<div class="checkbox">
<label>
<input type="checkbox" name="id_overview[]" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>
</label>
</div>
<?php } ?>
<!-- /input-group -->
</div>
<div class="box-footer">
<input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary">
</div>
</form>
Unchecked checkbox value will not get posted, so you have to use jquery and ajax
Your checkbox
<input class="id_overview" type="checkbox" name="id_overview[]" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>
your jquery
<script type="text/javascript">
$(document).ready(function() {
$("form#frm").submit(function(e) { // give a id to your form
e.preventDefault();
var ids = new Array();
$('.id_overview').each(function() {
if ($(this).is(':checked')) {
} else {
ids.push($(this).val());
}
});
$.ajax({
// send ids through your ajax code
});
});
});
</script>
create a new function and call it by ajax to delete the ids.
You can try like this
<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>">
<div class="box-body">
<?php foreach ($speed as $row){ ?>
<div class="checkbox">
<label>
<input type="hidden" name="all_id[]" value="<?php echo $row['id_overview']; ?>" />
<input type="checkbox" name="id_overview<?php echo $row['id_overview']; ?>" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?>
</label>
</div>
<?php } ?>
<!-- /input-group -->
</div>
<div class="box-footer">
<input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary">
</div>
</form>
and in you controller
if ($this->input->post('submit')) {
foreach($all_id as $row_id)
{
if($this->input->post('id_overview'.$row_id))
{
//do action where id is present
}
else
{
//do action if unchecked by getting id $row_id
}
}
}

localhost is redirecting to 127.0.0.1 after submitting form in Codeigniter 3

I have a problem in my code. I have a form in my code that looks like this:
<?php echo form_open('users/auth/login', array('class' => 'form floating-label')); ?>
<div class="form-group">
<input type="text" class="form-control" id="username" name="username" />
<label for="username">Username</label>
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" />
<label for="password">Password</label>
<p class="help-block">Forgotten?</p>
</div>
<br/>
<div class="row">
<div class="col-xs-12 text-right">
<?php echo $this->session->flashdata('note'); ?>
<button class="btn btn-primary btn-raised btn-ink" type="submit">Login Account</button>
</div>
</div>
<?php echo form_close(); ?>
Then after clicking the submit it redirect my localhost to 127.0.0.1
After submitting the form it redirects me to
http://127.0.0.1/teradasys/index.php/user/login
Here's my controller
public function index() {
if($this->aauth->is_loggedin()) {
} else {
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
public function login() {
$identifier = $this->input->post('username');
$password = $this->input->post('password');
if ($this->aauth->login($identifier, $password, true)){
return true;
} else {
$note = $this->aauth->get_errors_array();
$this->session->set_flashdata('note', $note[0]);
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
Please check your base url in application/config/config.php
and change it.
$config['base_url'] = 'http://localhost/test/';
Please check your base url in application/config/config.php
change from
$config['base_url'] = 'http://localhost/test/';
to
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
No need to change anything after placing this .If u r using local or live host.

Value not getting bind in the Magento controller

This is the predefined post function inside Magento controller.
public function postAction()
{
$post = $this->getRequest()->getPost();
if ( $post ) {
$translate = Mage::getSingleton('core/translate');
/* #var $translate Mage_Core_Model_Translate */
$translate->setTranslateInline(false);
try {
$postObject = new Varien_Object();
$postObject->setData($post);
$error = false;
if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
$error = true;
}
if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
//$error = true; //orignal code
$error = false;
}
if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
$error = true;
}
Mage::log($post['producturl']);
if (!Zend_Validate::is(trim($post['producturl']) , 'NotEmpty')) {
$error = true;
}
//if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
// $error = true;
//}
Mage::log($error);
Mage::log($postObject);
if ($error) {
throw new Exception();
}
$mailTemplate = Mage::getModel('core/email_template');
/* #var $mailTemplate Mage_Core_Model_Email_Template */
$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->sendTransactional(
Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
null,
array('data' => $postObject)
);
if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('');
return;
} catch (Exception $e) {
$translate->setTranslateInline(true);
Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
$this->_redirect('*/*/');
return;
}
} else {
$this->_redirect('*/*/');
}
}
When I log the producturl attribute, it prints the correct view.But I do not get that value in my email. How to get that value binded so it gets send to the email?
My form looks like this:
<form action="<?php echo $this->getUrl('contacts/index/post'); ?>" id="contactForm" method="post">
<div class="row">
<div class="col-sm-12">
<!--<label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>-->
<div class="input-box">
<input name="name" id="name" placeholder="Name*" title="<em>*</em><?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
</div>
</div>
<div class="col-sm-12">
<!--<label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>-->
<div class="input-box">
<input name="email" id="email" placeholder="Email*" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="text" />
</div>
</div>
<div class="col-sm-12">
<!--<label for="telephone" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Mobile') ?></label>-->
<div class="input-box">
<input name="telephone" id="telephone" placeholder="Mobile*" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text required-entry" type="text" />
</div>
</div>
<div class="col-sm-12">
<div class="input-box">
<input name="producturl" id="producturl" placeholder="Product URL*" title="<?php echo Mage::helper('contacts')->__('ProductURL') ?>" value="url hai bhai" class="input-text required-entry" type="text" />
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<!--<label for="comment" class=""><?php echo Mage::helper('contacts')->__('Comment') ?></label>-->
<div class="input-box input-textarea">
<textarea name="comment" id="comment" rows="3" placeholder="Message" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class=" input-text" placeholder="<?php echo Mage::helper('contacts')->__('Comment') ?>" style="width: 100%; height: 15%;resize:none;"></textarea>
</div>
</div>
</div>
<div class="row" style="text-align: center">
<div class="col-sm-12" style="padding-top:2%">
<input type="text" name="hideit" id="hideit" value="url hai bhai" style="display:none !important;" />
<button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('BOOK A DESIGNER') ?></span></span></button>
</div>
</div>
</div>
</div>
So, the emails templates are html files that you can actually find in the folder app/locale/[some_locale]/template/ if your locale is english of the US then some_locale will be en_US, if that is french from Belgium, then it will be fr_BE, so that is the language code on 2 letter as defined by ISO 639-1 then an underscore _ and country code as defined by ISO 3166-1 alpha-2.
And the name of the file will be something you could find if you do a global search on the handle of the template you actually stated in you comment.
So here contacts_email_email_template is actually defined in app/code/core/Mage/Contacts/etc/config.xml as being the file contact_form.html :
<template>
<email>
<contacts_email_email_template translate="label" module="contacts">
<label>Contact Form</label>
<file>contact_form.html</file>
<type>text</type>
</contacts_email_email_template>
</email>
</template>
So if you go and edit app/locale/en_US/template/email/contact_form.html that I reproduced here below and in which I added your product url value, that should work :
<!--#subject Contact Form#-->
<!--#vars
{"var data.name":"Sender Name",
"var data.email":"Sender Email",
"var data.telephone":"Sender Telephone",
"var data.comment":"Comment"}
#-->
Name: {{var data.name}}
Email: {{var data.email}}
Telephone: {{var data.telephone}}
Comment: {{var data.comment}}
<!-- this below line is actually what you are after -->
Product URL : {{var data.producturl}}
data, actually being the key of the array you are passing as the fifth argument of the function sendTransactional
Well if you wish add the product URL to the email you first need to get the product key and wrap it around the static getUrl function.
Mage::getUrl($_product->getUrlKey())
Next just assign the above code to a variable and use some jQuery / Javascript to append the url to the end of the content of the message body. Honestly I would not recommend you to do this.

Form Validation Showing 2 Callback Messages

I am trying to set my own callback message for codeigniter. But when I try to make my own it will show both messages codeigniter one and my custom one. Here is a link to my errors are showing up the top error is one that I would like to use http://postimg.org/image/v9ilejmnt/
How am I able to make it only show my custom error message?
I also use hmvc so I have had to add run($this) in the form validation to make callback work.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('user');
$this->load->library('form_validation');
$this->lang->load('common/login', 'english');
if($this->session->userdata('user_id')) {
redirect('dashboard');
} else {
return false;
redirect('login');
}
}
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]|xss_clean|callback_validate');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
redirect('dashboard');
}
}
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->error['warning'] = $this->lang->line('error_login');
return !$this->error;
}
}
}
View
<?php echo modules::run('common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-4 col-sm-4">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title">Administration Login</h2></div>
<div class="panel-body">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<form action="<?php echo $action;?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" size="50" />
</div>
<?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="password" value="" placeholder="Password" class="form-control"/>
</div>
<?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
</div>
</div>
</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>
you forgot to set error message for your your callback.
$this->form_validation->set_message('validate', 'invalid username or password');
return FALSE;
or you can do
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
in your view file you are showing errors for each fields (CI form validation messages) as well as your own <?php echo $error_warning; ?>
you should use following code
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->form_validation->set_message('validate', $this->lang->line('error_login'));
return FALSE;
}
and remove this block from view
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
}
and remove this too
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
I worked it out today. When want to use both validation methods custom and form validation can not use callbacks.
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
} else {
if($this->validate()) {
redirect('dashboard');
} else {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('login');
$this->load->view('common/login', $data);
}
}
}

Codeigniter form validation is not showing

for somehow my codeigniter form validation is not showing on login page
given below is my login page in view
<div class="full_w">
<p>Food4U</p>
<?php echo validation_errors(); ?>
<?php echo form_open('staff/valid_login'); ?>
<label for="city">Select City</label>
<select name="city" class="text">
<option value="">Select City</option>
<?php echo $this->front_model->get_Fcities(); ?>
</select>
<label for="login">Username:</label>
<input id="login" name="username" class="text" />
<label for="pass">Password:</label>
<input id="pass" name="password" type="password" class="text" />
<div class="sep"></div>
<button type="submit" class="ok">Login</button>
<a class="button" href="">Change Password</a>
<?php echo form_close(); ?>
</form>
</div>
given below are functions in controller
function valid_login()
{
$this->form_validation->set_rules('city','City','required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('username','Username','required|trim|xss_clean|callback_validate_credentials');
$this->form_validation->set_rules('password','Password','required|trim');
if($this->form_validation->run())
{
$data = array(
'username' =>$this->input->post('username'),
'is_loggedin' => 1
);
$this->session->set_userdata($data);
redirect('staff/member');
}
else
{
$this->load->view("dashboard/login");
}
}
function member()
{
$row['content']=$this->load->view("dashboard/member",array(),true);
$this->load->view("dashboard/template", $row);
}
function validate_credentials()
{
if($this->staff_model->can_login())
{
return true;
}
else
{
$this->form_validation->set_message('validate_credentials','Incorrect Username or Password');
return false;
}
}
and my model function is
function can_login()
{
$this->db->where('DB_User_City',$this->input->post('city'));
$this->db->where('DB_User_Login',$this->input->post('username'));
$this->db->where('DB_User_Pass',$this->input->post('password'));
$query= $this->db->get('frp_db_user');
if($query->num_rows() == 1)
{
return true;
}
else
{
return false;
}
}
if user just click on submit button user should see a message and if user provide any single value incorrect then user must also see whats wrong with login so that user can provide correct information
inside your view for each fields you set validation blow its field or some where else
write like this codes to show your validation errors.
<div class="full_w">
<p>Food4U</p>
<?php echo validation_errors(); ?>
<?php echo form_open('staff/valid_login'); ?>
<label for="city">Select City</label>
<select name="city" class="text">
<option value="">Select City</option>
<?php echo $this->front_model->get_Fcities(); ?>
</select>
<br />
<?=form_error('city')?>
<br />
<label for="login">Username:</label>
<input id="login" name="username" class="text" />
<br />
<?=form_error('username')?>
<br />
<label for="pass">Password:</label>
<input id="pass" name="password" type="password" class="text" />
<br />
<?=form_error('password')?>
<br />
<div class="sep"></div>
<button type="submit" class="ok">Login</button>
<a class="button" href="">Change Password</a>
<?php echo form_close(); ?>
</form>
</div>
Your code seems like ok. Try this one
if($this->form_validation->run() == FALSE)
{
//redirect to your login form
}
else
{
//redirect to dashboard
}
please let me know if you face any problem.

Resources