I'm using Tank Auth (it's working fine), but I can't get the form validation function to work when I include any value in the variable array along with set_value. I need these values as a label for form fields--they tells the user what to enter in the field.
How can I keep Tank Auth's form validation functionality AND use a value in the value field?
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login', 'Enter username'),
);
this populates field values : set_value('field_name');
this shows individual errors: form_errors('field_name');
If you use html5, you can simply add a placeholder!
{ placeholders should not be used as an alternative to labels!}
$login = array(
'name' => 'login',
'id' => 'login',
'value' => set_value('login'),
'placeholder' => 'Enter Username',
'required' => 'required'
);
Related
I used Laravel Form validation for validate form:
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users',
'email' => 'required|email|max:50|unique:users',
'mobile' => 'required'
);
this is working well on some thing add. but when i going to edit unique:users part given error "already been taken", so how to write validation to check exclude edit row.
If I wrote this way, does it work?
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,'.$id,
'email' => 'required|email|max:50|unique:users,'.$id,
'mobile' => 'required'
);
#user3099298 Your second set of code looks alright.
$rules = array(
'user_name' => 'required|min:3|max:20|unique:users,user_name,' . $user_id, // $user_id = Currently being edited user id.
'email' => 'unique:users,email_address,' . $user_id, // $user_id = Currently being edited user id.
'mobile' => 'required',
);
I have added column names in rules - may be you can try that one.
Please check here the documentation
Where they explain how to ignore some rules by providing ID.
I have a Job model where I want to submit a form to my other action (not to save the data directly into the db just to pass it to my other action) and I should check whether the user has selected at least 1 checkbox.
Here is my view's snippet:
echo '<tr class="v-top">
<td colspan=2>'.$this->Form->input('system_codes_filter', array(
'type' => 'select',
'multiple' => 'checkbox',
'label' => '',
'name' => 'system.codes.filter',
'class' => 'floatLeft',
'options' => array('System codes' => $this->Submit->getSystemCodes($systemcodes)),
'selected' => array('*'))).
'</td></tr>';
Here is my model's snippet:
public $validate = array(
'system_codes_filter' => array(
'rule' => array('multiple', array(
'min' => 1,
'message' => 'Please select at least on system code!'
)
)
)
);
As far as I know the validation is called when I want to use $this->Job->save(), but I don't use this in my action.
Unfortunately this does not work. When I submit my form without selecting any checkboxes, it let's me do that and if I look at the $this->validationErrors array, it is empty. What am I doing wrong? Thanks in advance.
I have a form with multi select box:
echo $this->Form->input('emp_id', array( 'options' => array( $arr),
'empty' => '(choose one)',
'div'=>'formfield',
'error' => array( 'wrap' => 'div',
'class' => 'formerror'
),
'label' => 'Team Members',
'type' => 'select', 'multiple' => true,
'style' => 'width:210px; height:125px;'
));
I selected multiple values from this list box and click the SAVE button.
But it displays the validation message.
How can i solve this?
class TravancoDSRGroup extends AppModel {
var $name = 'TravancoDSRGroup';
var $useTable = 'dsr_group'; // This model uses a database table 'exmp'
var $validate = array(
'emp_id' => array(
'rule' => 'notEmpty',
'message' => 'The employee field is required'
)
);
}
This is the model code....
If it is possible...?
You don't have to explicitly specify
'type' => 'select'
if you set 'emps' from the controller, it will allow cake's automagic to work. just add
$this->set(compact('emps'));
Post output of debug($this->request->data) for better understanding of problem.
Have you defined hasMany or HABTM relationships to the emp ? To have multiple values saved you will have to define hasMany or HABTM relations, if you wish to save it as CSV then you will have to do the processing yourself in 'beforeSave'.
In the user registration form, I have password and password2 so that user has to enter password 2x and they have to equal each other before he/she is registered. The validation rules work fine in this case. In the update user info form. I have both password and password2 set to blank so that if the user does not enter a value in the first password field or password2 field, then the system simply saves the current password which I have stored in a temporary variable in my user model. So far so good. What I want is to trigger all the same validation rules as when registering but ONLY if the user enters a value in the first password field, but leave blank and ignore otherwise. So I guess this is some kind of conditional validation.
In the user model:
$validate = array(
'password' => array(
'password_notempty'=>array(
'rule' => 'notEmpty',
'message' => 'Required field',
'on' => 'create')
,
'password_between'=>array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters',
'on' => 'create')
,
'password_alphanumeric'=>array(
'rule' => 'alphaNumeric',
'message' => 'Characters and numbers only',
'on' => 'create')
),
'password2' => array(
'password2_isequal' =>array(
'rule' => array('comparePasswords','password'),
'message' => 'Must be same value as password.')
)
);
Anyone have ideas on how to accomplish this?
this should help getting you started:
http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/
you can also look at the code for examples on how to create the validation needed.
basically you always use a different field and only map it to the real field if something is actually entered. also dont forget to include a confirmation field (to retype and confirm the correctly typed password)
the article describes how you can achieve all that will a behavior in 3-4 lines of code.
read below url :-
http://book.cakephp.org/1.3/view/1143/Data-Validation
http://book.cakephp.org/1.3/view/1152/Core-Validation-Rules
//Or Try this:-
$validate = array(
'new_password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'password' => array(
'between' => array(
'rule' => array('between', 5, 20),
'allowEmpty' => false,
'message' => 'You password must be between 7 and 20 characters long')),
'retype_password'=>array(
'rule'=>array('equalTo','password'),
'message'=>'Password does not match'),
'repeat_password'=>array(
'rule'=>array('equalTo','new_password'),
'message'=>'Password does not match'),
);
I read your answer about cakephp model alias and I did on my model what you wrote, I debuged my view and it list what I want, but it doesn't show the list on the respective field, do you have any idea what is wrong?
My model:
'ResponsavelManutencao' => array(
'className' => 'Operador',
'foreignKey' => 'responsavel_manutencao_id',
'conditions' => '',
'fields' => '',
'order' => '')
My controller:
function add(
$responsavelManutencao = $this->Dentista->ResponsavelManutencao->find('list');
$this->set(compact('responsavelManutencao'));)
My view:
echo $this->Form->input('responsavel_relacionamento_id', array(
'class' => 'field text small', 'empty' => '',
'after' => 'Funcionario responsavel por manter o contato comercial.'));
debug($responsavelRelacionamento);
I guess there is something wrong with the CakePHP singular/plural rules for your language. I think you should name your view variable "responsavelRelacionamentos" for it to work.
However, the safest way to accomplish your goal without inflection magic would be:
echo $this->Form->input('responsavel_relacionamento_id', array(
'options' => $responsavelRelacionamento,
'class' => 'field text small', 'empty' => '',
'after' => 'Funcionario responsavel por manter o contato comercial.'
));