Using set_value + another value in codeigniter - codeigniter

I've got a login form and I'm using Tank Auth for form validation, etc. For the form, I'm not using labels; instead I am populating the value field to tell users what to input (e.g., Email Address in the "email" field). But Tank Auth presets the value with a set_value function:
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login'),
);
Is there any way to keep the set_value function, but also enter a value that can be used in place of a label? Or is there some other way to get users to see the instructional text without getting rid of the set_value function?

You are able to set a second argument for set_value().
For example, you could write this:
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login', 'You\'re username'),
);
To set the preset value (if nothing else has been entered) to You're username

Related

Magnento module file upload not working

have a Magento module that takes an input and creates a new item off that input. Everything works fine, and I wanted to add a field that would input an image with the item. I made sure to add the corresponding database column, and made sure to make the change every where it would be needed, but when i upload an image through the new input the image field does not send in my post data. I still get an object with all the fields and values from my form but the File field is always omitted. Using $_FILE to try to access it yields nothing also.
Here is the form:
$form = new Varien_Data_Form(array(
'id' => 'edit_form',
'action' => $this->getUrl('*/*/edit', array('embroidery_id' => $this->getRequest()->getParam('embroidery_id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
));
$form->setUseContainer(true);
$this->setForm($form);
$fieldset = $form->addFieldset(
'general',
array(
'legend' => $this->__('Details')
)
);
And here are the fields:
$this->_addFieldsToFieldset($fieldset, array(
'name' => array(
'label' => $this->__('Name'),
'input' => 'text',
'required' => true,
),
'file_t' => array(
'label' => $this->__('File path'),
'required' => false,
'class' => 'disable',
'input' => 'file',
'name' => 'file_t',
'value' => 'file_t',
),
));
Any help or guidance would be appreciated. i already did some googling and looked at all the top results for adding in a file upload field, and I copied some of the code exactly but the post field for the file input is still empty.
For Clarification:
The _addFieldsToFieldset function just loops through the fields array and either fills them in with data or adds the field onto the $fieldset variable. I also tried adding the field straight onto the variable with the following code but it did not work also.
$fieldset->addField('file_t', 'file', array(
'label' => $this->__('File path'),
'required' => false,
'name' => 'file_t',
));
Here is the function that handles the saving. This function does work, it has been tried and tested multiple times when I submitted other forms, and all the fields were successfully saved, and everything was sent through, but whenever I add the file field on, the post data looks exactly the same, and everything still works, it just does not send the file field through the post.
if(isset($_FILES['file_t']['name'])){
echo 'TRUE';
}
var_dump($this->getRequest()->getPost()); exit();
$em->addData($postData);
$em->save();
$this->_getSession()->addSuccess(
$this->__('The embroidery has been saved.')
);
// redirect to remove $_POST data from the request
return $this->_redirect(
'mu_em_admin/embroidery/edit',
array('embroidery_id' => $em->getID())
);
I simplified alot of the code down to only include the important parts. I also have the var_dump function there to troubleshoot the code. The results of var_dump were the whole post array that was sent, except for the file field.
please try to put this form before body end and then submit.
what web browser do you use, and which version of php?

Create form in Controller using Sonata field type

In Symfony admin, I have a form, where the second field type depends on the ChoiceField value selected. The second field can be of Symfony Url field type or sonata provided sonata_type_model_list field type.
I have created an ajax request to My Bundle Controller to return the form, which contains the needed field.
> /src/MyBundle/Controller/MyController.php
namespace MyBundle\Controller
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Sonata\AdminBundle\Form\FormMapper;
class MyController extends CRUDController
{
public function getFieldAction()
{
//getting the value of choice field
$type = $this->get('request')->get('type');
//sonata.admin.reference is a service name of ReferenceBundle admin class
$fieldDescription = $this->admin->getModelManager()
->getNewFieldDescriptionInstance($this->admin->getClass(), 'reference');
$fieldDescription->setAssociationAdmin($this->container->get('sonata.admin.reference'));
$fieldDescription->setAdmin($this->admin);
$fieldDescription->setAssociationMapping(array(
'fieldName' => 'reference',
'type' => ClassMetadataInfo::ONE_TO_MANY,
));
// Getting form mapper in controller:
$contractor = $this->container->get('sonata.admin.builder.orm_form');
$mapper = new FormMapper($contractor, $this->admin->getFormBuilder(), $this->admin);
$form_mapper = $mapper->add('reference', 'sonata_type_model_list', array(
'translation_domain' => 'ReferenceBundle',
'sonata_field_description' => $fieldDescription,
'class' => $this->container->get('sonata.admin.reference')->getClass(),
'model_manager' => $this->container->get('sonata.admin.reference')->getModelManager(),
'label' => 'Reference',
'required' => false,
));
//#ToDo build $form from $form_mapper
return $this->render('MyBundle:Form:field.view.html.twig', array(
'form' => $form->createView(),
));
}
}
I cannot find any method in Sonata\AdminBundle\Form\FormMapper class to build a form (it seems to be possible with create() method, but it only works with common Symfony field types, not Sonata form field types, which are commonly generated in Block or Admin classes).
Is it possible to use Sonata\AdminBundle\Form\FormMapper in the Controller to build a form?
Or is there another way I can build a form with Sonata form field types in the Controller?
You should not use a controller, rather a Sonata Admin Service.
Sonata provides you with the 'sonata_type_choice_field_mask' type which allows you to change the fields displayed on the form dynamically depending on the value of this 'sonata_type_choice_field_mask' input.
Here is the doc where you can find everything about sonata types and the choice field mask.
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('reference', 'sonata_type_choice_field_mask', array(
'choices' => array(
//The list of available 'Reference' here
'choice1',
'choice2'
),
'map' => array(
//What you want to display depending of the selected option
'choice1' => array(
// List of the fields displayed if choice 1 is selected
'field1', 'field3'
),
'choice2' => array(
// List of the fields displayed if choice 2 is selected
'field2', 'field3'
)
),
'placeholder' => 'Choose an option',
'required' => false
))
->add('field1', 'sonata_type_model_list', array(/* Options goes here */))
->add('field2', 'url', array(/* Options goes here */))
->add('field3')
;
}
It looks like you have access to the admin which directly handles building the form with Sonata\AdminBundle\Admin\AbstractAdmin::getForm.
$form = $this->admin->getForm();

cakephp saveAssociated and validation foreign_key fails

I have two models that we're going to name Model and RelatedModel. Model has many RelatedModel. So if I add foreign key validation on validation array like:
public $validate = array(
'foreignKey' => array(
'rule' => 'numeric',
'required' => true,
'message' => 'The id of relatedmodel should be a number'
)
)
After I create a add() function to save new registers and in this function I use saveAssociated with validation true, this one fails throwing an error 'The id of relatedmodel should be a number'.
I'm debugging the code and saveAssociated checks validation of both models at the same time and before save Model.
Is this an issue?
I think what this function should do is to validate Model, save it, add foreignKey of RelatedModel and then validate it before save.
I came into this issue only recently. It's not an issue, saveAssociated() is designed to work this way unfortunately.
What you can do is alter the required => true on the fly using the model validator. Check out the book for more information.
http://book.cakephp.org/2.0/en/models/data-validation.html#dynamically-change-validation-rules
This is working as would be expected with your given rule. required in Cake means it expects the value of foreignKey to be set in the save data prior to saving. All the validation will happen before Cake saves the data (and therefore before foreignKey is generated).
You shouldn't need to validate that it is numeric if you are allowing Cake to generate this for you behind the scenes. If you want to check that it is being passed in the data for an UPDATE you could modify the required to be only for an update like this:-
public $validate = array(
'foreignKey' => array(
'rule' => 'numeric',
'required' => 'update',
'message' => 'The id of relatedmodel should be a number'
)
)
Personally I wouldn't bother validating foreign keys unless a user is setting them rather than Cake.
Update:
To validate the foreignKey if it exists in a form submission you can drop the required option from the validation rule:-
public $validate = array(
'foreignKey' => array(
'rule' => 'numeric',
'message' => 'The id of relatedmodel should be a number'
)
);
This will allow you to pass data where the foreignKey is not present without throwing a validation error whilst validating it if it is.

Cakephp custom validate return in textarea

I would like to use custom validation for textarea because there will be a Slovak accents but when I will enter the new line throws me an error.
Please advise how to validate enter button? thank you
var $validate = array('text'=>array('custom'=>array(
'rule' => array('custom', '/^[a-zA-Z0-9cšltžýáíéúóäônd".:,´()CŠLTŽÝÁÍÉÚÓND ]*$/i'),
'message' => 'Zadávajte prosím len čísla alebo písmená')
)
);
You need a custom validation rule and I would suggest externalizing that to a model function.
Also your inline validation seems a bit broken to me.
So what you can do:
var $validate = array(
'text' => array(
'rule' => 'myNewCustomRule',
'message' => 'Zadávajte prosím len čísla alebo písmená'
));
And the function in the model:
function myNewCustomRule($custom) {
//You may need to add here because the $data array is passed using the form field name as the key, you will have to extract the value to make the function generic
//array_shift($custom);
return preg_match('/^[a-zA-Z0-9cšltžýáíéúóäônd".:,´()CŠLTŽÝÁÍÉÚÓND ]*$/i', $custom);
}

CakePHP auth and validation

I have an app that uses Auth component. Logedin members can alter their data as long as I have no validation rules in users model. When I add array $validate in model logedin users cannot submit data to database.
I use one mysql table named users.
In other words this works but I don't have validation in signup view
<?php
class User extends AppModel {
var $name = 'User';
?>
But when I add validation like this:
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'email' => array(
'email' => array('rule' => 'email','required'=>true,'message' => 'Enter proper mail')
)
);
}
?>
validation in signup view works but users in secret area cannot enter data to database.
My guess is: This is happening because you have set required to true.
This enforces the rule that when submitted data of the User model, the email key needs to be set. Therefore, this works in your Sign Up form which obviously has the email key. On the other hand, the form that you're using in the secret area probably does not have an email field.
Just remove the "required" condition from your validation rule:
'email' => array(
'email' => array(
'rule' => array('email'),
'message' => 'Please enter a valid email',
),
),
Let me know if this works for you.

Resources