Yii model method addError is not working - forms

I have an issue about displaying error in a yii form.
This is my controller:
$custom_user->attributes = $_POST['CustomUser'];
if($custom_user->validate())
{
...
save
...
}
else
{
$custom_user->addError('username', 'Error X');
$this->redirect(array('access/index'),
array('user'=>$custom_user,
'tab'=>$tab_person,
));
}
This is my index view:
<?php $form=$this->beginWidget('BaseForm', array(
'id'=>'user-form',
'action'=>Yii::app()->createUrl('person/createUser'))); ?>
<?php echo $form->errorSummary($user);?>
<div class="right">
<?php echo $form->textField($user,'username',array('size'=>30,'maxlength'=>255)); ?>
<br/><?php echo $form->error($user,'username'); ?>
</div>
...
...
<div style="float: left;">
<?php echo $form->dateField($user,'valid_from',null,'valid_from_formated'); ?>
<br/><?php echo $form->error($user,'valid_from'); ?>
</div>
Here is my BaseForm
public $enableClientValidation = true;
public $enableAjaxValidation = true;
public $clientOptions = array( 'hideErrorMessage'=>false,
'validateOnSubmit'=>true,
'validateOnChange'=>false,
'validateOnType'=>false,
'afterValidateAttribute' => 'js:enableSubmitAV',
'afterValidate' => 'js:submitFormAV'); // always fires this after
Here is the problem:
"$form->errorSummary($user);" is empty
and noting is display under "$form->error($user,'username');"
But if I look in Firebug I can see that under "response" tab:
{"CustomUser_valid_until":["Valid Until must be
greater than \"20121127\"."]}
it's a good thing because it means my rules work great. But none errors are displayed. Not this one and even the error "Error X" I add in my controller is not diplayed... (I'm sure I pass throught the else statement, i tried it).
So, does anyone could have an idea?
Thanks for reading me and sorry for my approximate English.
Have a good day :)
Michaƫl

You are redirecting, meaning a whole new request is called, and any responses are deleted.
Why are you redirecting to "access/index" rather than just calling the view? If you do need to redirect, which you definitely can, try setting a flash variable as it is store in the browser's session and will persist through redirects:
Yii::app()->user->setFlash('username', "Error X");
Then on the access/index page:
<?php if(Yii::app()->user->hasFlash('username')):?>
<div class="error">
<?php echo Yii::app()->user->getFlash('username'); ?>
</div>
<?php endif; ?>

Ok finally I got what was wrong. It was the configuration in my CActiveForm. The option "enableAjaxValidation" seems to make some problem when it's on "true". I don't know really why but I've already had this problem with a login form befor. I'll try to work out this problem.

Related

CAKEPHP Confirming on Submit

