This is my model:
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
if ($this->scenario == "insert") {
return array(
array('requisition_id, sync', 'numerical', 'integerOnly' => true),
array('lastname, firstname, email, dob, phone, cv_path, experienceMonths, experienceYears, competencies, token', 'required', 'message' => "Câmpul este obligatoriu"),
array('email', 'email', 'message' => "Emailul este invalid!"),
array('dob', 'validateDob'),
array('dayOfBirth, monthOfBirth, yearOfBirth', 'safe'),
array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'),
array('verifyCode', 'on' => 'insert'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
);
} else if ($this->scenario == 'taleoUpdate') {
return array(
array('taleo_id, sync', 'required'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
);
}
else if($this->scenario == 'notjobapply'){
return array(
array('lastname, firstname, email, phone, cv_path, requisition_id', 'required', 'message'=>'Câmpul este obligatoriu'),
array('email', 'email', 'message' => "Emailul este invalid!"),
);
}
return array(
array('id, lastname, email, phone, dob, requisition_id, experienceMonths, experienceYears, sync, cv_path, created', 'safe', 'on' => 'search'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'captchaRequired'),
);
}
My problem is that it does not validate the letters from the image. I don't know why. I think that my validation is not correct in the rules. Anyone any clues?
Yes you have a lot of scenarios and applying different rules to them.
Make sure that the scenario that you're currently running does have a validation rule.
I can see that you use scenarios in two ways, one is the conditions you're using that I'm not fully sure they work, but maybe they do.
And the second is the Yii way, by specifying in the 'on' attribute in every rule declaration.
I would recommend writing all the rules again from scratch in this structure:
return array(
// scenarioA
array('field1, field2', 'required', 'on' => 'scenarioA'),
array('field1, field2', 'required', 'on' => 'scenarioA'),
array('field1, field2', 'required', 'on' => 'scenarioA'),
// scenarioB
array('field3, field4', 'required', 'on' => 'scenarioB'),
array('field3, field4', 'required', 'on' => 'scenarioB'),
array('field3, field4', 'required', 'on' => 'scenarioB'),
// scenarioC
array('field5, field6', 'required', 'on' => 'scenarioC'),
array('field5, field6', 'required', 'on' => 'scenarioC'),
array('field5, field6', 'required', 'on' => 'scenarioC'),
);
Or you could keep your condition blocks solution if you test that they behave correctly, but in that case you should remove the 'on' parameter from the rules inside the blocks.
Because for example here:
if ($this->scenario == "insert") {
return array(
...
array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'),
You're putting code inside the condition where you verify that scenario is 'insert' but then again you specify the 'on' that will make that rule only be applied on 'taleoUpdate' scenario so it makes no sense.
Oh and as for the captcha, as you can see with your format you only reach the rule for it if the scenario is different from those you've specified above.
Related
i have a table in my database( table case ) that contains case type id which is a foreign key to the table (case_type). when users fill this table they dont know the case type id they only know the category of the case. so what i want to do is when users fill the case table they write the case type category and based on that it match the category with the case id since i have a separate table and submit that id in to the case table.
so far what i have tried :
from vue i send the category selected from a dropdown by the user to this controller function
public function getIdOfCategoryByName(Request $request){
$name = $request->input('name');
$getidbyname=case_type::where('category',$name)->get('id');
return $getidbyname;
}
this is where i dont know how to insert the id that i got back to this function
public function createcase(){
$request->validate([
'title' => 'required',
'description' => 'required',
'caseTypeId' => 'required',
'amountreceived' => 'required',
'amoutneeded' =>'required',
'uid' => 'required',
'visible' => 'required',
'image' => 'required',
'cityId' => 'required'
]);
waitingdonations::create($request->post());
return response()->json([
'message' => 'waiting donation successfull',
]);
}
if my question is unclear please check this website https://towardsdatascience.com/sql-insert-values-with-joined-ids-from-another-table-83ff7f149296
I'm trying to display all images that have a certain custom field from the types plugin set to true. It would also work to filter them by post_content or post_excerpt but none of my attempts have worked so far.
<?
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_content' => 'foo',
'numberposts' => -1
);
?>
<? print_r(get_posts($args)); ?>
This get's all images allthough only one has the post_content foo. My attempt to use WP_Query failed miserably as well.
Any help is appreciated!
WP_Query method :
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
I am presuming that the reason why you failed with WP_Query is due to the following condition.
Codex states : The default WP_Query sets 'post_status'=>'publish', but attachments default to 'post_status'=>'inherit' so you'll need to explicitly set post_status to 'inherit' or 'any' as well.
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
get_posts method :
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'meta_key' => 'custom-field',
'meta_value' => 'custom value',
'numberposts' => -1
);
print_r(get_posts($args));
The only draw back with this method is that the meta_value needs to exactly match what was entered in the custom field. If you still like to use get_posts then use the meta_query as shown in WP_Query example above.
I'm having troubles to generate an sql query with operators.Actually wirte this code:
$args = array(
'post_type' => 'listings',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(
//CITY
array(
'key' => 'city',
'value' => $city,
'compare' => 'LIKE'
),
//type
array(
'key' => 'type',
'value' => $type,
'compare' => 'LIKE'
),
//PRICE
array(
'key' => 'price',
'value' => array( 100, 5000 ),
'compare' => 'BETWEEN'
),
)
);
This code work fine, but now, i need to get the posts with price between $start and $end, OR price = -1. In other words, post that not have set the price, or set to -1 must show in the query. The others conditions (city, type,etc) must be match with AND
Any ideas? Thanks!
My initial thought is to add a filter onto posts_where or posts_join to inject a custom SQL condition into the query.
Something along these lines:
wp_postmeta.meta_key='price' AND
(wp_postmeta.meta_value='-1' OR
CAST(wp_postmeta.meta_value AS INT) BETWEEN $start AND $end)
i am new in cakephp so i dont know i to how write this query in cakephp . at times now i have this query
$count = $this->User->find('count', array(
'conditions' => array('User.mobileNo' => $mobileNo)));
this query is checking that if the mobile number in database is equal to the one the user has given .. i want to add another condition which is
mobile number is equal to the one the user has given the mobile number and email is equal to the one the user has given the email for example
$count = $this->User->find('count', array(
'conditions' => array('User.mobileNo' => $mobileNo))) And
'conditions' => array('User.email' => $email)))
You need only add to your conditions array:
$count = $this->User->find('count', array(
'conditions' => array(
'User.mobileNo' => $mobileNo,
'User.email' => $email
)
));
There are many examples like this in the documentation e.g..
$conditions = array("Post.title" => "This is a post", "Post.author_id" => 1);
// Example usage with a model:
$this->Post->find('first', array('conditions' => $conditions));
I've had a working registration/update model, I wanted to expand on my models so I added in a regex to the password field. I have checked the regex works online and even my client side validation shows it works, but the model refuses to save now. I'm not really sure why, could anyone help me out please?
return array(
array('firstName, lastName, email, password', 'required'),
array('firstName', 'length', 'max'=>45),
array('lastName', 'length', 'max'=>80),
array('email', 'length', 'max'=>120),
// email must be valid email
array('email', 'email'),
// email must be unique
array('email', 'unique'),
// Regex for password
array('password','match', 'pattern'=>'/^[a-z0-9_-]{7,20}$/i',
'message'=>'The password must be between 7 and 20 characters
long'),
array('password', 'length', 'min'=>7, 'max'=>64),
array('date_modified', 'safe'),
array('active, date_modified', 'default', 'setOnEmpty' => true, 'value' => null),
array('id, first_name, last_name, email, pass, active, date_created, date_modified, live', 'safe', 'on'=>'search'),
);
Thanks
Jonny
You can create your own validation rule.
http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/
Or else you can define validation rule in YII Model, something like this:
return array(
array('password', 'length', 'min'=>7, 'max'=>64),
array('password','pattern'=>'/^[A-Za-z0-9_!##$%^&*()+=?.,]+$/u', 'message'=>'Spaces or given characters are not allowed'),
);
There are more validation you can specify in your model.