Codeigniter form validation - how to reject default values - php

I have a form with default values set, e.g.:
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail', 'jdoe#example.com'); ?>" />
I validate the form using Codeigniter's form_validation class. For this field:
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email'
));
Here's my problem. When the user leaves the field untouched and submits it's default value 'jdoe#example.com', I want to reject this value. I know I can use a callback, but that would mean I have to write a callback for every field of the form. Not nice!
I've looked into the validation rules. 'differs' looked promising, but it only checks whether one field is different from another field.
Now what's the best way to deal with default values in Codeigniter?

Sounds like you should be using a placeholder instead of a default value. Otherwise what you are asking for doesn't make much sense.
<input type="text" id="mail" name="mail" value="<?php echo set_value('mail'); ?>" placeholder="jdoe#example.com" />
Otherwise you could try the built in validation rule "in_list" e.g. in_list[red,blue,green]
$config = array(
array(
'field' => 'user',
'label' => 'Naam',
'rules' => 'trim|required|valid_email|in_list[jdoe#example.com]'
));
EDIT: Actually you would want the opposite of the in_list rule, i think that would require a callback, but you would only need to write one callback to use for each field.

Related

Using Codeception with form input array

So I am trying to use Codeception on a form where I have multiple inputs that have a name like such.
<input type="text" name="flavours[]" >
I have tried
$I->fillField('flavors[]', 'Blue Razberry');
However Codeception returns
Couldn't fill field "flavors[]","Blue Razberry":
InvalidArgumentException: Unreachable field "" unable to find a field with that name.
Is it possible in Codeception? If not, is there another way?
Thanks in advance!
You can you submitForm method instead
$I->submitForm('#name_of_your_form', array(
'field' => 'value',
'field' => 'value'
));
Use it:
$I->fillField('flavors', 'Blue Razberry');

Review form is not being submitted

In my project, I added a new field location in "product reviews" of admin panel going through the following steps as explained in many blogs.
Created a new field in database table review_detail as location.
Added the following code in app/code/code/Mage/Adminhtml/Block/Review/Edit/Form.php
$fieldset->addField('location', 'text', array(
'name' => 'location',
'label' => Mage::helper('adminhtml')->__('Location'),
'required' => false
)
);
Just above:
$fieldset->addField('nickname', 'text', array(
'label' => Mage::helper('review')->__('Nickname'),
'required' => true,
'name' => 'nickname'
));
.Added the following code in app/code/core/Mage/Review/Model/Resource/Review.php
$detail = array(
'title' => $object->getTitle(),
'detail' => $object->getDetail(),
'nickname' => $object->getNickname(),
'location' => $object->getLocation() /* added */
);
Added "location" in below function array. In the file: app/code/core/Mage/Review/Model/Resource/Review/Collection.php
protected function _initSelect()
{
parent::_initSelect();
$this->getSelect()
->join(array('detail' => $this->_reviewDetailTable),
'main_table.review_id = detail.review_id',
array('detail_id', 'title', 'detail', 'nickname', 'customer_id','location'));
return $this;
}
Added the following in {$mytheme}/template/review/form.phtml:
<li>
<label for="location_field" class="required"><em>*</em><?php echo $this->__('Location') ?></label>
<div class="input-box">
<input type="text" name="nickname" id="location_field" class="input-text required-entry" value="<?php echo $this->htmlEscape($data->getLocation()) ?>" />
</div>
</li>
My problem is that though I can see a new field in admin panel, whenever I submit a review form it is not being submitted/stored in database.
I even re-indexed and cleared the cache.
What should I change more to make it work properly?
Please help... I am on magento 1.8.
PS: I know core files should not be changed. I will override this to new module once I have success in this issue.
I did follow exact steps explained in quetion. And find it working properly.
Only issue I faced was that in {$mytheme}/template/review/form.phtml
You have defined name="nickname" for location field instead of name="location"
Correct this and if you still face same issue than then check if Module Classes as being overridden.
Have a look at the html code created in the browser. Check for:
is your field included within -tags?
is the action type and target set correctly?
with a browser console of your choice (e.g. chrome F12) ensure that fields are correctly set and the form is really sent.
Try this, take db backup first. Delete entry of table from core_resource table and load the site.. In short try to recreate the db table with your column 'location'. I don't know ,what is wrong with setters when we add new field in varien forms they didn't work properly.
I hope this will work.

Localization of array value in CakePHP