I have a submit button in my form like this :
<?php echo $this->Form->end(__('Submit')); ?>
and I want to embed variables that are part of the input form.
For example, I want to embed a variable that is input here $this->Form->input('user_id');.
Does anyone know how to do this? I currently have this onsubmit as part of the options array, but it throws errors when I try to embed variables.
<?php echo $this->Form->create('Shift', array(
'onsubmit' => 'return confirm("Creating shift for '$id' on ");'
)); ?>
Now making an edit to this question. So the Javascript solution worked fine. Issue now is that I want the alert box to display the user's name, which is what is displayed in the dropbox. However, the HTML tags contain the user's ids. Here is the HTML
<div class="input select required">
<label for="my_user_id">User</label>
<select name="data[Shift][user_id]" id="my_user_id" required="required">
<option value="1">john doe</option>
<option value="2">john johnson</option>
Is there a way to access the value between the <option> tags using Javascript?
You need to use double quotes:
<?php
echo $this->Form->create('Shift', array(
'onsubmit' => "return confirm(\"Creating shift for '$id' on \");"
));
?>
Another option is to use $this->Form->postLink() and set $confirmMessage parameter if it is a simple state shift form.
You can use JQuery
echo $this->Form->create('Shift', array('id' => 'myform'));
echo $this->Form->input('user_id', array('id' => 'my_user_id', 'type'=>'text'));
After the form:
<?php echo $this->Html->scriptBlock('
$(document).ready(function() {
$("#myform").submit(function(event) {
alert("Creating shift for " + $("#my_user_id").val());
});
});
');
?>

Is there a 'kosher' way to display options in a select tag as a list of links, and NOT a drop-down?

My problem is that I am making a dynamic generated form using codeigniter. I need my dynamic results to be made into links. These 'links' are contained within a form and when clicked, should pass an item_id. The displayed link however should portray the item_name. I have both the id and name of every product within a foreach, gathered from a query. The reason I am doing it like this and not having them as a radio/drop-down, is because I'm attempting to make this touchscreen friendly. I need to have them list out as links. I am trying to stay away from javascript on this if I can. Just wondering if anyone had an idea of what I could do here. As I asked in my title, what I am looking for is a way to make form inputs as if they where select/option but listed as links. I also tried them as submit tags, but then I cannot display the name and have the value as the id.
EDIT:
I should also add that $key is the id of each product and $info is the name.
foreach($products as $key => $info):
?>
<div class="field_row clearfix">
<?php
/* NEED THE FORM ELEMENTS HERE */
?>
</div>
<?php
endforeach;
You could use jquery and js metadata to achieve this.
N.B. untested code of the top of my head.
<?php
foreach ($products as $key => $info)
{
?>
<div class="product-link {item_id: <?php echo $key; ?>}">
<?php echo $info; ?>
</div>
<?php
}
?>
<script type=text/javascript">
/*<![CDATA[*/
$(document).ready(function() {
// listen event on drop down
$(".product-link").click(function () {
var item_id = $(this).metadata().item_id;
// do what ever you want here, maybe a redirect
window.location.href = "/products/items/"+item_id;
});
});
/*]]>*/
</script>

Zend Framework Custom Forms with viewScript

I am having some problems working out how to use custom forms in Zend Framework.
I have followed various guides but none seem to work. Nothing at all gets rendered.
Here is the bits of code that I am trying to use (All code below is in the default module). I have simplified the code to a single input for the test.
applications/forms/One/Nametest.php
class Application_Form_One_Nametest extends Zend_Form {
public function init() {
$this->setMethod('post');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Box Name')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit Message');
$submit->setAttrib('id', 'submitbutton');
$submit->setAttrib('class', 'bluebutton');
$this->addElements(array($name, $submit));
}
}
application/views/scripts/one/formlayout.phtml
<form action="<?= $this->escape($this->form->getAction()) ?>" method="<?= $this->escape($this->form->getMethod()) ?>">
<p>
Please provide us the following information so we can know more about
you.
</p>
<? echo $this->element->name ?>
<? echo $this->element->submit ?>
</form>
application/controllers/IndexController.php
public function formtestAction() {
$form = new Application_Form_One_Nametest();
$form->setDecorators(array(array('ViewScript', array('viewScript' => 'one/formlayout.phtml'))));
$this->view->form = $form;
}
application/views/scripts/index/formtest.phtml
<h1>Formtest</h1>
<?
echo $this->form;
?>
The above code does not throw any errors or render any part of formlayout.phtml including the form tags or text between the p tags.
Can anybody tell me what I might be doing wrong?
I think the problem is your form element's decorator. You should set the decorator to ViewHelper and Error only. It works for me at least.
Here is the code I used and it should work
applications/forms/Form.php
class Application_Form_Form extends Zend_Form {
public function loadDefaultDecorators() {
$this->setDecorators(
array(
array(
'ViewScript',
array(
'viewScript' => 'index/formlayout.phtml',
)
)
)
);
}
public function init() {
$this->setAction('/action');
$this->setMethod('post');
$this->addElement('text', 'name', array(
'decorators' => array('ViewHelper', 'Errors')
));
}
}
application/views/scripts/index/formlayout.phtml
<form action="<?php echo $this->element->getAction(); ?>" method="<?php echo $this->element->getMethod(); ?>">
<div>
<label for="name">Box Name</label>
<?php echo $this->element->name; ?>
</div>
<input type="submit" value="Submit Message" id="submitbutton" class="bluebutton">
</form>
application/views/scripts/index/index.phtml
<!-- application/views/scripts/index/index.phtml -->
<?php echo $this -> form; ?>
application/controllers/IndexController.php
public function indexAction() {
$form = new Application_Form_Form();
$this -> view -> form = $form;
}
Here is a very simple example to get you going adapted from this article.
The form:-
class Application_Form_Test extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
$this->setAction('/');
$text = new Zend_Form_Element_Text('testText');
$submit = new Zend_Form_Element_Submit('submit');
$this->setDecorators(
array(
array('ViewScript', array('viewScript' => '_form_test.phtml'))
)
);
$this->addElements(array($text, $submit));
$this->setElementDecorators(array('ViewHelper'));
}
}
The order in which setDecorators(), addElements() and setElementDecorators() are called is very important here.
The view script _form_test.phtml can be called anything you like, but it needs to be in /views/scripts so that it can be found by the renderer.
/views/scripts/_form_test.phtml would look something like this:-
<form id="contact" action="<?php echo $this->element->getAction(); ?>"
method="<?php echo $this->element->getMethod(); ?>">
<p>
Text<br />
<?php echo $this->element->testText; ?>
</p>
<p>
<?php echo $this->element->submit ?>
</p>
</form>
You instantiate the form, pass it to the view and render it as usual. The output from this example looks like this:-
<form id='contact' action='/' method='post'>
<p>
Text<br />
<input type="text" name="testText" id="testText" value=""></p>
<p>
<input type="submit" name="submit" id="submit" value="submit"></p>
</form>
That should be enough to get you started creating your own forms.
Usually, if you don't see anything on the screen it means that some sort of error happened.
Maybe you have errors turned off or something, maybe not. I'm just trying to give you ideas.
The only things I could spot where the following.
In the below code, you have to still specify the form when trying to print out the elements.
<form>
action="<?php $this->escape($this->element->getAction()) ?>"
method="<?php $this->escape($this->element->getMethod()) ?>" >
<p>
Please provide us the following information so we can know more about
you.
</p>
<?php echo $this->element->getElement( 'name' ); ?>
<?php echo $this->element->getElement( 'submit' ) ?>
</form>
As vascowhite's code shows, once you are inside the viewscript, the variable with the form is called element. The viewscript decorator uses a partial to do the rendering and thus it creates its own scope within the viewscript with different variable names.
So, although in your original view it was called $form, in the viewscript you'll have to call it element.
Also, maybe it was copy/paste haste, but you used <? ?> tags instead of <?= ?> or <?php ?> tags. Maybe that caused some error that is beyond parsing and that's why you got no output.

