Form rules don't work - php

My controller:
public function rules()
{
return [
[['text'], 'required', 'message' => 'Fill in this field'],
['text', 'string', 'min' => 6, 'message' => 'Too short message'],
['text', 'validateUser']
];
}
public function validateUser($attribute)
{
if (Yii::$app->user->isGuest)
$this->addError($attribute, 'You must be logged in');
}
I have 2 problems:
1) Instead of the message 'Too short message' i see 'text field should contain at least 6 characters.'
2) validateUser not added a warning 'You must be logged in'

Ad1. It should be ['text', 'string', 'min' => 6, 'tooShort' => 'Too short message'],
Ad2. Are you sure this condition is true? Can you use die() here or something like that? Or this validation rule isn't even activated?

For validateUser
public function rules()
{
return [
[['text'], 'required', 'message' => 'Fill in this field'],
['text', 'string', 'min' => 6, 'message' => 'Too short message'],
['text', 'required', 'when' => function($model){
return (Yii::$app->user->isGuest);
},'message'=>'You must be logged in'],
];
}

Related

how do I change the validation message from 'required' on max character limit?

I keep getting the message 'required' when the character validation of 'max'is exceeded, but I have specified 'message' /'tooLong' to say "max character limit is 2.
Any help?
My model:
/**
* #return array validation rules for model attributes.
*/
public function rules()
{
return array(
[['question_id', 'survey_instance_id'], 'required'],
[['question_choice_id'], 'number'],
[['question_choice_id'], 'required', 'when' => function($QuestionAnswer) {
$Question = Question::findOne($QuestionAnswer->question_id);
return $Question->isMultipleChoice;
}],
[['question_choice_id'], 'exist', 'targetClass' => QuestionChoice::className(), 'targetAttribute' => 'id'],
[['answer'], 'string', 'max' => 1, 'tooLong' => 'Max character limit is 1'],
[['answer'], 'required', 'when' => function($QuestionAnswer) {
$Question = Question::findOne($QuestionAnswer->question_id);
return !$Question->isMultipleChoice;
}],
);
}
Well you can customize your error message.
public function rules()
{
return [
['username', 'required', 'message' => 'Please choose a username.'],
];
}

Prevent users without [activation having "true" value], to log in

How to prevent users log in without mail confirmation?
The database set correct. When user confirms the registration by clicking the link in the received message, the value "confirmed" changes from 0(default) to 1. Of course boolean type. I need "else if" statement to work, but I can't figure out how. Here is my code:
public function postSignin(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required'
]);
if (!Auth::attempt($request->only(['email', 'password']),
$request->has('remember')))
{
return redirect()->back()->with(notify()->flash("Ooops..", "error", [
'timer' => 5000,
'text' => 'Could not sign in with those details',
]));
}
else if ((Auth::attempt($request->only(['email', 'password'])))&&('confirmed'===0))
{
return redirect()->back()->with(notify()->flash("Ooops..", "error", [
'timer' => 5000,
'text' => 'Activate your account',
]));
}
else
{
return redirect()->route('home');
}
}
public function postSignin(Request $request)
{
$this->validate($request, [
'email' => 'required',
'password' => 'required'
]);
// This line will always log the user **in** if the account is confirmed or not.
if (!Auth::attempt($request->only(['email', 'password']),
$request->has('remember')))
{
return redirect()->back()->with(notify()->flash("Ooops..", "error", [
'timer' => 5000,
'text' => 'Could not sign in with those details',
]));
}
else if (!Auth::attempt(["email"=>$request->input("email"),"password"=>$request->input('password'),"confirmation"=>1])
{
return redirect()->back()->with(notify()->flash("Ooops..", "error", [
'timer' => 5000,
'text' => 'Activate your account',
]));
}
else
{
return redirect()->route('home');
}
}

CakePHP 3 Modelless form with dotted fields

In my form.ctp I have some inputs which are named with dot notation, so they are joined on the POST to the same array inside $this->request->data.
The problem is that I can't manage to create a Modelless form for it, because it won't map dot notations to array, and searchs the fields like consumer.name literally in request->data. Is there anything I can do to validate this data with a simple $form->execute()?
My Cake version is 3.1.1
The code:
//Form Class
protected function _buildSchema(Schema $schema)
{
return $schema->addField('consumer.name', 'string')
->addField('consumer.card_number', ['type' => 'string'])
->addField('consumer.expire_month', ['type' => 'string'])
->addField('consumer.expire_year', ['type' => 'string'])
->addField('type', ['type' => 'string'])
->addField('consumer.cvv', ['type' => 'string']);
}
protected function _buildValidator(Validator $validator)
{
return $validator->add('consumer.name', 'length', [
'rule' => ['minLength', 3],
'message' => 'Consumer name too short',
])
->requirePresence('type', true)
->requirePresence('consumer.name', true)
->requirePresence('consumer.card_number', true)
->requirePresence('consumer.expire_month', true)
->requirePresence('consumer.expire_year', true)
->requirePresence('consumer.cvv', true)
->add('consumer.card_number', 'validFormat', [
'rule' => array('custom', '/[0-9]{16}/'),
'message' => 'A valid card number is required',
])
->add('consumer.expire_month', 'validFormat', [
'rule' => array('custom', '/[01]?[0-9]/'),
'message' => 'A valid expire month is required',
])
->add('consumer.expire_year', 'validFormat', [
'rule' => array('custom', '/[0-9]{2}/'),
'message' => 'A valid expire year is required',
])
->add('consumer.cvv', 'validFormat', [
'rule' => array('custom', '/[0-9]{3}/'),
'message' => 'A valid CVV is required',
]);
}
My view has several inputs like this:
<?php echo $this->Form->input('consumer.name', [
'label' => false,
'class' => 'form-control inputbox_user',
'pattern' => "^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$",
'required' => true,
'title' => 'Introduce el nombre del titular de la tarjeta.',
]);
?>
And my controller logic:
$form = new PaymentForm;
if ($this->request->is(['patch', 'post', 'put'])){
if( $form->execute($this->request->data) ){
//ok
}
}
}
The dot notation will be already turned into a "HTML array" in the view. Debug your request and you'll see ['consumer']['name']. This works as intended by the framework and is correct. AFAIK there is no way around that - and not needed.
Simply change your notation to underscore: consumer_name for example. To use the dots is for another reason a bad idea as well: You will make things difficult for yourself if you need to access them via JS or CSS.