I am trying to localize an existing cakephp application. For the most part I have it working the way I want but there is one scenario I can't figure out.
I am using the form helper to create a form like this:
echo $this->Form->create('search', array('url' =>
array('controller' => '/',
'action' => '/search/searcher'),
'onsubmit'=>'return checkForm();',
'class'=>'find-form'));
echo '<fieldset>';
echo $this->Form->input('name', array(
'type'=>'hidden',
'div'=>false,
'id'=>'name',
'class'=>'nice',
'label'=>false,
'value'=>''
));
echo $this->Form->input('myvalue', array(
'type'=>'text',
'div'=>false,
'id'=>'searchval',
'class'=>'nice',
'value'=> __('Enter search string'),
'label'=>false));
The problem is with the line:
'value'=> __('Enter search string'),
The resulting html looks like this:
...
<fieldset>
<input type="hidden" name="data[search][name]"
id="name" value="" />
Enter Search String
<input name="data[search][myvalue]"
type="text"
id="searchval"
class="nice"
and so on.
The value "Enter Search String" is translated properly but it is outside of the input tag so it shows up outside of the search box.
I've tried various permutations of the code like:
'value'=> echo __('Enter search string'),
'value'=> `__('Enter search string')`,
And nothing seems to work. As I said it works in other areas of the view (in as an array value though) but I can't get this one working.
BTW, the code before I started looked like this:
'value'=> 'Enter search string',
Any ideas?
Thanks for your time.
In CakePHP 1.x the __() function echoes the content by default, while in this case you only want to store it. You can use the second argument to return rather than echo the contents, like this:
'value' => __('Enter search string', true),
From CakePHP 2.0 on upwards you should no longer have to do this, as it returns by default.

cakephp input type number

I have a mobile website form that I want to add type attributes to the inputs so that there correct keyboard format will pop up.
However in cakephp setting the type as number a textarea is created instead of the input and the type is not set.
Setting type as text does work.
How do I overide this and have cakephp just keep it as a text input with type=number?
<?php echo $form->input('phone',array('type' => 'number')); ?>
Result:
<textarea id="UserCardExpires" rows="6" cols="30" name="data[User][card_expires]"class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset"></textarea>
This is ok:
<?php echo $form->input('postcode' ,array('type' => 'text')); ?>
Result
<input type="text" id="UserPostcode" name="data[User][postcode]" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset">
On older versions of Cake, the Form helper won't automagically interpret $options['type'] as the HTML5 input-element type attribute. You have to force it by using "type" as an option on an explicit text element.
Use the following:
$form->text( 'phone', array( 'type' => 'number' ) );
I think phone numbers might be:
echo $form->text( 'phone', array( 'type' => 'tel' ) );
EDIT:
Sorry I'm an idiot, thats HTML5.

CodeIgniter: Validate form with multidimensional POST data

So the framework is CodeIgniter 2.0.2. I have a form that has groups of fields that correspond to rows in a database. The names of the fields are in the format:
opt[0][foo]
opt[0][bar]
opt[1][foo]
opt[1][bar]
etc...
The index (1,2,etc...) does not correspond to row IDs in the database, it is simply a way to split up the groups of fields. There may be gaps in the index as users are able to add and remove an arbitrary number of the field groups. All groups are identical, that is, they contain exactly the same set of fields with the same second level names.
I want to be able to use CodeIgniter's validation library to validate the form and (p)re-populate as necessary. I've found plenty of posts (in addition to the excellent CI user guide) on the pre-populating and I know how to get the working with the re-populating in general. However, this is the first time I've had to try it with the indexed field names as above. I've tried the below and it doesn't work:
array(
'field' => 'opt[][foo]',
'label' => 'Foo',
'rules' => 'required'
)
I'm guessing I was just hoping for too much and CodeIgniter doesn't support what I need it to do. Extending the existing form validation library is an option so if anyone has been in the same situation and can provide some tips that would be very welcome.
UPDATE:
Just a little extra info, I've also tried validating a specifically indexed field (see below) and that also didn't work... As I understand it multidimensional validation should work in the specific case:
array(
'field' => 'opt[0][foo]',
'label' => 'Foo',
'rules' => 'required'
)
The following controller code works for me on CI 2.0.2
public function test() {
$this->load->library('form_validation');
$this->load->helper('form');
$this->form_validation->set_rules('test[test1][test2]', 'Test', 'required|valid_email');
$this->form_validation->run();
echo validation_errors();
echo form_open($this->uri->uri_string());
echo form_input('test[test1][test2]', set_value('test[test1][test2]'));
echo form_submit();
echo form_close();
}
You can use this to loop through the opt variable and set validation rules for each input.
if(!empty($opt))
{
foreach($opt as $id => $value)
{
$this->form_validation->set_rules('opt[' . $id . '][foo]', 'Foo', 'required');
$this->form_validation->set_rules('opt[' . $id . '][bar]', 'Bar', 'required');
}
}
You should take a look at the callback functions for the validating class - this should help you accomplish what you need for validation.

Categories