I have my form fields where I am appending an id within a for loop.
<?php echo $this->Form->input('Shipment.current_city'.$sh, array('label' => 'City')); ?>
I would like to know how to validate such dynamic fields. Currently Cake is not recognizing my validation rules from my model due to the appended id.
Thanks.
You can find the solution of your issue here.
http://php-dev-zone.blogspot.com/2013/08/dynamic-fields-validation-in-cakephp.html
Related
So I have a form in my view:
{{Form::file('projectPicture', ['class' => 'uploadedImage', 'data-some-attribute' => ''])}}
with the attribute data-some-attribute.
And in my route I retrieve it like so:
$request->file('projectPicture');
How do I get a data-some-attribute in the route? Is it even possible?
I know I can use ajax to pass any data, but can it be avoided in this case?
Thank you!
It is not possible how you intend it to work, only because it's not how form data is working under the hood. The second argument in your sample Form::file is just decorating the rendered form element. It has no correlation with the form data that is transferred between the server and client.
For all intents and purposes form data is just a glorified set of key value pairs. If you wanted to pass some-data-attribute to your route controller, you have two options -
Add another form field, and make it empty using Form::hidden. In this case, you would just name the field some-data-attribute.
If your form is submitted through a POST method, you can tack on some-data-attribute onto the form's route and retrieve it from the request.
ie - your/route becomes your/route?some-data-attribute=whatever, and you can retrieve it later with something like $request->input('some-data-attribute').
I have set in form_validation.php the array that validates my form.
However, I have some dynamic fields that may (or may not) be inserted into this array.
So I use the following code, I want to know if this is the right way
if($this->form_validation->run('agenda_de_contatos')){
if($this->input->post('contato[]')!=""){
$this->form_validation->set_rules($this->input->post('contato[]'), 'USC_CONTATO', 'trim|xss_clean');
$this->form_validation->set_rules($this->input->post('contato_tipo[]'), 'USC_TIPO', 'trim|xss_clean');
}
if($this->form_validation->run()){...}
Or if I can somehow add itens to the original array and then run form validation?
Thank's,
So i have this field i want to keep hidden in my form.
For this purpose i have tried the following:
<?php echo $this->Form->input('group_id', array('hiddenField' => true, 'value'=> 2)); ?>
I also tried:
<?php echo $this->Form->input('group_id', array('options' => array('hiddenField'=> 'true'), 'value'=>2 )); ?>
How ever i still see the input field..
What am i doing wrong?
You misread the documentation, I assume.
hiddenField is to enable/disable specific hidden fields for specific form fields.
You are either looking for
$this->Form->hidden('group_id')
or
$this->Form->input('group_id', ['type' => 'hidden']);
I usually only use the latter.
See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html
BUT - that said - you shouldnt actually use either one of those. And omit any fields that serve no real purpose for the view and its form.
Instead you should inject those fields into the data array prior to saving.
See http://www.dereuromark.de/2010/06/23/working-with-forms/
If you are looking to add a hidden field that uses a related second data array that will not be passed via post or put by default, you can use this to pass it:
echo $this->Form->hidden('Group.name');
This is useful for echoing out edit page titles when the post or put encounters an error. A dynamic title can lose Group.name data array when your form is set up such as this:
<h1>Edit Group - <?php echo h($this->request->data['Group']['name']); ?></h1>
For data that is to be saved to db however, follow Mark's suggestion above.
Try following code in cakephp 3 to set hidden field
<?php
echo $this->Form->hidden('name');
?>
I'm new to Codeigniter and found my first trouble when validating form. There's validation form library that helps to do it, and it has function to set rules, for example:
$this->form_validation->set_rules('username', 'Username', 'required');
I can't find in documentation anything about applying required rule to every field in my form (since it's generated dynamicly based on database content)
Thanks to Rooneyl here's the answer:
CodeIgniter doesn't support setting validation rules through all input fields. Altho it is possible to use set rule inside a loop. Luckily for me, my form was generated from database content, so looping through same table elements made generating rules easier for me.
Hovewer, if form isn't dynamicly generated, we can always loop through post (taken from CI forum):
foreach($_POST as $p) {
$this->form_validation->set_rules($p, ucfirst($p), 'required|trim');
}
I have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);