We're using Zend_Filter_Input to validate a Dojo From at Backend. There is "option" Input Element where we have to verify that the submitted value is allowed.
Problem: If nothing is selected the Zend_Validate_inArray Validator returns
" you must provide an non Empty value"
thats fine, but we have to change the message. I cant find the proper way to do this..
'FIELD' => array(new Zend_Validate_InArray($allowedValues),
'messages' => 'MESSAGE_WRONG_VALUE',
'default' => ''
),
Does someone know how to change the "isEmpty" Message?
It is Zend Framework 1.11.12
Try this:
$element->setRequired(true)->addErrorMessage('Your message');
Related
I'm trying to run this code:
if(!$this->isChild()) {
$formMapper->add('post', 'sonata_type_model', array(), array('edit' => 'list'));
From this tutorial: http://sonata-project.org/bundles/doctrine-orm-admin/2-1/doc/tutorial/creating_your_first_admin_class/defining_admin_class.html
I'm aware that you have to use sonata_type_model_list as of 2.1
sonata_type_model_list : this type replaces the option edit = list provided as a 4th argument on the sonata_type_model
The problem is that I have absolutely no idea how to do that. I have found ZERO examples anywhere after a whole day of google searches. All I want to do is replace the edit=>list with sonata_type_model_list.
Can you please tell me how to do that in the code above?
This is how I used it in my code. However it's not working in all browsers. When I select the taget entity the form value in the parent view doesn't get updated (FireFox and IE).
$formMapper->
...
->add('image', 'sonata_type_model_list',
array(
'compound' => true,
'by_reference' => true
)
)
...
I also find it very hard to find some tutorials/examples on how to use this type. Best thing you can do is to go through their source code. Which is awful time consuming.
One way I found out how to configure these form types is to provide a wrong argument.
e.g. 'my_compound' => true,
This will result in an error telling you that 'my_compound' isn't a valid parameter and also will show you a list of valid parameters.
Hope this helps!
I have just started zend framework 2. I just want to know how to customize message in form for elements having require ON(true). Right now its showing "Please fill out this field" (If the particular textbox is empty and I click the submit button).
I just want to change this message. Initially I thought this message is coming from library
but I was wrong. Can this possible?
Please provide the method how you are creating your form. Ultimately you should simply overwrite the messages of the Validators. Each validator has the option to overwrite messages. The basic syntax is as follows:
// This assumes to be INSIDE a Validator
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => "Dude, it's empty. It shouldn't be!"
)
)
The example overwrites the NotEmpty validator message if no input is given. Furthermore you should know, that if you use the HTML5 Attribute required then some browsers will add a pre-submit-validation to your form and the error-message displayed by the browser cannot be changed.
$username = $this->createElement('text', 'username');
$username->addErrorMessage('The username is required!');
I'm fairly new to ZF.
I've been building a website on Zend Framework.
Everything looks good.
But I can't figure out how to resolve 1 problem, that actually is essential when developing article management module.
I've got form that has ZEND_TextArea that looks like this:
$full_text = new Zend_Form_Element_TextArea('full_text');
$full_text->setLabel('Description:')
->setOptions(array('rows' => '28','cols' => '40'))
->setRequired(true)
->addValidator('NotEmpty', true)
->addFilter('HTMLEntities')
->addFilter('StringTrim');
It work great, it has filter HTMLEntities that is really essential for filtering TextArea.
When displaying the saved data on the website I'm using html_entity_decode($item['full_text']) and it's fine.
But as soon as I try to edit it, it loads encoded text into my textarea, after editing it encodes my already encoded text - and on the front page I get terrible things like:
p;quot;color: #ff0000;">asdasda</
span>sdas <strong>sdfsdf&
lt;/strong>&
Maybe someone can help me figure out how to handle this problem, particularly load decoded data into Edit form of TextArea, so that my string doesn't get encoded twice, and when editing it was show in human manner and not into encoded one.
If you can provide code example - will be really AWESOME!!
thanks!!
Do not use HtmlEntities filter in edit form, if you use it in create form.
Create form :
$elements[] = $this->createElement('text','name',array(
'label' => 'test',
'filters' => array('HtmlEntities'),
));
Edit Form :
$elements[] = $this->createElement('text','name',array(
'label' => 'test',
'value' => html_entity_decode($value)
));
Your doing html_entity_decode() to set value, to show right 'name' to user..
When UPDATING data in model, you use htmlEntities filter again:
$data['name'] = $HtmlEntities->filter($data['name']);
This is a CakePHP / General PHP question.
In my application I use a query string like /login?continue=/admin/posts
This query string is used to redirect users to the URL in the query, but it doesn't work so it seems as though the app can't see the string...
This has got me wondering as basically when you arrive at the page with the string it's a GET request where as when you login, it becomes a POST or XML request (if using AJAX). Do I need to add the query string manually to the form for the POST to see it?
Either in the form action or a hidden input? Or am I barking up the wrong tree?
I'm currently grabbing the query like so:
if(isset($this->params['url']['continue']))
{
$pathtoredirect = $this->params['url']['continue'];
}
else
{
$pathtoredirect = $this->Auth->redirect();
}
But that's within the POST request so perhaps the query is lost... and adding it to a hidden input would not solve the problem with the current code so I would either change the code to look at the hidden field or pass the query with the action on the form?
e.g. <form action="/login?continue=/admin/posts" method="post">
Am I correct in thinking this? And would anyone be able to offer solutions or pros and cons of the two methods I mention?
In short I'm asking how to add the query string to my form action value
It currently looks like:
php echo $this->Form->create('User',
array(
'id' => 'loginform',
'type' => 'post',
'url' => array
(
'admin'=>false,
'controller' => 'users',
'action' => 'login'
)
)
);
So how would I add the query string to the form?
Thanks
When receiving a POST request you can receive both POST and GET variables through the superglobals $_POST and $_GET.
You can either send your paramater in $_GET by including it in the form's action attribute or send it in $_POST by creating an <input type="hidden"> tag within the form
A slash (/) is a reserved character. Encode it with %2F.
http://blooberry.com/indexdot/html/topics/urlencoding.htm#whatwhy
The solution was to do this:
<?php echo $this->Form->create('User',
array('id' => 'loginform', 'type' => 'post',
'url' => array('admin'=>false,'controller'=>'users','action'=>'login','url'=>$this->params['url']['continue']))); ?>
as I have a route already setup to handle the additional URL parameter:
Router::connect('/auth/login', array('controller'=>'users','action'=>'login'));
Router::connect('/auth/login?continue=:url',
array('controller'=>'users','action'=>'login'),
array(
'url' => '[A-Za-z0-9/\._-]+',
'pass' => array('url')
));
If anyone sees any issues with the way I do this though, please feel free to comment.
sfValidatorChoice is not working on multiple select element, my code
$this->form=new MyTestForm();
$options_array=array("php","python","java");
$widgetSchema["my_select"] =new sfWidgetFormChoice(array('choices' => $options_array,'multiple' => true,'expanded' => true ));
$validatorSchema["my_select"] = new sfValidatorChoice(array("choices" =>array_keys($options_array)));
Note: i have also tried using array_keys and by directly passing the array to sfValidatorChoice.
when i submit, it gives me Invalid error(when checked) and Required(when unchecked).
is there any error in parameters or is bug?
Firstly, you need to enable "multiple" in the validator as well as the widget:
"multiple" => true
To make having any selection optional, you need to set required to false:
"required" => false
Finally, I can't exactly remember how to use sfValidatorChoice (it's been a while), but I think it's best to make the values readable, so I'd do:
$options_array=array('php'=>'php','python'=>'python','java'=>'java');
I'm not certain this will fix the problem, but it may well do.