How to get validation when saving multiple records with newEntities() CakePhp 3.x?

For some reason I am not getting any validation errors when saving multiple records. I can grab the errors using print_r($user->errors()); but they are not automatically injected into the form like when adding a single user. According to the docs "Validating entities before saving is done automatically when using the newEntity(), newEntities()." I am not sure if there is a specific way to set up the form to make it return validation for multiple records or if you have to do special validation in the model for inputs that have indexes or what?
view:
<div class="page-wrap">
<div class="form">
<h1>Join Now</h1>
<?php
echo $this->Form->create(null, ['controller' => 'users', 'action' => 'addMultiple']);
echo $this->Form->input('1.full_name');
echo $this->Form->input('1.username');
echo $this->Form->input('1.email');
echo $this->Form->input('1.password');
echo $this->Form->input('1.password_confirmation', array('type' => 'password'));
if ($current_user['role'] === 1 && isset($logged_in)) {
echo $this->Form->input('1.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
}
echo $this->Form->input('2.full_name');
echo $this->Form->input('2.username');
echo $this->Form->input('2.email');
echo $this->Form->input('2.password');
echo $this->Form->input('2.password_confirmation', array('type' => 'password'));
if ($current_user['role'] === 1 && isset($logged_in)) {
echo $this->Form->input('2.role', ['type' => 'select', 'options' => ['1' => 'Admin', '2' => 'Editor', '3' => 'Author', '4' => 'Reader'], 'default' => '4']);
}
echo $this->Form->button(__('Sign Up'));
echo $this->Form->end();
?>
</div>
</div>
Controller:
public function addMultiple()
{
$users = $this->Users->newEntities($this->request->data());
if ($this->request->is('post')) {
foreach($users as $user) {
if( empty($this->request->session()->read('Auth.User')) || $this->request->session()->read('Auth.User.role') !== 1 ) {
$user->role = 4;
}
if ($this->Users->save($user)) {
$this->Flash->success(__('You have been added.'));
} else {
$this->Flash->error(__('You could not be added. Please, try again.'));
}
}
}
}
Table:
public function initialize(array $config)
{
parent::initialize($config);
$this->table('users');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('MembershipOrders', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->hasMany('MembershipOrders', [
'foreignKey' => 'affiliate_token',
'joinType' => 'INNER'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->notEmpty('full_name', 'A full name is required')
->add('full_name', 'notBlank', [
'rule' => 'notBlank',
'message' => __('A full name is required'),
]);
$validator
->notEmpty('username', 'A username is required')
->add('username', [
'notBlank' => [
'rule' => 'notBlank',
'message' => __('A username is required'),
]
]);
$validator
->notEmpty('email', 'An email is required')
->add('email', [
'notBlank' => [
'rule' => 'notBlank',
'message' => __('A full name is required'),
],
'unique' => [
'rule' => 'validateUnique',
'provider' => 'table',
'message' => __('That email has already been used.'),
]
]);
$validator
->notEmpty('old_password', 'You must enter your old password is required')
->add('old_password', 'notBlank', [
'rule' => 'notBlank',
'message' => __('Your old password is required'),
]);
$validator
->notEmpty('password', 'A password is required')
->add('password', 'notBlank', [
'rule' => 'notBlank',
'message' => __('A full name is required'),
]);
$validator
->notEmpty('password_confirmation', 'Password confirmation is required')
->add('password_confirmation',
'comareWith', [
'rule' => ['compareWith', 'password'],
'message' => 'Passwords do not match.'
]);
$validator
->notEmpty('role', 'A role is required')
->add('role', 'inList', [
'rule' => ['inList', ['1', '2', '3', '4']],
'message' => 'Please enter a valid role'
]);
return $validator;
}
You can use 'addNestedMany()' : http://book.cakephp.org/3.0/en/core-libraries/validation.html#nesting-validators
You have to pass the entity object to the Form->create(... function, instead of passing nullas the following:
echo $this->Form->create($user, .....

Custom validation errors in laravel

In updating profile i use a validator class method:
class UpdateRequest extends Request {
public function authorize() { return true; }
public function rules() {
return [
'name' => 'required',
'email' => 'required|email',
];
}
}
How to add an additional validation error like:
public function postUpdate(UpdateRequest $request)
if($user->email == $request->get('email')) {
$request->addEerror("The email has already been taken."); //shows an fatal error
}
}
?
Thank you
You have not mentioned which Laravel version you are using, assuming 5.1
You can create a message array for different validation type, like the example below:
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email|unique:users,email,'.$user->id
];
In your resources/lang/en/validation.php file
$custom_messages = [
'required' => 'The :attribute field is required.',
'email' => [
'required' => 'The email id field is required.',
'email' => 'Please enter a valid email format.',
'unique' => 'The email id has already been taken.',
]
];
This should do the trick.

Categories