Zend Framework - Flashmessenger - Only one character

I have a little problem with FlashMessenger. When I want to retrieve the messages in my layout, it writes the first letter of the message... example "test" displays "t".
I tried a solution posted in this question, but nothing changed for me.
I use php 5.3.6
Here is my code:
In my method :
$message = 'test';
$this->_helper->FlashMessenger($message);
Call in the Layout
<div id="message_box">
<?php echo $this->flashMessages(); ?>
</div>
Can someone help me?
Try this:
In controller:
$this->_helper->FlashMessenger->addMessage("Your message", 'actions');
// you can redirect to another controller ...
$this->view->messages = $this->_helper->FlashMessenger->getMessages('actions');
In phtml file:
<!-- some html code -->
<div id="message_box">
<?php echo $this->messages[0]; ?>
</div>

how can strips html tags in joomla

hi i am working on joomla. and i used the latest news module but it emerging a problem that the new display on page having the tags like paragraph i wanna remove that tags from the news
my code is
<?php // no direct access
defined('_JEXEC') or die('Restricted access'); ?>
<?php $i=1 ;?>
<?php foreach ($list as $item): ?>
<?php if($i==1) {echo "<div class='latestnews_ttl'> <p>".$item->cat_title." </p></div>";} $i++; ?>
<div class="news_box">
<p style=" padding:0px;"><strong><?php echo $item->text; ?> </strong></p>
<p><?php if(strlen($item->introtext)>100)
{
$txt = str_split($item->introtext, 100);
echo $txt['0']."...";
}
else{
echo $item->introtext;
}
?></p>
<div class='readmore'>read more</div></div>
<?php endforeach; ?>
It will give news with prefix tag "<p>" so please give the solution of it.
thanks in advance
Tried strip_tags() ?
Always remember in Joomla! never to modify core code, always overwrite the core templates by following the following guide http://docs.joomla.org/Tutorial_talk:Template_overrides
1: http://docs.joomla.org/Tutorial_talk:Template_overrides or a quick how to http://www.techportal.co.za/joomla/joomla-tutorials/124-how-to-modify-the-appearance-of-the-category-list-page-without-having-modifications-done-to-the-joomla-core-code
If you do this in the core it will work, but once you may upgrade your changes could be gone.
Since joomla is written in php you could use strip_tags